Skip to content

Moves config for new users into a .config folder per #33 #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 17, 2021
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