Skip to content

Commit 211cf41

Browse files
authored
Merge branch 'main' into feature/auth-improvements-#80-#82
2 parents c80ac48 + f71e161 commit 211cf41

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/manifoldco/promptui v0.8.0
1414
github.com/mattn/go-isatty v0.0.13 // indirect
1515
github.com/mattn/go-sqlite3 v1.14.7
16-
github.com/mitchellh/go-homedir v1.1.0
1716
github.com/mitchellh/mapstructure v1.4.1 // indirect
1817
github.com/pelletier/go-toml v1.9.2 // indirect
1918
github.com/spf13/afero v1.6.0 // indirect

internal/util/path.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,47 @@
33
package util
44

55
import (
6+
"errors"
7+
"fmt"
68
"os"
79
"path/filepath"
8-
9-
"github.com/mitchellh/go-homedir"
10+
"runtime"
1011
)
1112

12-
var subFolder = ".twitch-cli"
13+
var legacySubFolder = ".twitch-cli"
14+
var subFolder = "twitch-cli"
1315

1416
// GetApplicationDir returns a string representation of the home path for use with configuration/data storage needs
1517
func GetApplicationDir() (string, error) {
16-
home, err := homedir.Dir()
18+
home, err := os.UserHomeDir()
1719
if err != nil {
1820
return "", err
1921
}
2022

21-
path := filepath.Join(home, subFolder)
23+
// check if the home/.twitch-cli folder exists; if so, use that as the path
24+
if _, err := os.Stat(filepath.Join(home, ".twitch-cli")); !os.IsNotExist(err) {
25+
return filepath.Join(home, ".twitch-cli"), nil
26+
}
27+
28+
// handles the XDG_CONFIG_HOME var as well as using AppData
29+
configPath, err := os.UserConfigDir()
30+
if err != nil {
31+
return "", err
32+
}
33+
path := filepath.Join(configPath, subFolder)
2234

35+
// if the full path doesn't exist, make all the folders to get there
2336
if _, err := os.Stat(path); os.IsNotExist(err) {
24-
os.Mkdir(path, 0700)
37+
err := os.MkdirAll(path, os.ModePerm)
38+
if err != nil {
39+
return "", err
40+
}
41+
}
42+
43+
// if the user ends up in this state, provide some basic diagnostic info
44+
if path == "" {
45+
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())
46+
return "", errors.New(triageMessage)
2547
}
2648

2749
return path, nil

internal/util/path_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestGetApplicationDir(t *testing.T) {
1414

1515
dir, err := GetApplicationDir()
1616
a.Nil(err, "GetApplicationDir() failed with error %v", err)
17-
a.Equal(true, strings.HasSuffix(dir, ".twitch-cli"), "GetApplicationDir() expected to end with %v, got %v", ".twitch-cli", dir)
17+
a.Equal(true, strings.HasSuffix(dir, "twitch-cli"), "GetApplicationDir() expected to end with %v, got %v", ".twitch-cli", dir)
1818
}
1919

2020
func TestGetConfigPath(t *testing.T) {

0 commit comments

Comments
 (0)