Skip to content

Commit f747383

Browse files
author
lleadbet
committed
fixing per feedback
1 parent ada48aa commit f747383

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

internal/util/path.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
package util
44

55
import (
6+
"errors"
7+
"fmt"
68
"os"
79
"path/filepath"
10+
"runtime"
811

912
"github.com/mitchellh/go-homedir"
1013
)
@@ -18,26 +21,39 @@ func GetApplicationDir() (string, error) {
1821
if err != nil {
1922
return "", err
2023
}
21-
22-
legacyFolder := false
23-
2424
// check if the home/.twitch-cli folder exists; if so, use that as the path
2525
if _, err := os.Stat(filepath.Join(home, ".twitch-cli")); !os.IsNotExist(err) {
26-
legacyFolder = true
26+
return filepath.Join(home, ".twitch-cli"), nil
2727
}
2828

29-
path := filepath.Join(home, legacySubFolder)
29+
path := ""
3030

31-
if !legacyFolder {
32-
path = filepath.Join(home, ".config", subFolder) // issue #33- putting into a subfolder to avoid clutter
33-
subpath := filepath.Join(home, ".config")
34-
if _, err := os.Stat(subpath); os.IsNotExist(err) {
35-
os.Mkdir(subpath, 0700)
31+
xdg, exists := os.LookupEnv("XDG_CONFIG_HOME") // per comment in PR #71- using this env var if present
32+
if !exists || xdg == "" {
33+
// if not present, set sane defaults- APPDATA\twitch-cli for Windows, .config/twitch-cli for OSX/Linux
34+
if runtime.GOOS == "WINDOWS" {
35+
path = filepath.Join("$APPDATA", subFolder)
36+
} else {
37+
path = filepath.Join(home, ".config", subFolder)
3638
}
39+
} else {
40+
// if it does exist, then just use it and combine with the subfolder; example is: $HOME/.config/twitch-cli
41+
path = filepath.Join(xdg, subFolder)
3742
}
3843

44+
// if the full path doesn't exist, make all the folders to get there
3945
if _, err := os.Stat(path); os.IsNotExist(err) {
40-
os.Mkdir(path, 0700)
46+
err := os.MkdirAll(path, os.ModePerm)
47+
if err != nil {
48+
return "", err
49+
}
50+
}
51+
println(path)
52+
53+
// if the user ends up in this state, provide some basic diagnostic info
54+
if path == "" {
55+
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())
56+
return "", errors.New(triageMessage)
4157
}
4258

4359
return path, nil

0 commit comments

Comments
 (0)