From 4eeb6d67a94edc9ea222219b37edd54b37ef756b Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Wed, 8 Jan 2020 23:34:24 -0500 Subject: [PATCH] Support XDG_CONFIG_HOME Clean up workflow and eliminate a method One place where we calculate the full dir Closes #699 --- cfg/config_files.go | 30 +++++++++++------------------- cfg/error_messages.go | 4 ++-- flags/flags.go | 5 +++-- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/cfg/config_files.go b/cfg/config_files.go index a357d619..077d1458 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -64,7 +64,6 @@ func Initialize(hasCustom bool) { // These always get created because this is where modules should write any permanent // data they need to persist between runs (i.e.: log, textfile, etc.) - createXdgConfigDir() createWtfConfigDir() if !hasCustom { @@ -75,7 +74,13 @@ func Initialize(hasCustom bool) { // WtfConfigDir returns the absolute path to the configuration directory func WtfConfigDir() (string, error) { - configDir, err := expandHomeDir(WtfConfigDirV2) + configDir := os.Getenv("XDG_CONFIG_HOME") + if configDir == "" { + configDir = WtfConfigDirV2 + } else { + configDir = configDir + "/wtf/" + } + configDir, err := expandHomeDir(configDir) if err != nil { return "", err } @@ -100,7 +105,8 @@ func LoadWtfConfigFile(filePath string) *config.Config { // chmodConfigFile sets the mode of the config file to r+w for the owner only func chmodConfigFile() { - relPath := fmt.Sprintf("%s%s", WtfConfigDirV2, WtfConfigFile) + configDir, _ := WtfConfigDir() + relPath := fmt.Sprintf("%s%s", configDir, WtfConfigFile) absPath, _ := expandHomeDir(relPath) _, err := os.Stat(absPath) @@ -114,27 +120,13 @@ func chmodConfigFile() { } } -// createXdgConfigDir creates the necessary base directory for storing the config file -// If ~/.config is missing, it will try to create it -func createXdgConfigDir() { - xdgConfigDir, _ := expandHomeDir(XdgConfigDir) - - if _, err := os.Stat(xdgConfigDir); os.IsNotExist(err) { - err := os.Mkdir(xdgConfigDir, os.ModePerm) - if err != nil { - displayXdgConfigDirCreateError(err) - os.Exit(1) - } - } -} - // createWtfConfigDir creates the necessary directories for storing the default config file // If ~/.config/wtf is missing, it will try to create it func createWtfConfigDir() { wtfConfigDir, _ := WtfConfigDir() if _, err := os.Stat(wtfConfigDir); os.IsNotExist(err) { - err := os.Mkdir(wtfConfigDir, os.ModePerm) + err := os.MkdirAll(wtfConfigDir, os.ModePerm) if err != nil { displayWtfConfigDirCreateError(err) os.Exit(1) @@ -204,7 +196,7 @@ func home() (string, error) { // to the new, XDG-compatible location func migrateOldConfig() { srcDir, _ := expandHomeDir(WtfConfigDirV1) - destDir, _ := expandHomeDir(WtfConfigDirV2) + destDir, _ := WtfConfigDir() // If the old config directory doesn't exist, do not move if _, err := os.Stat(srcDir); os.IsNotExist(err) { diff --git a/cfg/error_messages.go b/cfg/error_messages.go index 5db6426e..35ea88d0 100644 --- a/cfg/error_messages.go +++ b/cfg/error_messages.go @@ -30,8 +30,8 @@ func displayDefaultConfigWriteError(err error) { displayError(err) } -func displayXdgConfigDirCreateError(err error) { - fmt.Printf("\n%s Could not create the '%s' directory.\n", aurora.Red("ERROR"), aurora.Yellow(XdgConfigDir)) +func displayXdgConfigDirCreateError(path string, err error) { + fmt.Printf("\n%s Could not create the '%s' directory.\n", aurora.Red("ERROR"), aurora.Yellow(path)) fmt.Println() displayError(err) } diff --git a/flags/flags.go b/flags/flags.go index 35b1eb94..bc6fcbdb 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -7,6 +7,7 @@ import ( goFlags "github.com/jessevdk/go-flags" "github.com/olebedev/config" + "github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/help" ) @@ -79,10 +80,10 @@ func (flags *Flags) Parse() { } // If no config file is explicitly passed in as a param then set the flag to the default config file - homeDir, err := os.UserHomeDir() + configDir, err := cfg.WtfConfigDir() if err != nil { fmt.Printf("Error: %v\n", err) os.Exit(1) } - flags.Config = filepath.Join(homeDir, ".config", "wtf", "config.yml") + flags.Config = filepath.Join(configDir, "config.yml") }