From 08c7e768c0935b877ed59dfcf8e273869d464177 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 4 Jul 2019 21:17:39 -0700 Subject: [PATCH] WTF-482 Sanity-check position configuration data for modules If a module is missing any of the positional data it now informs the user and exits gracefully with an error. --- cfg/common_settings.go | 21 +++++------- cfg/config_files.go | 1 + cfg/position.go | 73 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 cfg/position.go diff --git a/cfg/common_settings.go b/cfg/common_settings.go index 12a58026..ce1be396 100644 --- a/cfg/common_settings.go +++ b/cfg/common_settings.go @@ -30,12 +30,12 @@ type Module struct { Type string } -type Position struct { - Height int - Left int - Top int - Width int -} +// type Position struct { +// Height int +// Left int +// Top int +// Width int +// } type Sigils struct { Checkbox struct { @@ -64,7 +64,7 @@ type Common struct { func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config.Config, globalSettings *config.Config) *Common { colorsConfig, _ := globalSettings.Get("wtf.colors") - positionPath := "position" + // positionPath := "position" sigilsPath := "wtf.sigils" common := Common{ @@ -86,12 +86,7 @@ func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config Type: moduleConfig.UString("type", name), }, - Position: Position{ - Height: moduleConfig.UInt(positionPath + ".height"), - Left: moduleConfig.UInt(positionPath + ".left"), - Top: moduleConfig.UInt(positionPath + ".top"), - Width: moduleConfig.UInt(positionPath + ".width"), - }, + Position: NewPositionFromYAML(name, moduleConfig), Enabled: moduleConfig.UBool("enabled", false), RefreshInterval: moduleConfig.UInt("refreshInterval", 300), diff --git a/cfg/config_files.go b/cfg/config_files.go index 19f46ecf..fb5a0b6d 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -132,6 +132,7 @@ func LoadConfigFile(filePath string) *config.Config { 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()) + os.Exit(1) } diff --git a/cfg/position.go b/cfg/position.go new file mode 100644 index 00000000..7bf65fad --- /dev/null +++ b/cfg/position.go @@ -0,0 +1,73 @@ +package cfg + +import ( + "fmt" + "os" + "strings" + + "github.com/olebedev/config" +) + +const ( + positionPath = "position" +) + +// Position represents the onscreen location of a widget +type Position struct { + Height int + Left int + Top int + Width int +} + +// NewPositionFromYAML creates and returns a new instance of Position +func NewPositionFromYAML(name string, moduleConfig *config.Config) Position { + errs := make(map[string]error) + + // Parse the positional data from the config data + top, err := moduleConfig.Int(positionPath + ".top") + errs["top"] = err + + left, err := moduleConfig.Int(positionPath + ".left") + errs["left"] = err + + width, err := moduleConfig.Int(positionPath + ".width") + errs["width"] = err + + height, err := moduleConfig.Int(positionPath + ".height") + errs["height"] = err + + validatePositions(name, errs) + + pos := Position{ + Top: top, + Left: left, + Width: width, + Height: height, + } + + return pos +} + +/* -------------------- Unexported Functions -------------------- */ + +// If any of the position values have an error then we inform the user and exit the app +func validatePositions(name string, errs map[string]error) { + var errStr string + + for pos, err := range errs { + if err != nil { + errStr += fmt.Sprintf(" - Invalid value for %s\n", pos) + } + } + + if errStr != "" { + fmt.Println() + fmt.Printf("\033[0;31mErrors in %s configuration\033[0m\n", strings.Title(name)) + fmt.Println(errStr) + fmt.Println("Please check your config.yml file.") + fmt.Println() + + os.Exit(1) + } +}