Merge pull request #96 from liamg/xdg-base-dir

Store config in $XDG_CONFIG_HOME if available
This commit is contained in:
Liam Galvin 2018-11-30 21:56:40 +00:00 committed by GitHub
commit f0d22e193a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/liamg/aminal/config"
"github.com/liamg/aminal/version"
@ -48,10 +49,16 @@ func loadConfigFile() *config.Config {
return &config.DefaultConfig
}
places := []string{
fmt.Sprintf("%s/.aminal.toml", home),
places := []string{}
xdgHome := os.Getenv("XDG_CONFIG_HOME")
if xdgHome != "" {
places = append(places, fmt.Sprintf("%s/aminal/config.toml", xdgHome))
}
places = append(places, fmt.Sprintf("%s/.config/aminal/config.toml", home))
places = append(places, fmt.Sprintf("%s/.aminal.toml", home))
for _, place := range places {
if b, err := ioutil.ReadFile(place); err == nil {
if c, err := config.Parse(b); err == nil {
@ -62,10 +69,18 @@ func loadConfigFile() *config.Config {
}
}
parts := strings.Split(places[0], string(os.PathSeparator))
path := strings.Join(parts[0:len(parts)-1], string(os.PathSeparator))
err := os.MkdirAll(path, 0744)
if err != nil {
panic(err)
}
if b, err := config.DefaultConfig.Encode(); err != nil {
fmt.Printf("Failed to encode config file: %s\n", err)
} else {
if err := ioutil.WriteFile(fmt.Sprintf("%s/.aminal.toml", home), b, 0644); err != nil {
if err := ioutil.WriteFile(fmt.Sprintf("%s/config.toml", path), b, 0644); err != nil {
fmt.Printf("Failed to encode config file: %s\n", err)
}
}