Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding config-path flag #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions aocdl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
73 changes: 35 additions & 38 deletions aocdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"math/rand"
"net/http"
"os"
"strconv"
"text/template"
"time"

Expand All @@ -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.

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down