migrate config files

This commit is contained in:
piotr
2021-09-25 03:44:38 +02:00
parent 62b7b4f90e
commit fb00364e51
3 changed files with 39 additions and 5 deletions

Binary file not shown.

27
main.go
View File

@@ -208,6 +208,29 @@ func main() {
// ENVIRONMENT // ENVIRONMENT
configDirectory = configDir() configDirectory = configDir()
// Placing the drawer config files in the nwg-panel config directory was a mistake.
// Let's move them to their own location.
oldConfigDirectory, err := oldConfigDir()
if err == nil {
for _, p := range []string{"drawer.css", "preferred-apps.json"} {
if pathExists(path.Join(oldConfigDirectory, p)) {
log.Infof("File %s found in stale location, moving to %s", p, configDirectory)
if !pathExists(path.Join(configDirectory, p)) {
err = os.Rename(path.Join(oldConfigDirectory, p), path.Join(configDirectory, p))
if err == nil {
log.Info("Success")
} else {
log.Warn(err)
}
} else {
log.Warnf("Failed moving %s to %s: path already exists!", path.Join(oldConfigDirectory, p), path.Join(configDirectory, p))
}
}
}
}
// Copy default style sheet if not found
if !pathExists(filepath.Join(configDirectory, "drawer.css")) { if !pathExists(filepath.Join(configDirectory, "drawer.css")) {
copyFile(filepath.Join(getDataHome(), "nwg-drawer/drawer.css"), filepath.Join(configDirectory, "drawer.css")) copyFile(filepath.Join(getDataHome(), "nwg-drawer/drawer.css"), filepath.Join(configDirectory, "drawer.css"))
} }
@@ -241,9 +264,9 @@ func main() {
paFile := filepath.Join(configDirectory, "preferred-apps.json") paFile := filepath.Join(configDirectory, "preferred-apps.json")
preferredApps, err = loadPreferredApps(paFile) preferredApps, err = loadPreferredApps(paFile)
if err != nil { if err != nil {
log.Error(fmt.Sprintf("Custom associations file %s not found or invalid", paFile)) log.Errorf("Custom associations file %s not found or invalid", paFile)
} else { } else {
log.Info(fmt.Sprintf("Found %v associations in %s", len(preferredApps), paFile)) log.Infof("Found %v associations in %s", len(preferredApps), paFile)
} }
// USER INTERFACE // USER INTERFACE

View File

@@ -10,6 +10,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort" "sort"
@@ -149,14 +150,24 @@ func readTextFile(path string) (string, error) {
return string(bytes), nil return string(bytes), nil
} }
func oldConfigDir() (string, error) {
if os.Getenv("XDG_CONFIG_HOME") != "" {
dir := path.Join(os.Getenv("XDG_CONFIG_HOME"), "nwg-panel")
return dir, nil
}
return "", errors.New("old config dir not found")
}
func configDir() string { func configDir() string {
if os.Getenv("XDG_CONFIG_HOME") != "" { if os.Getenv("XDG_CONFIG_HOME") != "" {
dir := fmt.Sprintf("%s/nwg-panel", os.Getenv("XDG_CONFIG_HOME")) dir := path.Join(os.Getenv("XDG_CONFIG_HOME"), "nwg-drawer")
createDir(dir) createDir(dir)
return (fmt.Sprintf("%s/nwg-panel", os.Getenv("XDG_CONFIG_HOME"))) return dir
} }
dir := fmt.Sprintf("%s/.config/nwg-panel", os.Getenv("HOME")) dir := path.Join(os.Getenv("XDG_CONFIG_HOME"), "nwg-drawe")
createDir(dir) createDir(dir)
return dir return dir
} }