Bladeren bron

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

Jguer 7 jaren geleden
bovenliggende
commit
93de8ba9b3
4 gewijzigde bestanden met toevoegingen van 16 en 4 verwijderingen
  1. 1 0
      .gitignore
  2. 15 2
      cmd.go
  3. 0 1
      config.go
  4. 0 1
      print.go

+ 1 - 0
.gitignore

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

+ 15 - 2
cmd.go

@@ -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 {

+ 0 - 1
config.go

@@ -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 {

+ 0 - 1
print.go

@@ -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