Skip to content

Commit

Permalink
Move config file to correct folder (#282)
Browse files Browse the repository at this point in the history
* change config and auth storage dirs to config folder, make changes to README

* rename variables, extend storage.go

* fix testing
  • Loading branch information
DiogoFerrao authored May 6, 2024
1 parent 6dc1783 commit 54abe14
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ You can configure the CLI using the command:
stackit config
```

The configurations are stored in `~/stackit/cli-config.json` and are valid for all commands. For example, you can set a default `project-id` by running:
The configuration is saved in a file. The file's location varies depending on the operating system:

- Unix - `$XDG_CONFIG_HOME/stackit/cli-config.json`
- MacOS - `$HOME/Library/Application Support/stackit/cli-config.json`
- Windows - `%AppData%\stackit\cli-config.json`

The configuration options apply to all commands and can be set using the `stackit config set` command. For example, you can set a default `project-id` by running:

```bash
stackit config set --project-id xxxx-xxxx-xxxxx
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func NewRootCmd(version, date string, p *print.Printer) *cobra.Command {
argsString := print.BuildDebugStrFromSlice(os.Args)
p.Debug(print.DebugLevel, "arguments: %s", argsString)

configFilePath := viper.ConfigFileUsed()
p.Debug(print.DebugLevel, "using config file: %s", configFilePath)

configKeys := viper.AllSettings()
configKeysStr := print.BuildDebugStrFromMap(configKeys)
p.Debug(print.DebugLevel, "config keys: %s", configKeysStr)
Expand Down
20 changes: 10 additions & 10 deletions internal/pkg/auth/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type AuthFlow string

const (
keyringService = "stackit-cli"
textFileFolderName = ".stackit"
textFileFolderName = "stackit"
textFileName = "cli-auth-storage.txt"
)

Expand Down Expand Up @@ -78,11 +78,11 @@ func setAuthFieldInEncodedTextFile(key authFieldKey, value string) error {
return err
}

homeDir, err := os.UserHomeDir()
configDir, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf("get home dir: %w", err)
return fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(homeDir, textFileFolderName)
textFileDir := filepath.Join(configDir, textFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

contentEncoded, err := os.ReadFile(textFilePath)
Expand Down Expand Up @@ -152,11 +152,11 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) {
return "", err
}

homeDir, err := os.UserHomeDir()
configDir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("get home dir: %w", err)
return "", fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(homeDir, textFileFolderName)
textFileDir := filepath.Join(configDir, textFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

contentEncoded, err := os.ReadFile(textFilePath)
Expand All @@ -183,11 +183,11 @@ func getAuthFieldFromEncodedTextFile(key authFieldKey) (string, error) {
// If it doesn't, creates it with the content "{}" encoded.
// If it does, does nothing (and returns nil).
func createEncodedTextFile() error {
homeDir, err := os.UserHomeDir()
configDir, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf("get home dir: %w", err)
return fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(homeDir, textFileFolderName)
textFileDir := filepath.Join(configDir, textFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

err = os.MkdirAll(textFileDir, os.ModePerm)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/auth/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ func deleteAuthFieldInEncodedTextFile(key authFieldKey) error {
return err
}

homeDir, err := os.UserHomeDir()
configDir, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf("get home dir: %w", err)
return fmt.Errorf("get config dir: %w", err)
}
textFileDir := filepath.Join(homeDir, textFileFolderName)
textFileDir := filepath.Join(configDir, textFileFolderName)
textFilePath := filepath.Join(textFileDir, textFileName)

contentEncoded, err := os.ReadFile(textFilePath)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (

// Backend config keys
const (
configFolder = ".stackit"
configFolder = "stackit"
configFileName = "cli-config"
configFileExtension = "json"
ProjectNameKey = "project_name"
Expand Down Expand Up @@ -73,9 +73,9 @@ var ConfigKeys = []string{
var folderPath string

func InitConfig() {
home, err := os.UserHomeDir()
configDir, err := os.UserConfigDir()
cobra.CheckErr(err)
configFolderPath := filepath.Join(home, configFolder)
configFolderPath := filepath.Join(configDir, configFolder)
configFilePath := filepath.Join(configFolderPath, fmt.Sprintf("%s.%s", configFileName, configFileExtension))

// Write config dir path to global variable
Expand Down

0 comments on commit 54abe14

Please sign in to comment.