From bc37b7cd427dd3d12be086b79813e25b78e4bb6f Mon Sep 17 00:00:00 2001 From: UnaTried <103455203+UnaTried@users.noreply.github.com> Date: Mon, 3 Feb 2025 20:19:47 +0100 Subject: [PATCH] refactor: change folder location for `Backup` and `Extracted` (#3282) --- src/cmd/cmd.go | 4 ++-- src/utils/path-utils.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/cmd/cmd.go b/src/cmd/cmd.go index 377d6a858d..4f5e95febb 100644 --- a/src/cmd/cmd.go +++ b/src/cmd/cmd.go @@ -15,7 +15,7 @@ import ( var ( spicetifyFolder = utils.GetSpicetifyFolder() rawFolder, themedFolder = getExtractFolder() - backupFolder = utils.GetUserFolder("Backup") + backupFolder = utils.GetStateFolder("Backup") userThemesFolder = utils.GetUserFolder("Themes") quiet bool isAppX = false @@ -205,7 +205,7 @@ func GetSpotifyPath() string { } func getExtractFolder() (string, string) { - dir := utils.GetUserFolder("Extracted") + dir := utils.GetStateFolder("Extracted") raw := filepath.Join(dir, "Raw") utils.CheckExistAndCreate(raw) diff --git a/src/utils/path-utils.go b/src/utils/path-utils.go index 2105396367..f070d6c00e 100644 --- a/src/utils/path-utils.go +++ b/src/utils/path-utils.go @@ -65,6 +65,36 @@ func GetSpicetifyFolder() string { return result } +func GetStateFolder(name string) string { + result, isAvailable := os.LookupEnv("SPICETIFY_STATE") + defer func() { CheckExistAndCreate(result) }() + + if isAvailable && len(result) > 0 { + return result + } + + if runtime.GOOS == "windows" { + parent := os.Getenv("APPDATA") + + result = filepath.Join(parent, "spicetify") + } else if runtime.GOOS == "linux" { + parent, isAvailable := os.LookupEnv("XDG_STATE_HOME") + + if !isAvailable || len(parent) == 0 { + parent = filepath.Join(os.Getenv("HOME"), ".local", "state") + CheckExistAndCreate(parent) + } + + result = filepath.Join(parent, "spicetify") + } else if runtime.GOOS == "darwin" { + parent := filepath.Join(os.Getenv("HOME"), ".local", "state") + CheckExistAndCreate(parent) + + result = filepath.Join(parent, "spicetify") + } + return result +} + // getUserFolder checks if folder `name` is available in spicetifyFolder, // else creates then returns the path. func GetUserFolder(name string) string {