Use XDG directories, and respect corresponding environment vars

This commit is contained in:
Sasha Koshka 2022-11-26 20:52:30 -05:00
parent a42dd60a16
commit e60a990d10

View File

@ -8,6 +8,29 @@ import "strconv"
import "image/color" import "image/color"
import "path/filepath" import "path/filepath"
// 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
}
// Type represents the data type of a configuration parameter. // Type represents the data type of a configuration parameter.
type Type int type Type int
@ -41,9 +64,15 @@ type Config struct {
Parameters map[string] any Parameters map[string] any
} }
// Load loads and parses the files /etc/<name>/<name>.conf and // Load loads and parses the files /etc/xdg/<name>/<name>.conf and
// <home>/.config/<name>/<name>.conf. // <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment
// variables are set - then it uses those.
func (config *Config) Load (name string) (err error) { func (config *Config) Load (name string) (err error) {
if strings.ContainsAny(name, "/\\|:.%") {
err = ErrorIllegalName
return
}
if config.LegalParameters == nil { if config.LegalParameters == nil {
config.LegalParameters = make(map[string] Type) config.LegalParameters = make(map[string] Type)
} }