Skip to content

fix(config): use SCW_CLI_CONFIG_PATH with set config #4682

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 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/scw/testdata/test-all-usage-alias-usage.golden
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
This namespace allows you to manage your aliases
Aliases are store in cli config file, Default path for this configuration file is based on the following priority order:
Aliases are stored in cli config file, Default path for this configuration file is based on the following priority order:

- $SCW_CLI_CONFIG_PATH
- $XDG_CONFIG_HOME/scw/cli.yaml
- $HOME/.config/scw/cli.yaml
- $USERPROFILE/.config/scw/cli.yaml
- $XDG_CONFIG_HOME/scw/config.yaml
- $HOME/.config/scw/config.yaml
- $USERPROFILE/.config/scw/config.yaml

You can use multiple aliases in one command
aliases in your commands are evaluated and you get completion
Expand Down
2 changes: 1 addition & 1 deletion core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
ctx = InjectMeta(ctx, meta)

// Load CLI config
cliCfg, err := cliConfig.LoadConfig(ExtractCliConfigPath(ctx))
cliCfg, err := cliConfig.LoadConfig(ExtractConfigPath(ctx))
if err != nil {
printErr := printer.Print(err, nil)
if printErr != nil {
Expand Down
12 changes: 1 addition & 11 deletions core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,9 @@ func ExtractHTTPClient(ctx context.Context) *http.Client {
func ExtractConfigPath(ctx context.Context) string {
meta := extractMeta(ctx)
if meta.ConfigPathFlag != "" {
return extractMeta(ctx).ConfigPathFlag
return meta.ConfigPathFlag
}
// This is only useful for test when we override home environment variable
if home := meta.OverrideEnv["HOME"]; home != "" {
return path.Join(home, ".config", "scw", "config.yaml")
}

return scw.GetConfigPath()
}

func ExtractCliConfigPath(ctx context.Context) string {
meta := extractMeta(ctx)
// This is only useful for test when we override home environment variable
if home := meta.OverrideEnv["HOME"]; home != "" {
return path.Join(home, ".config", "scw", cliConfig.DefaultConfigFileName)
}
Expand Down
8 changes: 4 additions & 4 deletions docs/commands/alias.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!-- DO NOT EDIT: this file is automatically generated using scw-doc-gen -->
# Documentation for `scw alias`
This namespace allows you to manage your aliases
Aliases are store in cli config file, Default path for this configuration file is based on the following priority order:
Aliases are stored in cli config file, Default path for this configuration file is based on the following priority order:

- $SCW_CLI_CONFIG_PATH
- $XDG_CONFIG_HOME/scw/cli.yaml
- $HOME/.config/scw/cli.yaml
- $USERPROFILE/.config/scw/cli.yaml
- $XDG_CONFIG_HOME/scw/config.yaml
- $HOME/.config/scw/config.yaml
- $USERPROFILE/.config/scw/config.yaml

You can use multiple aliases in one command
aliases in your commands are evaluated and you get completion
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
const (
ScwConfigPathEnv = "SCW_CLI_CONFIG_PATH"

DefaultConfigFileName = "cli.yaml"
DefaultConfigFileName = "config.yaml"
defaultConfigPermission = 0o644

DefaultOutput = "human"
Expand Down
8 changes: 4 additions & 4 deletions internal/namespaces/alias/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func aliasRootCommand() *core.Command {
Groups: []string{"config"},
Short: "Alias related commands",
Long: `This namespace allows you to manage your aliases
Aliases are store in cli config file, Default path for this configuration file is based on the following priority order:
Aliases are stored in cli config file, Default path for this configuration file is based on the following priority order:

- $SCW_CLI_CONFIG_PATH
- $XDG_CONFIG_HOME/scw/cli.yaml
- $HOME/.config/scw/cli.yaml
- $USERPROFILE/.config/scw/cli.yaml
- $XDG_CONFIG_HOME/scw/config.yaml
- $HOME/.config/scw/config.yaml
- $USERPROFILE/.config/scw/config.yaml

You can use multiple aliases in one command
aliases in your commands are evaluated and you get completion
Expand Down
8 changes: 6 additions & 2 deletions internal/namespaces/config/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ The only allowed attributes are access_key, secret_key, default_organization_id,
configPath := core.ExtractConfigPath(ctx)
config, err := scw.LoadConfigFromPath(configPath)
if err != nil {
return nil, err
if strings.Contains(err.Error(), "no such file or directory") {
fmt.Fprintln(os.Stdout, "config file not found, will attempt to create it")
config = &scw.Config{}
} else {
return nil, err
}
}

// send_telemetry is the only key that is not in a profile but in the config object directly
Expand All @@ -316,7 +321,6 @@ The only allowed attributes are access_key, secret_key, default_organization_id,
profileValue.Field(i).Set(field)
}
}

// Save
err = config.SaveTo(configPath)
if err != nil {
Expand Down
Loading