Added a way to save configuration files

This commit is contained in:
Sasha Koshka 2022-11-26 22:10:22 -05:00
parent 3cfe8be7bb
commit 46b2ca3d43
1 changed files with 87 additions and 17 deletions

View File

@ -2,13 +2,13 @@ package config
import "io"
import "os"
import "fmt"
import "bufio"
import "strings"
import "strconv"
import "image/color"
import "path/filepath"
// when making changes to this file, look at
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
@ -118,28 +118,15 @@ type Config struct {
// <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) {
if strings.ContainsAny(name, "/\\|:.%") {
if !nameIsLegal(name) {
err = ErrorIllegalName
return
}
if config.LegalParameters == nil {
config.LegalParameters = make(map[string] Type)
}
if config.Parameters == nil {
config.Parameters = make(map[string] any)
}
var homeDirectory string
homeDirectory, err = os.UserHomeDir()
var configHome string
configHome, err = getConfigHome()
if err != nil { return }
configHome := os.Getenv("XDG_CONFIG_HOME")
if configHome == "" {
configHome = filepath.Join(homeDirectory, "/.config/")
}
configDirsString := os.Getenv("XDG_CONFIG_DIRS")
if configDirsString == "" {
configDirsString = "/etc/xdg/"
@ -173,6 +160,14 @@ func (config *Config) Load (name string) (err error) {
// Before they are processed, leading and trailing whitespace is trimmed from
// the key and the value. Keys are case sensitive.
func (config *Config) LoadFrom (reader io.Reader) (err error) {
if config.LegalParameters == nil {
config.LegalParameters = make(map[string] Type)
}
if config.Parameters == nil {
config.Parameters = make(map[string] any)
}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
@ -241,6 +236,63 @@ func (config *Config) LoadFrom (reader io.Reader) (err error) {
return
}
// Save overwrites the main user configuration file, which is located at
// <home>/.config/<name>/<name>.conf unless $XDG_CONFIG_HOME has been set, in
// which case the value of that variable is used instead.
func (config *Config) Save (name string) (err error) {
if !nameIsLegal(name) {
err = ErrorIllegalName
return
}
var configHome string
configHome, err = getConfigHome()
if err != nil { return }
err = os.MkdirAll(configHome, 0755)
if err != nil { return }
file, err := os.OpenFile (
filepath.Join(configHome, name, name + ".conf"),
os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0744)
if err != nil { return }
defer file.Close()
err = config.SaveTo(file)
if err != nil { return }
return
}
// SaveTo writes the configuration data to the specified io.Writer.
func (config *Config) SaveTo (writer io.Writer) (err error) {
for key, value := range config.Parameters {
switch value.(type) {
case string:
fmt.Fprintf(writer,"%s: %s\n", key, value.(string))
case color.RGBA:
colorValue := value.(color.RGBA)
colorInt := uint64 (
colorValue.R << 16 |
colorValue.G << 8 |
colorValue.B)
fmt.Fprintf(writer,"%s: #%x\n", key, colorInt)
case int:
fmt.Fprintf(writer,"%s: %i\n", key, value.(int))
case float64:
fmt.Fprintf(writer,"%s: %f\n", key, value.(float64))
case bool:
fmt.Fprintf(writer,"%s: %t\n", key, value.(bool))
}
}
return
}
func parseColor (value string) (valueColor color.Color, err error) {
if value[0] == '#' {
if len(value) != 7 {
@ -268,3 +320,21 @@ func parseColor (value string) (valueColor color.Color, err error) {
return
}
func getConfigHome () (home string, err error) {
var homeDirectory string
homeDirectory, err = os.UserHomeDir()
if err != nil { return }
configHome := os.Getenv("XDG_CONFIG_HOME")
if configHome == "" {
configHome = filepath.Join(homeDirectory, "/.config/")
}
return
}
func nameIsLegal (name string) (legal bool) {
legal = strings.ContainsAny(name, "/\\|:.%")
return
}