Added configuration flags

This commit is contained in:
Pijus Kamandulis 2019-04-20 22:30:42 +03:00
parent 39d14eaf7a
commit e13f98eb4a
3 changed files with 34 additions and 10 deletions

View File

@ -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)
}
}

13
main.go
View File

@ -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"

29
structs/config.go Normal file
View File

@ -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
}