1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/cfg/position.go
Chris Cummer 08c7e768c0 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.
2019-07-05 21:45:59 -07:00

74 lines
1.5 KiB
Go

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