Config file fixes

This commit is contained in:
Sasha Koshka 2022-11-26 22:36:16 -05:00
parent 46b2ca3d43
commit 639e43cfa7

View File

@ -118,7 +118,7 @@ type Config struct {
// <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment // <home>/.config/<name>/<name>.conf, unless the corresponding XDG environment
// variables are set - then it uses those. // variables are set - then it uses those.
func (config *Config) Load (name string) (err error) { func (config *Config) Load (name string) (err error) {
if !nameIsLegal(name) { if nameIsIllegal(name) {
err = ErrorIllegalName err = ErrorIllegalName
return return
} }
@ -126,6 +126,7 @@ func (config *Config) Load (name string) (err error) {
var configHome string var configHome string
configHome, err = getConfigHome() configHome, err = getConfigHome()
if err != nil { return } if err != nil { return }
println(configHome)
configDirsString := os.Getenv("XDG_CONFIG_DIRS") configDirsString := os.Getenv("XDG_CONFIG_DIRS")
if configDirsString == "" { if configDirsString == "" {
@ -240,7 +241,7 @@ func (config *Config) LoadFrom (reader io.Reader) (err error) {
// <home>/.config/<name>/<name>.conf unless $XDG_CONFIG_HOME has been set, in // <home>/.config/<name>/<name>.conf unless $XDG_CONFIG_HOME has been set, in
// which case the value of that variable is used instead. // which case the value of that variable is used instead.
func (config *Config) Save (name string) (err error) { func (config *Config) Save (name string) (err error) {
if !nameIsLegal(name) { if nameIsIllegal(name) {
err = ErrorIllegalName err = ErrorIllegalName
return return
} }
@ -280,13 +281,15 @@ func (config *Config) SaveTo (writer io.Writer) (err error) {
fmt.Fprintf(writer,"%s: #%x\n", key, colorInt) fmt.Fprintf(writer,"%s: #%x\n", key, colorInt)
case int: case int:
fmt.Fprintf(writer,"%s: %i\n", key, value.(int)) fmt.Fprintf(writer,"%s: %d\n", key, value.(int))
case float64: case float64:
fmt.Fprintf(writer,"%s: %f\n", key, value.(float64)) fmt.Fprintf(writer,"%s: %f\n", key, value.(float64))
case bool: case bool:
fmt.Fprintf(writer,"%s: %t\n", key, value.(bool)) fmt.Fprintf(writer,"%s: %t\n", key, value.(bool))
default:
fmt.Fprintf(writer,"# %s: unknown type\n", key)
} }
} }
@ -326,15 +329,15 @@ func getConfigHome () (home string, err error) {
homeDirectory, err = os.UserHomeDir() homeDirectory, err = os.UserHomeDir()
if err != nil { return } if err != nil { return }
configHome := os.Getenv("XDG_CONFIG_HOME") home = os.Getenv("XDG_CONFIG_HOME")
if configHome == "" { if home == "" {
configHome = filepath.Join(homeDirectory, "/.config/") home = filepath.Join(homeDirectory, "/.config/")
} }
return return
} }
func nameIsLegal (name string) (legal bool) { func nameIsIllegal (name string) (legal bool) {
legal = strings.ContainsAny(name, "/\\|:.%") legal = strings.ContainsAny(name, "/\\|:.%")
return return
} }