diff --git a/go.mod b/go.mod index 8783f760..985dc42a 100755 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ require ( github.com/manifoldco/promptui v0.8.0 github.com/mattn/go-isatty v0.0.13 // indirect github.com/mattn/go-sqlite3 v1.14.7 - github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.4.1 // indirect github.com/pelletier/go-toml v1.9.2 // indirect github.com/spf13/afero v1.6.0 // indirect diff --git a/internal/util/path.go b/internal/util/path.go index c94381b2..0ed804e7 100644 --- a/internal/util/path.go +++ b/internal/util/path.go @@ -3,25 +3,47 @@ package util import ( + "errors" + "fmt" "os" "path/filepath" - - "github.com/mitchellh/go-homedir" + "runtime" ) -var subFolder = ".twitch-cli" +var legacySubFolder = ".twitch-cli" +var subFolder = "twitch-cli" // GetApplicationDir returns a string representation of the home path for use with configuration/data storage needs func GetApplicationDir() (string, error) { - home, err := homedir.Dir() + home, err := os.UserHomeDir() if err != nil { return "", err } - path := filepath.Join(home, subFolder) + // check if the home/.twitch-cli folder exists; if so, use that as the path + if _, err := os.Stat(filepath.Join(home, ".twitch-cli")); !os.IsNotExist(err) { + return filepath.Join(home, ".twitch-cli"), nil + } + + // handles the XDG_CONFIG_HOME var as well as using AppData + configPath, err := os.UserConfigDir() + if err != nil { + return "", err + } + path := filepath.Join(configPath, subFolder) + // if the full path doesn't exist, make all the folders to get there if _, err := os.Stat(path); os.IsNotExist(err) { - os.Mkdir(path, 0700) + err := os.MkdirAll(path, os.ModePerm) + if err != nil { + return "", err + } + } + + // if the user ends up in this state, provide some basic diagnostic info + if path == "" { + triageMessage := fmt.Sprintf("Invalid path generated; Please file a bugreport here: https://github.com/twitchdev/twitch-cli/issues/new\nInclude this in the report:\n-----\nOS: %v\nArchitecture: %v\nVersion: %v\n-----", runtime.GOOS, runtime.GOARCH, GetVersion()) + return "", errors.New(triageMessage) } return path, nil diff --git a/internal/util/path_test.go b/internal/util/path_test.go index 9c786904..c6aceceb 100644 --- a/internal/util/path_test.go +++ b/internal/util/path_test.go @@ -14,7 +14,7 @@ func TestGetApplicationDir(t *testing.T) { dir, err := GetApplicationDir() a.Nil(err, "GetApplicationDir() failed with error %v", err) - a.Equal(true, strings.HasSuffix(dir, ".twitch-cli"), "GetApplicationDir() expected to end with %v, got %v", ".twitch-cli", dir) + a.Equal(true, strings.HasSuffix(dir, "twitch-cli"), "GetApplicationDir() expected to end with %v, got %v", ".twitch-cli", dir) } func TestGetConfigPath(t *testing.T) {