Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from dbarbuzzi/refactor-config-file-location
Browse files Browse the repository at this point in the history
Refactor config file location
  • Loading branch information
dbarbuzzi authored Oct 24, 2018
2 parents 7d44161 + d706a26 commit 5b7e51a
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions tvd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,24 @@ var (
ClientID string
// Version is the build/release version
Version = "dev"

DefaultConfig = Config{
ClientID: ClientID,
Workers: 4,
StartTime: "0 0 0",
EndTime: "end",
Quality: "best",
}
DefaultConfigFolder = os.ExpandEnv("${HOME}/.config/tvd/")
DefaultConfigFile = "config.toml"
DefaultConfigPath = filepath.Join(DefaultConfigFolder, DefaultConfigFile)
)

// command-line args/flags
var (
clientID = kingpin.Flag("client", "Twitch app Client ID").Short('C').String()
workers = kingpin.Flag("workers", "Max number of concurrent downloads (default: 4)").Short('w').Int()
configFile = kingpin.Flag("config", "Path to config file").Short('c').Default("config.toml").String()
configFile = kingpin.Flag("config", "Path to config file (default: $HOME/.config/tvd/config.toml)").Short('c').String()
logFile = kingpin.Flag("logfile", "Path to logfile").Short('L').String()

vodID = kingpin.Arg("vod", "ID of the VOD to download").Default("0").Int()
Expand Down Expand Up @@ -76,20 +87,27 @@ func main() {
}

// set base config
config := Config{
ClientID: ClientID,
Workers: 4,
StartTime: "0 0 0",
EndTime: "end",
Quality: "best",
}
config := DefaultConfig
log.Printf("default config: %+v\n", config.WithoutClientID())

// config file
fileConfig, err := loadConfig(*configFile)
configfile := DefaultConfigPath
if *configFile != "" {
configfile = *configFile
}
fileConfig, err := loadConfig(configfile)
if err != nil {
fmt.Println(err)
log.Fatalln(err)
if configfile == DefaultConfigPath {
log.Print("creating default config file")
innerErr := createDefaultConfigFile()
if innerErr != nil {
fmt.Println(err)
log.Fatalln(err)
}
} else {
fmt.Println(err)
log.Fatalln(err)
}
}
log.Printf("config from file: %+v\n", config.WithoutClientID())
config.Update(fileConfig)
Expand Down Expand Up @@ -126,6 +144,24 @@ func main() {
}
}

func createDefaultConfigFile() error {
err := os.MkdirAll(DefaultConfigFolder, os.ModePerm)
if err != nil {
return err
}

configFile, err := os.Create(DefaultConfigPath)
if err != nil {
return err
}
defer configFile.Close()

// TODO: marshal default config toml to file
e := toml.NewEncoder(configFile)
err = e.Encode(DefaultConfig)
return err
}

// DownloadVOD downloads a VOD based on the various info passed in the config
func DownloadVOD(cfg Config) error {
fmt.Println("Fetching access token")
Expand Down

0 comments on commit 5b7e51a

Please sign in to comment.