diff --git a/cfg/config_files.go b/cfg/config_files.go index c0263368..12058940 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -140,12 +140,17 @@ func CreateFile(fileName string) (string, error) { } // LoadWtfConfigFile loads the config.yml file to configure the app -func LoadWtfConfigFile(filePath string) *config.Config { +func LoadWtfConfigFile(filePath string, isCustomConfig bool) *config.Config { absPath, _ := expandHomeDir(filePath) cfg, err := config.ParseYamlFile(absPath) if err != nil { - displayWtfConfigFileLoadError(err) + if isCustomConfig { + displayWtfCustomConfigFileLoadError(err) + } else { + displayWtfConfigFileLoadError(err) + } + os.Exit(1) } @@ -242,6 +247,17 @@ func displayWtfConfigFileLoadError(err error) { fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) } +func displayWtfCustomConfigFileLoadError(err error) { + fmt.Println("\n\033[1mERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.") + fmt.Println() + fmt.Println("This could mean one of two things:") + fmt.Println() + fmt.Println(" 1. That file doesn't exist.") + fmt.Println(" 2. That file has a YAML syntax error. Try running it through http://www.yamllint.com to check for errors.") + fmt.Println() + fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) +} + // Expand expands the path to include the home directory if the path // is prefixed with `~`. If it isn't prefixed with `~`, the path is // returned as-is. diff --git a/flags/flags.go b/flags/flags.go index 992c343a..c16f8c64 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -46,8 +46,8 @@ func (flags *Flags) RenderIf(version string, config *config.Config) { } } -// HasConfig returns TRUE if a config path was passed in, FALSE if one was not -func (flags *Flags) HasConfig() bool { +// HasCustomConfig returns TRUE if a config path was passed in, FALSE if one was not +func (flags *Flags) HasCustomConfig() bool { return len(flags.Config) > 0 } @@ -72,7 +72,7 @@ func (flags *Flags) Parse() { // If no config file is explicitly passed in as a param, // set the flag to the default config file - if !flags.HasConfig() { + if !flags.HasCustomConfig() { homeDir, err := utils.Home() if err != nil { fmt.Printf("Error: %v\n", err) diff --git a/main.go b/main.go index 131dad5b..bdb6e824 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,7 @@ func refreshAllWidgets(widgets []wtf.Wtfable) { } } -func watchForConfigChanges(app *tview.Application, configFilePath string, grid *tview.Grid, pages *tview.Pages) { +func watchForConfigChanges(app *tview.Application, configFilePath string, isCustomConfig bool, grid *tview.Grid, pages *tview.Pages) { watch := watcher.New() absPath, _ := utils.ExpandHomeDir(configFilePath) @@ -93,7 +93,7 @@ func watchForConfigChanges(app *tview.Application, configFilePath string, grid * // Disable all widgets to stop scheduler goroutines and remove widgets from memory disableAllWidgets(runningWidgets) - config := cfg.LoadWtfConfigFile(absPath) + config := cfg.LoadWtfConfigFile(absPath, false) widgets := maker.MakeWidgets(app, pages, config) runningWidgets = widgets @@ -128,6 +128,7 @@ func watchForConfigChanges(app *tview.Application, configFilePath string, grid * func main() { log.SetFlags(log.LstdFlags | log.Lshortfile) + // Manage the configuration directories and file cfg.MigrateOldConfig() cfg.CreateXdgConfigDir() cfg.CreateWtfConfigDir() @@ -136,7 +137,7 @@ func main() { // Parse and handle flags flags := flags.NewFlags() flags.Parse() - config := cfg.LoadWtfConfigFile(flags.ConfigFilePath()) + config := cfg.LoadWtfConfigFile(flags.ConfigFilePath(), flags.HasCustomConfig()) flags.RenderIf(version, config) if flags.Profile { @@ -165,7 +166,7 @@ func main() { app.SetInputCapture(keyboardIntercept) - go watchForConfigChanges(app, flags.Config, display.Grid, pages) + go watchForConfigChanges(app, flags.Config, flags.HasCustomConfig(), display.Grid, pages) if err := app.SetRoot(pages, true).Run(); err != nil { fmt.Printf("Error: %v\n", err)