1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Merge branch 'xdg' of github.com:Seanstoppable/wtf into Seanstoppable-xdg

This commit is contained in:
Chris Cummer 2020-01-15 21:46:20 -08:00
commit d91a7f88c9
3 changed files with 16 additions and 23 deletions

View File

@ -64,7 +64,6 @@ func Initialize(hasCustom bool) {
// These always get created because this is where modules should write any permanent // 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.) // data they need to persist between runs (i.e.: log, textfile, etc.)
createXdgConfigDir()
createWtfConfigDir() createWtfConfigDir()
if !hasCustom { if !hasCustom {
@ -75,7 +74,13 @@ func Initialize(hasCustom bool) {
// WtfConfigDir returns the absolute path to the configuration directory // WtfConfigDir returns the absolute path to the configuration directory
func WtfConfigDir() (string, error) { 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 { if err != nil {
return "", err 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 // chmodConfigFile sets the mode of the config file to r+w for the owner only
func chmodConfigFile() { func chmodConfigFile() {
relPath := fmt.Sprintf("%s%s", WtfConfigDirV2, WtfConfigFile) configDir, _ := WtfConfigDir()
relPath := fmt.Sprintf("%s%s", configDir, WtfConfigFile)
absPath, _ := expandHomeDir(relPath) absPath, _ := expandHomeDir(relPath)
_, err := os.Stat(absPath) _, 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 // createWtfConfigDir creates the necessary directories for storing the default config file
// If ~/.config/wtf is missing, it will try to create it // If ~/.config/wtf is missing, it will try to create it
func createWtfConfigDir() { func createWtfConfigDir() {
wtfConfigDir, _ := WtfConfigDir() wtfConfigDir, _ := WtfConfigDir()
if _, err := os.Stat(wtfConfigDir); os.IsNotExist(err) { if _, err := os.Stat(wtfConfigDir); os.IsNotExist(err) {
err := os.Mkdir(wtfConfigDir, os.ModePerm) err := os.MkdirAll(wtfConfigDir, os.ModePerm)
if err != nil { if err != nil {
displayWtfConfigDirCreateError(err) displayWtfConfigDirCreateError(err)
os.Exit(1) os.Exit(1)
@ -204,7 +196,7 @@ func home() (string, error) {
// to the new, XDG-compatible location // to the new, XDG-compatible location
func migrateOldConfig() { func migrateOldConfig() {
srcDir, _ := expandHomeDir(WtfConfigDirV1) srcDir, _ := expandHomeDir(WtfConfigDirV1)
destDir, _ := expandHomeDir(WtfConfigDirV2) destDir, _ := WtfConfigDir()
// If the old config directory doesn't exist, do not move // If the old config directory doesn't exist, do not move
if _, err := os.Stat(srcDir); os.IsNotExist(err) { if _, err := os.Stat(srcDir); os.IsNotExist(err) {

View File

@ -30,8 +30,8 @@ func displayDefaultConfigWriteError(err error) {
displayError(err) displayError(err)
} }
func displayXdgConfigDirCreateError(err error) { func displayXdgConfigDirCreateError(path string, err error) {
fmt.Printf("\n%s Could not create the '%s' directory.\n", aurora.Red("ERROR"), aurora.Yellow(XdgConfigDir)) fmt.Printf("\n%s Could not create the '%s' directory.\n", aurora.Red("ERROR"), aurora.Yellow(path))
fmt.Println() fmt.Println()
displayError(err) displayError(err)
} }

View File

@ -7,6 +7,7 @@ import (
goFlags "github.com/jessevdk/go-flags" goFlags "github.com/jessevdk/go-flags"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/help" "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 // 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 { if err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
flags.Config = filepath.Join(homeDir, ".config", "wtf", "config.yml") flags.Config = filepath.Join(configDir, "config.yml")
} }