diff --git a/README.md b/README.md index 39f547afb..a9b50a724 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 94c7c1e87..b3f546347 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -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) diff --git a/internal/pkg/auth/storage.go b/internal/pkg/auth/storage.go index 1bcb6e784..9d08ca070 100644 --- a/internal/pkg/auth/storage.go +++ b/internal/pkg/auth/storage.go @@ -18,7 +18,7 @@ type AuthFlow string const ( keyringService = "stackit-cli" - textFileFolderName = ".stackit" + textFileFolderName = "stackit" textFileName = "cli-auth-storage.txt" ) @@ -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) @@ -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) @@ -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) diff --git a/internal/pkg/auth/storage_test.go b/internal/pkg/auth/storage_test.go index 5b09c3c3d..1d3d4dab9 100644 --- a/internal/pkg/auth/storage_test.go +++ b/internal/pkg/auth/storage_test.go @@ -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) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 4077c1d3a..c4d7742a4 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -39,7 +39,7 @@ const ( // Backend config keys const ( - configFolder = ".stackit" + configFolder = "stackit" configFileName = "cli-config" configFileExtension = "json" ProjectNameKey = "project_name" @@ -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