diff --git a/fileio/file_rw.go b/fileio/file_rw.go index 929d2bc..60664af 100644 --- a/fileio/file_rw.go +++ b/fileio/file_rw.go @@ -29,7 +29,7 @@ func WriteToFile(filename string, content string) { // InitOutputDirectory - Creates output directory func InitOutputDirectory(path string) { if _, err := os.Stat(path); os.IsNotExist(err) { - os.Mkdir(path, os.ModePerm) + os.MkdirAll(path, os.ModePerm) } } diff --git a/main.go b/main.go index d56def9..81d9a4e 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "os" "path/filepath" . "./fileio" @@ -13,17 +12,13 @@ import ( ) func main() { - args := os.Args[1:] - if len(args) < 1 { - fmt.Println("Usage: rss-dl [FEED_URL]") - return - } + GetConfig() fp := gofeed.NewParser() - LogInfo("Downloading " + args[0]) - feed, _ := fp.ParseURL(args[0]) + LogInfo("Downloading " + Config.FeedURL) + feed, _ := fp.ParseURL(Config.FeedURL) - outputDir := ToCleanString(feed.Title) + outputDir := Config.OutputPath + "/" + ToCleanString(feed.Title) InitOutputDirectory(outputDir) feedInfoPath := outputDir + "/feed_details.json" diff --git a/structs/config.go b/structs/config.go new file mode 100644 index 0000000..fc4c6a6 --- /dev/null +++ b/structs/config.go @@ -0,0 +1,29 @@ +package structs + +import ( + "flag" + "fmt" + "os" +) + +// Config - Runtime configuration +var Config struct { + FeedURL string + OutputPath string +} + +// GetConfig - Returns Config object +func GetConfig() { + outputPath := flag.String("output", ".", "Output path") + + flag.Parse() + + args := flag.Args() + if len(args) < 1 { + fmt.Println("Usage: rss-dl [OPTIONS] FEED_URL") + os.Exit(2) + } + + Config.FeedURL = flag.Args()[len(args)-1] + Config.OutputPath = *outputPath +}