mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
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.
This commit is contained in:
parent
d76f6807cc
commit
08c7e768c0
@ -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),
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
73
cfg/position.go
Normal file
73
cfg/position.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user