1
0
mirror of https://github.com/taigrr/wtf synced 2026-03-29 07:35:16 -07:00

Closes #217. Use XDG-compatible config directory

This change is largely experimental and it's entirely possible it could
wipe out your existing configuration. Be warned.

Old config path was: ~/.wtf/
New config path is:  ~/.config/wtf/

If an existing config directory already exists, this change attempts to
copy it to the new location.

Note that if your config file contains paths to files in the old config
directory, they won't work. You'll need to change them by hand.
This commit is contained in:
Chris Cummer
2018-06-20 16:46:02 -07:00
parent d9d351497f
commit 0e141e03c8
5 changed files with 122 additions and 4 deletions

View File

@@ -6,11 +6,53 @@ import (
"os"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/logger"
"github.com/senorprogrammer/wtf/wtf"
)
const CONFIG_DIR_V1 = "~/.wtf/"
const CONFIG_DIR_V2 = "~/.config/wtf/"
/* -------------------- Config Migration -------------------- */
// MigrateOldConfig copies any existing configuration from the old location
// to the new, XDG-compatible location
func MigrateOldConfig() {
srcDir, _ := wtf.ExpandHomeDir(CONFIG_DIR_V1)
destDir, _ := wtf.ExpandHomeDir(CONFIG_DIR_V2)
// If the old config directory doesn't exist, do not move
if _, err := os.Stat(srcDir); os.IsNotExist(err) {
return
}
// If the new config directory already exists, do not move
if _, err := os.Stat(destDir); err == nil {
return
}
// Time to move
err := Copy(srcDir, destDir)
if err != nil {
panic(err)
} else {
logger.Log(fmt.Sprintf("Copied old config from %s to %s", srcDir, destDir))
}
// Delete the old directory if the new one exists
if _, err := os.Stat(destDir); err == nil {
err := os.RemoveAll(srcDir)
if err != nil {
logger.Log(err.Error())
}
}
}
/* -------------------- Config Migration -------------------- */
// ConfigDir returns the absolute path to the configuration directory
func ConfigDir() (string, error) {
configDir, err := wtf.ExpandHomeDir("~/.wtf/")
configDir, err := wtf.ExpandHomeDir(CONFIG_DIR_V2)
if err != nil {
return "", err
}
@@ -18,7 +60,7 @@ func ConfigDir() (string, error) {
return configDir, nil
}
// CreateConfigDir creates the .wtf directory in the user's home dir
// CreateConfigDir creates the wtf/ directory in the user's home dir
func CreateConfigDir() {
configDir, _ := ConfigDir()