|
3 | 3 | package util
|
4 | 4 |
|
5 | 5 | import (
|
| 6 | + "errors" |
| 7 | + "fmt" |
6 | 8 | "os"
|
7 | 9 | "path/filepath"
|
8 |
| - |
9 |
| - "github.com/mitchellh/go-homedir" |
| 10 | + "runtime" |
10 | 11 | )
|
11 | 12 |
|
12 |
| -var subFolder = ".twitch-cli" |
| 13 | +var legacySubFolder = ".twitch-cli" |
| 14 | +var subFolder = "twitch-cli" |
13 | 15 |
|
14 | 16 | // GetApplicationDir returns a string representation of the home path for use with configuration/data storage needs
|
15 | 17 | func GetApplicationDir() (string, error) {
|
16 |
| - home, err := homedir.Dir() |
| 18 | + home, err := os.UserHomeDir() |
17 | 19 | if err != nil {
|
18 | 20 | return "", err
|
19 | 21 | }
|
20 | 22 |
|
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) |
22 | 34 |
|
| 35 | + // if the full path doesn't exist, make all the folders to get there |
23 | 36 | 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) |
25 | 47 | }
|
26 | 48 |
|
27 | 49 | return path, nil
|
|
0 commit comments