1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Display a useful error message for custom configuration files

This commit is contained in:
Chris Cummer 2019-07-20 13:04:13 -07:00
parent ab94bd34ac
commit 5e18538f14
3 changed files with 26 additions and 9 deletions

View File

@ -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 {
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.

View File

@ -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)

View File

@ -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)