Added a way to save configuration files
This commit is contained in:
parent
3cfe8be7bb
commit
46b2ca3d43
104
config/config.go
104
config/config.go
@ -2,13 +2,13 @@ package config
|
|||||||
|
|
||||||
import "io"
|
import "io"
|
||||||
import "os"
|
import "os"
|
||||||
|
import "fmt"
|
||||||
import "bufio"
|
import "bufio"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
import "path/filepath"
|
import "path/filepath"
|
||||||
|
|
||||||
|
|
||||||
// when making changes to this file, look at
|
// when making changes to this file, look at
|
||||||
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
// 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
|
// <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 strings.ContainsAny(name, "/\\|:.%") {
|
if !nameIsLegal(name) {
|
||||||
err = ErrorIllegalName
|
err = ErrorIllegalName
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.LegalParameters == nil {
|
var configHome string
|
||||||
config.LegalParameters = make(map[string] Type)
|
configHome, err = getConfigHome()
|
||||||
}
|
|
||||||
|
|
||||||
if config.Parameters == nil {
|
|
||||||
config.Parameters = make(map[string] any)
|
|
||||||
}
|
|
||||||
|
|
||||||
var homeDirectory string
|
|
||||||
homeDirectory, err = os.UserHomeDir()
|
|
||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
|
|
||||||
configHome := os.Getenv("XDG_CONFIG_HOME")
|
|
||||||
if configHome == "" {
|
|
||||||
configHome = filepath.Join(homeDirectory, "/.config/")
|
|
||||||
}
|
|
||||||
|
|
||||||
configDirsString := os.Getenv("XDG_CONFIG_DIRS")
|
configDirsString := os.Getenv("XDG_CONFIG_DIRS")
|
||||||
if configDirsString == "" {
|
if configDirsString == "" {
|
||||||
configDirsString = "/etc/xdg/"
|
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
|
// Before they are processed, leading and trailing whitespace is trimmed from
|
||||||
// the key and the value. Keys are case sensitive.
|
// the key and the value. Keys are case sensitive.
|
||||||
func (config *Config) LoadFrom (reader io.Reader) (err error) {
|
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)
|
scanner := bufio.NewScanner(reader)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
@ -241,6 +236,63 @@ func (config *Config) LoadFrom (reader io.Reader) (err error) {
|
|||||||
return
|
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) {
|
func parseColor (value string) (valueColor color.Color, err error) {
|
||||||
if value[0] == '#' {
|
if value[0] == '#' {
|
||||||
if len(value) != 7 {
|
if len(value) != 7 {
|
||||||
@ -268,3 +320,21 @@ func parseColor (value string) (valueColor color.Color, err error) {
|
|||||||
|
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user