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:
		
							parent
							
								
									ab94bd34ac
								
							
						
					
					
						commit
						5e18538f14
					
				| @ -140,12 +140,17 @@ func CreateFile(fileName string) (string, error) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // LoadWtfConfigFile loads the config.yml file to configure the app | // 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) | 	absPath, _ := expandHomeDir(filePath) | ||||||
| 
 | 
 | ||||||
| 	cfg, err := config.ParseYamlFile(absPath) | 	cfg, err := config.ParseYamlFile(absPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		displayWtfConfigFileLoadError(err) | 		if isCustomConfig { | ||||||
|  | 			displayWtfCustomConfigFileLoadError(err) | ||||||
|  | 		} else { | ||||||
|  | 			displayWtfConfigFileLoadError(err) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -242,6 +247,17 @@ func displayWtfConfigFileLoadError(err error) { | |||||||
| 	fmt.Printf("Error: \033[0;31m%s\033[0m\n\n", 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 | // Expand expands the path to include the home directory if the path | ||||||
| // is prefixed with `~`. If it isn't prefixed with `~`, the path is | // is prefixed with `~`. If it isn't prefixed with `~`, the path is | ||||||
| // returned as-is. | // returned as-is. | ||||||
|  | |||||||
| @ -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 | // HasCustomConfig returns TRUE if a config path was passed in, FALSE if one was not | ||||||
| func (flags *Flags) HasConfig() bool { | func (flags *Flags) HasCustomConfig() bool { | ||||||
| 	return len(flags.Config) > 0 | 	return len(flags.Config) > 0 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -72,7 +72,7 @@ func (flags *Flags) Parse() { | |||||||
| 
 | 
 | ||||||
| 	// If no config file is explicitly passed in as a param, | 	// If no config file is explicitly passed in as a param, | ||||||
| 	// set the flag to the default config file | 	// set the flag to the default config file | ||||||
| 	if !flags.HasConfig() { | 	if !flags.HasCustomConfig() { | ||||||
| 		homeDir, err := utils.Home() | 		homeDir, err := utils.Home() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("Error: %v\n", err) | 			fmt.Printf("Error: %v\n", err) | ||||||
|  | |||||||
							
								
								
									
										9
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								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() | 	watch := watcher.New() | ||||||
| 	absPath, _ := utils.ExpandHomeDir(configFilePath) | 	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 | 				// Disable all widgets to stop scheduler goroutines and remove widgets from memory | ||||||
| 				disableAllWidgets(runningWidgets) | 				disableAllWidgets(runningWidgets) | ||||||
| 
 | 
 | ||||||
| 				config := cfg.LoadWtfConfigFile(absPath) | 				config := cfg.LoadWtfConfigFile(absPath, false) | ||||||
| 
 | 
 | ||||||
| 				widgets := maker.MakeWidgets(app, pages, config) | 				widgets := maker.MakeWidgets(app, pages, config) | ||||||
| 				runningWidgets = widgets | 				runningWidgets = widgets | ||||||
| @ -128,6 +128,7 @@ func watchForConfigChanges(app *tview.Application, configFilePath string, grid * | |||||||
| func main() { | func main() { | ||||||
| 	log.SetFlags(log.LstdFlags | log.Lshortfile) | 	log.SetFlags(log.LstdFlags | log.Lshortfile) | ||||||
| 
 | 
 | ||||||
|  | 	// Manage the configuration directories and file | ||||||
| 	cfg.MigrateOldConfig() | 	cfg.MigrateOldConfig() | ||||||
| 	cfg.CreateXdgConfigDir() | 	cfg.CreateXdgConfigDir() | ||||||
| 	cfg.CreateWtfConfigDir() | 	cfg.CreateWtfConfigDir() | ||||||
| @ -136,7 +137,7 @@ func main() { | |||||||
| 	// Parse and handle flags | 	// Parse and handle flags | ||||||
| 	flags := flags.NewFlags() | 	flags := flags.NewFlags() | ||||||
| 	flags.Parse() | 	flags.Parse() | ||||||
| 	config := cfg.LoadWtfConfigFile(flags.ConfigFilePath()) | 	config := cfg.LoadWtfConfigFile(flags.ConfigFilePath(), flags.HasCustomConfig()) | ||||||
| 	flags.RenderIf(version, config) | 	flags.RenderIf(version, config) | ||||||
| 
 | 
 | ||||||
| 	if flags.Profile { | 	if flags.Profile { | ||||||
| @ -165,7 +166,7 @@ func main() { | |||||||
| 
 | 
 | ||||||
| 	app.SetInputCapture(keyboardIntercept) | 	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 { | 	if err := app.SetRoot(pages, true).Run(); err != nil { | ||||||
| 		fmt.Printf("Error: %v\n", err) | 		fmt.Printf("Error: %v\n", err) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user