Skip to content

Commit

Permalink
Merge pull request #71 from twitchdev/enhancement/config-path-issue-33
Browse files Browse the repository at this point in the history
Moves config for new users into a .config folder per #33
  • Loading branch information
lleadbet authored Aug 17, 2021
2 parents de5d1ff + 92604a2 commit f71e161
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 28 additions & 6 deletions internal/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/util/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f71e161

Please sign in to comment.