Use XDG_CONFIG_HOME as default config directory instead of hardcoded when available. Fixes #50

This commit is contained in:
Jguer 2017-10-16 23:38:51 +09:00
parent 131e61af5d
commit 93de8ba9b3
4 changed files with 16 additions and 4 deletions

1
.gitignore vendored
View file

@ -20,3 +20,4 @@ _cgo_export.*
*.exe
*.test
*.prof
yay

17
cmd.go
View file

@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -42,10 +43,22 @@ func init() {
defaultSettings(&config)
var err error
configfile := os.Getenv("HOME") + "/.config/yay/config.json"
if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
if info, err := os.Stat(dir); err == nil && info.IsDir() == true {
configfile = os.Getenv("XDG_CONFIG_HOME") + "/yay/config.json"
} else {
configfile = os.Getenv("HOME") + "/.config/yay/config.json"
}
} else {
configfile = os.Getenv("HOME") + "/.config/yay/config.json"
}
if _, err = os.Stat(configfile); os.IsNotExist(err) {
_ = os.MkdirAll(os.Getenv("HOME")+"/.config/yay", 0755)
err = os.MkdirAll(filepath.Dir(configfile), 0755)
if err != nil {
fmt.Println("Unable to create config directory:", filepath.Dir(configfile), err)
os.Exit(2)
}
// Save the default config if nothing is found
config.saveConfig()
} else {

View file

@ -79,7 +79,6 @@ func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
// SaveConfig writes yay config to file.
func (config *Configuration) saveConfig() error {
config.NoConfirm = false
configfile := os.Getenv("HOME") + "/.config/yay/config.json"
marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {

View file

@ -163,7 +163,6 @@ func PrintInfo(a *rpc.Pkg) {
// BiggestPackages prints the name of the ten biggest packages in the system.
func biggestPackages() {
localDb, err := AlpmHandle.LocalDb()
if err != nil {
return