2022-11-26 18:28:32 -07:00
|
|
|
package config
|
|
|
|
|
|
|
|
import "os"
|
|
|
|
import "bufio"
|
|
|
|
import "errors"
|
|
|
|
import "strings"
|
|
|
|
import "strconv"
|
|
|
|
import "image/color"
|
|
|
|
import "path/filepath"
|
|
|
|
|
2022-11-26 18:52:30 -07:00
|
|
|
|
|
|
|
// when making changes to this file, look at
|
|
|
|
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
|
|
|
|
// Error represents an error that can be returned by functions or methods in
|
|
|
|
// this module.
|
|
|
|
type Error int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ErrorIllegalName is thrown when an application name contains illegal
|
|
|
|
// characters such as a slash.
|
|
|
|
ErrorIllegalName Error = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
func (err Error) Error () (description string) {
|
|
|
|
switch err {
|
|
|
|
case ErrorIllegalName:
|
|
|
|
description = "name contains illegal characters"
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-26 18:28:32 -07:00
|
|
|
// Type represents the data type of a configuration parameter.
|
|
|
|
type Type int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// string
|
|
|
|
TypeString Type = iota
|
|
|
|
|
|
|
|
// image/color.RGBA
|
|
|
|
TypeColor
|
|
|
|
|
|
|
|
// int
|
|
|
|
TypeInteger
|
|
|
|
|
|
|
|
// float64
|
|
|
|
TypeFloat
|
|
|
|
|
|
|
|
// bool
|
|
|
|
TypeBoolean
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds a list of configuration parameters.
|
|
|
|
type Config struct {
|
|
|
|
// LegalParameters holds the names and types of all parameters that can
|
|
|
|
// be parsed. If the parser runs into a parameter that is not listed
|
|
|
|
// here, it will print out an error message and keep on parsing.
|
|
|
|
LegalParameters map[string] Type
|
|
|
|
|
|
|
|
// Parameters holds the names and values of all parsed parameters. If a
|
|
|
|
// value is non-nil, it can be safely type asserted into whatever type
|
|
|
|
// was requested.
|
|
|
|
Parameters map[string] any
|
|
|
|
}
|
|
|
|
|
2022-11-26 18:52:30 -07:00
|
|
|
// Load loads and parses the files /etc/xdg/<name>/<name>.conf and
|
|
|
|
// <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment
|
|
|
|
// variables are set - then it uses those.
|
2022-11-26 18:28:32 -07:00
|
|
|
func (config *Config) Load (name string) (err error) {
|
2022-11-26 18:52:30 -07:00
|
|
|
if strings.ContainsAny(name, "/\\|:.%") {
|
|
|
|
err = ErrorIllegalName
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-26 18:28:32 -07:00
|
|
|
if config.LegalParameters == nil {
|
|
|
|
config.LegalParameters = make(map[string] Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Parameters == nil {
|
|
|
|
config.Parameters = make(map[string] any)
|
|
|
|
}
|
2022-11-26 18:52:30 -07:00
|
|
|
|
2022-11-26 18:28:32 -07:00
|
|
|
var homeDirectory string
|
|
|
|
homeDirectory, err = os.UserHomeDir()
|
|
|
|
if err != nil { return }
|
2022-11-26 18:52:30 -07:00
|
|
|
|
|
|
|
configHome := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
if configHome == "" {
|
|
|
|
configHome = filepath.Join(homeDirectory, "/.config/")
|
|
|
|
}
|
|
|
|
|
|
|
|
configDirsString := os.Getenv("XDG_CONFIG_DIRS")
|
|
|
|
if configDirsString == "" {
|
|
|
|
configDirsString = "/etc/xdg/"
|
|
|
|
}
|
|
|
|
|
|
|
|
configDirs := strings.Split(configDirsString, ":")
|
|
|
|
configDirs = append(configDirs, configHome)
|
|
|
|
|
|
|
|
for _, directory := range configDirs {
|
|
|
|
config.loadFile(filepath.Join(directory, name, name + ".conf"))
|
|
|
|
}
|
|
|
|
|
2022-11-26 18:28:32 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *Config) loadFile (path string) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value, found := strings.Cut(scanner.Text(), ":")
|
|
|
|
if !found {
|
|
|
|
println (
|
|
|
|
"config: error in file", path +
|
|
|
|
": key-value separator missing")
|
|
|
|
println(scanner.Text())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key = strings.TrimSpace(key)
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
|
|
|
what, isKnown := config.LegalParameters[key]
|
|
|
|
if !isKnown {
|
|
|
|
println (
|
|
|
|
"config: error in file", path +
|
|
|
|
": unknown parameter")
|
|
|
|
println(scanner.Text())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch what {
|
|
|
|
case TypeString:
|
|
|
|
config.Parameters[key] = value
|
|
|
|
|
|
|
|
case TypeColor:
|
|
|
|
var valueColor color.Color
|
|
|
|
valueColor, err = parseColor(value)
|
|
|
|
if err != nil {
|
|
|
|
println (
|
|
|
|
"config: error in file", path +
|
|
|
|
":", err)
|
|
|
|
println(scanner.Text())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
config.Parameters[key] = valueColor
|
|
|
|
|
|
|
|
case TypeInteger:
|
|
|
|
var valueInt int
|
|
|
|
valueInt, err = strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
println (
|
|
|
|
"config: error in file", path +
|
|
|
|
": malformed integer literal")
|
|
|
|
println(scanner.Text())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
config.Parameters[key] = valueInt
|
|
|
|
|
|
|
|
case TypeFloat:
|
|
|
|
var valueFloat float64
|
|
|
|
valueFloat, err = strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
println (
|
|
|
|
"config: error in file", path +
|
|
|
|
": malformed float literal")
|
|
|
|
println(scanner.Text())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
config.Parameters[key] = valueFloat
|
|
|
|
|
|
|
|
case TypeBoolean:
|
|
|
|
value = strings.ToLower(value)
|
|
|
|
truthy :=
|
|
|
|
value == "true" ||
|
|
|
|
value == "yes" ||
|
|
|
|
value == "on" ||
|
|
|
|
value == "1"
|
|
|
|
config.Parameters[key] = truthy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseColor (value string) (valueColor color.Color, err error) {
|
|
|
|
if value[0] == '#' {
|
|
|
|
if len(value) != 7 {
|
|
|
|
err = errors.New("wrong length color literal")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var colorInt uint64
|
|
|
|
colorInt, err = strconv.ParseUint(value[1:7], 16, 24)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("malformed color literal")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
valueColor = color.RGBA {
|
|
|
|
R: uint8(colorInt >> 16),
|
|
|
|
G: uint8(colorInt >> 8),
|
|
|
|
B: uint8(colorInt),
|
|
|
|
A: 0xFF,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errors.New("malformed color literal")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|