From eb085a8e4cc3e447f24dd5b04894cad9c5b45328 Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 7 Dec 2022 17:46:51 -0800 Subject: [PATCH] adding config param --- aocdl/config.go | 44 +++++++++++++++++------------ aocdl/main.go | 73 ++++++++++++++++++++++++------------------------- 2 files changed, 61 insertions(+), 56 deletions(-) diff --git a/aocdl/config.go b/aocdl/config.go index 7bc8a4e..bc406f9 100644 --- a/aocdl/config.go +++ b/aocdl/config.go @@ -17,31 +17,39 @@ type configuration struct { Wait bool `json:"-"` } -func loadConfigs() (*configuration, error) { +func loadConfigs(configPath string) (*configuration, error) { config := new(configuration) - home := "" - usr, err := user.Current() - if err == nil { - home = usr.HomeDir - } - - if home != "" { - err = config.mergeWithFileIfExists(filepath.Join(home, ".aocdlconfig")) + // If a config path was given, use it above any other defaults + if len(configPath) > 0 { + err := config.mergeWithFileIfExists(configPath) if err != nil { return nil, err } - } + } else { + home := "" + usr, err := user.Current() + if err == nil { + home = usr.HomeDir + } - wd, _ := os.Getwd() + if home != "" { + err = config.mergeWithFileIfExists(filepath.Join(home, ".aocdlconfig")) + if err != nil { + return nil, err + } + } - // If we could not determine either directory or if we are not currently in - // the home directory, try and load the configuration relative to the - // current working directory. - if wd == "" || home == "" || wd != home { - err = config.mergeWithFileIfExists(".aocdlconfig") - if err != nil { - return nil, err + wd, _ := os.Getwd() + + // If we could not determine either directory or if we are not currently in + // the home directory, try and load the configuration relative to the + // current working directory. + if wd == "" || home == "" || wd != home { + err = config.mergeWithFileIfExists(".aocdlconfig") + if err != nil { + return nil, err + } } } diff --git a/aocdl/main.go b/aocdl/main.go index b9c6c6f..208b3ad 100644 --- a/aocdl/main.go +++ b/aocdl/main.go @@ -9,7 +9,6 @@ import ( "math/rand" "net/http" "os" - "strconv" "text/template" "time" @@ -28,6 +27,9 @@ const usageMessage = `Usage: Options: + -config-path ../.aocdlconfig + Load config from the path specified. + -session-cookie 0123456789...abcdef Use the specified string as session cookie. @@ -65,21 +67,29 @@ Please provide your session cookie as a command line parameter: aocdl -session-cookie 0123456789...abcdef -Or create a configuration file named '.aocdlconfig' in your home directory or in -the current directory and add the 'session-cookie' key: +Or create a configuration file named '.aocdlconfig' in your home directory, +current directory, or passed in with the -config-path option and add the +'session-cookie' key: { "session-cookie": "0123456789...abcdef" } ` +type startupFlags struct { + configuration + ConfigPath string +} + func main() { rand.Seed(time.Now().Unix()) - config, err := loadConfigs() + flags := parseFlags() + + config, err := loadConfigs(flags.ConfigPath) checkError(err) - addFlags(config) + addFlags(flags, config) if config.SessionCookie == "" { fmt.Fprintln(os.Stderr, missingSessionCookieMessage) @@ -145,32 +155,23 @@ func checkError(err error) { } } -func addFlags(config *configuration) { +func parseFlags() startupFlags { flags := flag.NewFlagSet("", flag.ContinueOnError) ignored := new(bytes.Buffer) flags.SetOutput(ignored) - sessionCookieFlag := flags.String("session-cookie", "", "") - outputFlag := flags.String("output", "", "") - yearFlag := flags.String("year", "", "") - dayFlag := flags.String("day", "", "") + var input startupFlags - forceFlag := flags.Bool("force", false, "") - waitFlag := flags.Bool("wait", false, "") - - var year, day int + flags.StringVar(&input.SessionCookie, "session-cookie", "", "") + flags.StringVar(&input.Output, "output", "", "") + flags.IntVar(&input.Year, "year", 0, "") + flags.IntVar(&input.Year, "day", 0, "") + flags.BoolVar(&input.Force, "force", false, "") + flags.BoolVar(&input.Wait, "wait", false, "") flagErr := flags.Parse(os.Args[1:]) - if flagErr == nil { - year, flagErr = parseIntFlag(*yearFlag) - } - - if flagErr == nil { - day, flagErr = parseIntFlag(*dayFlag) - } - if flagErr == flag.ErrHelp { fmt.Println(titleAboutMessage) fmt.Println(usageMessage) @@ -185,31 +186,27 @@ func addFlags(config *configuration) { os.Exit(1) } - flagConfig := new(configuration) - flagConfig.SessionCookie = *sessionCookieFlag - flagConfig.Output = *outputFlag - flagConfig.Year = year - flagConfig.Day = day + return input +} + +func addFlags(flags startupFlags, config *configuration) { + flagConfig := &configuration{ + SessionCookie: flags.SessionCookie, + Output: flags.Output, + Year: flags.Year, + Day: flags.Day, + } config.merge(flagConfig) - if *forceFlag { + if flags.Force { config.Force = true } - if *waitFlag { + if flags.Wait { config.Wait = true } } -func parseIntFlag(text string) (int, error) { - if text == "" { - return 0, nil - } - // Parse in base 10. - value, err := strconv.ParseInt(text, 10, 0) - return int(value), err -} - func renderOutput(config *configuration) error { tmpl, err := template.New("output").Parse(config.Output) if err != nil {