From 452e6f20db048d2502790162e03f9f2e952a61b4 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 20 Jul 2019 12:23:19 -0700 Subject: [PATCH] WTF-510 Add error messaging if the config directories cannot be created --- cfg/config_files.go | 49 +++++++++++++++++++++++++++------------- main.go | 4 ++-- modules/todo/keyboard.go | 2 +- modules/todo/widget.go | 4 ++-- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/cfg/config_files.go b/cfg/config_files.go index 3a9a8a6d..c0263368 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -56,7 +56,7 @@ func MigrateOldConfig() { /* -------------------- Config Migration -------------------- */ // ConfigDir returns the absolute path to the configuration directory -func ConfigDir() (string, error) { +func WtfConfigDir() (string, error) { configDir, err := expandHomeDir(WtfConfigDirV2) if err != nil { return "", err @@ -73,7 +73,8 @@ func CreateXdgConfigDir() { if _, err := os.Stat(xdgConfigDir); os.IsNotExist(err) { err := os.Mkdir(xdgConfigDir, os.ModePerm) if err != nil { - panic(err) + displayXdgConfigDirCreateError(err) + os.Exit(1) } } } @@ -81,12 +82,13 @@ func CreateXdgConfigDir() { // CreateWtfConfigDir creates the necessary directories for storing the default config file // If ~/.config/wtf is missing, it will try to create it func CreateWtfConfigDir() { - wtfConfigDir, _ := ConfigDir() + wtfConfigDir, _ := WtfConfigDir() if _, err := os.Stat(wtfConfigDir); os.IsNotExist(err) { err := os.Mkdir(wtfConfigDir, os.ModePerm) if err != nil { - panic(err) + displayWtfConfigDirCreateError(err) + os.Exit(1) } } } @@ -114,7 +116,7 @@ func CreateWtfConfigFile() { // If successful, eturns the absolute path to the file // If unsuccessful, returns an error func CreateFile(fileName string) (string, error) { - configDir, err := ConfigDir() + configDir, err := WtfConfigDir() if err != nil { return "", err } @@ -137,21 +139,13 @@ func CreateFile(fileName string) (string, error) { return filePath, nil } -// LoadConfigFile loads the config.yml file to configure the app -func LoadConfigFile(filePath string) *config.Config { +// LoadWtfConfigFile loads the config.yml file to configure the app +func LoadWtfConfigFile(filePath string) *config.Config { absPath, _ := expandHomeDir(filePath) cfg, err := config.ParseYamlFile(absPath) if err != nil { - 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. Your \033[0;33mconfig.yml\033[0m file is missing. Check in \033[0;33m~/.config/wtf\033[0m to see if \033[0;33mconfig.yml\033[0m is there.") - fmt.Println(" 2. Your \033[0;33mconfig.yml\033[0m file has a 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()) - + displayWtfConfigFileLoadError(err) os.Exit(1) } @@ -225,6 +219,29 @@ const simpleConfig = `wtf: /* -------------------- Unexported Functions -------------------- */ +func displayXdgConfigDirCreateError(err error) { + fmt.Printf("\n\033[1mERROR:\033[0m Could not create the '\033[0;33m%s\033[0m' directory.\n", XdgConfigDir) + fmt.Println() + fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) +} + +func displayWtfConfigDirCreateError(err error) { + fmt.Printf("\n\033[1mERROR:\033[0m Could not create the '\033[0;33m%s\033[0m' directory.\n", WtfConfigDirV2) + fmt.Println() + fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", err.Error()) +} + +func displayWtfConfigFileLoadError(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. Your \033[0;33mconfig.yml\033[0m file is missing. Check in \033[0;33m~/.config/wtf\033[0m to see if \033[0;33mconfig.yml\033[0m is there.") + fmt.Println(" 2. Your \033[0;33mconfig.yml\033[0m file has a 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/main.go b/main.go index 20e5c434..131dad5b 100644 --- a/main.go +++ b/main.go @@ -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.LoadConfigFile(absPath) + config := cfg.LoadWtfConfigFile(absPath) widgets := maker.MakeWidgets(app, pages, config) runningWidgets = widgets @@ -136,7 +136,7 @@ func main() { // Parse and handle flags flags := flags.NewFlags() flags.Parse() - config := cfg.LoadConfigFile(flags.ConfigFilePath()) + config := cfg.LoadWtfConfigFile(flags.ConfigFilePath()) flags.RenderIf(version, config) if flags.Profile { diff --git a/modules/todo/keyboard.go b/modules/todo/keyboard.go index a24fc083..fad85952 100644 --- a/modules/todo/keyboard.go +++ b/modules/todo/keyboard.go @@ -49,7 +49,7 @@ func (widget *Widget) displayPrev() { } func (widget *Widget) openFile() { - confDir, _ := cfg.ConfigDir() + confDir, _ := cfg.WtfConfigDir() wtf.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath)) } diff --git a/modules/todo/widget.go b/modules/todo/widget.go index e9498e40..638b2001 100644 --- a/modules/todo/widget.go +++ b/modules/todo/widget.go @@ -102,7 +102,7 @@ func (widget *Widget) init() { // Loads the todo list from Yaml file func (widget *Widget) load() { - confDir, _ := cfg.ConfigDir() + confDir, _ := cfg.WtfConfigDir() filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath) fileData, _ := wtf.ReadFileBytes(filePath) @@ -135,7 +135,7 @@ func (widget *Widget) newItem() { // persist writes the todo list to Yaml file func (widget *Widget) persist() { - confDir, _ := cfg.ConfigDir() + confDir, _ := cfg.WtfConfigDir() filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath) fileData, _ := yaml.Marshal(&widget.list)