From 4496cc7c31ce6a903ecc56b8a4bf3e6fefafe1cd Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 16 Jun 2018 09:13:23 -0700 Subject: [PATCH] Clean up the flag and config handling in main() --- cfg/config_files.go | 38 ++++++++++++------------ flags/flags.go | 6 ++++ wtf.go | 72 ++++++++++++++++++++++----------------------- 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/cfg/config_files.go b/cfg/config_files.go index ff7f997e..46a99780 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -30,6 +30,25 @@ func CreateConfigDir() { } } +// CreateConfigFile creates a simple config file in the config directory if +// one does not already exist +func CreateConfigFile() { + filePath, err := CreateFile("config.yml") + if err != nil { + panic(err) + } + + // If the file is empty, write to it + file, err := os.Stat(filePath) + + if file.Size() == 0 { + err = ioutil.WriteFile(filePath, []byte(simpleConfig), 0644) + if err != nil { + panic(err) + } + } +} + // CreateFile creates the named file in the config directory, if it does not already exist. // If the file exists it does not recreate it. // If successful, eturns the absolute path to the file @@ -88,25 +107,6 @@ func ReadConfigFile(fileName string) (string, error) { return string(fileData), nil } -// WriteConfigFile creates a simple config file in the config directory if -// one does not already exist -func WriteConfigFile() { - filePath, err := CreateFile("config.yml") - if err != nil { - panic(err) - } - - // If the file is empty, write to it - file, err := os.Stat(filePath) - - if file.Size() == 0 { - err = ioutil.WriteFile(filePath, []byte(simpleConfig), 0644) - if err != nil { - panic(err) - } - } -} - const simpleConfig = `wtf: colors: border: diff --git a/flags/flags.go b/flags/flags.go index 131ed856..3e798e4e 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -22,6 +22,10 @@ func NewFlags() *Flags { /* -------------------- Exported Functions -------------------- */ +func (flags *Flags) ConfigFilePath() string { + return flags.Config +} + func (flags *Flags) HasConfig() bool { return len(flags.Config) > 0 } @@ -42,6 +46,8 @@ func (flags *Flags) Parse(version string) { } } + // If no config file is explicitly passed in as a param, + // set the flag to the default config file if !flags.HasConfig() { homeDir, err := wtf.Home() if err != nil { diff --git a/wtf.go b/wtf.go index c545fd52..dd25461c 100644 --- a/wtf.go +++ b/wtf.go @@ -86,8 +86,38 @@ func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { return event } -func loadConfig(filePath string) { +func loadConfigFile(filePath string) { Config = cfg.LoadConfigFile(filePath) + + // Always in alphabetical order + bamboohr.Config = Config + bargraph.Config = Config + bittrex.Config = Config + blockfolio.Config = Config + circleci.Config = Config + clocks.Config = Config + cmdrunner.Config = Config + cryptolive.Config = Config + gcal.Config = Config + git.Config = Config + github.Config = Config + gitlab.Config = Config + gspreadsheets.Config = Config + ipapi.Config = Config + ipinfo.Config = Config + jenkins.Config = Config + jira.Config = Config + newrelic.Config = Config + opsgenie.Config = Config + power.Config = Config + prettyweather.Config = Config + security.Config = Config + status.Config = Config + system.Config = Config + textfile.Config = Config + todo.Config = Config + weather.Config = Config + wtf.Config = Config } // redrawApp redraws the rendered views to screen on a defined interval (set in config.yml) @@ -119,7 +149,7 @@ func setTerm() { os.Setenv("TERM", Config.UString("wtf.term", os.Getenv("TERM"))) } -func watchForConfigChanges(app *tview.Application, configFlag string, grid *tview.Grid, pages *tview.Pages) { +func watchForConfigChanges(app *tview.Application, configFilePath string, grid *tview.Grid, pages *tview.Pages) { watch := watcher.New() // notify write events. @@ -129,7 +159,7 @@ func watchForConfigChanges(app *tview.Application, configFlag string, grid *tvie for { select { case <-watch.Event: - loadConfig(configFlag) + loadConfigFile(configFilePath) // Disable all widgets to stop scheduler goroutines and rmeove widgets from memory. disableAllWidgets() makeWidgets(app, pages) @@ -145,7 +175,7 @@ func watchForConfigChanges(app *tview.Application, configFlag string, grid *tvie }() // Watch config file for changes. - if err := watch.Add(configFlag); err != nil { + if err := watch.Add(configFilePath); err != nil { log.Fatalln(err) } @@ -217,36 +247,6 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) { } func makeWidgets(app *tview.Application, pages *tview.Pages) { - // Always in alphabetical order - bamboohr.Config = Config - bargraph.Config = Config - bittrex.Config = Config - blockfolio.Config = Config - circleci.Config = Config - clocks.Config = Config - cmdrunner.Config = Config - cryptolive.Config = Config - gcal.Config = Config - git.Config = Config - github.Config = Config - gitlab.Config = Config - gspreadsheets.Config = Config - ipapi.Config = Config - ipinfo.Config = Config - jenkins.Config = Config - jira.Config = Config - newrelic.Config = Config - opsgenie.Config = Config - power.Config = Config - prettyweather.Config = Config - security.Config = Config - status.Config = Config - system.Config = Config - textfile.Config = Config - todo.Config = Config - weather.Config = Config - wtf.Config = Config - mods, _ := Config.Map("wtf.mods") for mod := range mods { if enabled := Config.UBool("wtf.mods."+mod+".enabled", false); enabled { @@ -275,9 +275,9 @@ func main() { } cfg.CreateConfigDir() - cfg.WriteConfigFile() + cfg.CreateConfigFile() + loadConfigFile(flags.ConfigFilePath()) - loadConfig(flags.Config) setTerm() app := tview.NewApplication()