Skip to content

Commit 7079569

Browse files
authored
fix: config flag (#2476)
1 parent d91da82 commit 7079569

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

internal/core/autocomplete.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ func (node *AutoCompleteNode) addGlobalFlags() {
6565
PrinterTypeTemplate.String(),
6666
}
6767

68+
node.Children["-c"] = NewAutoCompleteFlagNode(node, &FlagSpec{
69+
Name: "-c",
70+
})
71+
node.Children["--config"] = NewAutoCompleteFlagNode(node, &FlagSpec{
72+
Name: "--config",
73+
})
6874
node.Children["-D"] = NewAutoCompleteFlagNode(node, &FlagSpec{
6975
Name: "-D",
7076
})

internal/core/autocomplete_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ func TestAutocomplete(t *testing.T) {
147147
t.Run("scw test flower delete hibiscus with-leaves=tr", run(&testCase{Suggestions: AutocompleteSuggestions{"with-leaves=true"}}))
148148
t.Run("scw test flower delete hibiscus with-leaves=yes", run(&testCase{Suggestions: nil}))
149149
t.Run("scw test flower create leaves.0.size=", run(&testCase{Suggestions: AutocompleteSuggestions{"leaves.0.size=L", "leaves.0.size=M", "leaves.0.size=S", "leaves.0.size=XL", "leaves.0.size=XXL"}}))
150-
t.Run("scw -", run(&testCase{Suggestions: AutocompleteSuggestions{"--debug", "--help", "--output", "--profile", "-D", "-h", "-o", "-p"}}))
150+
t.Run("scw -", run(&testCase{Suggestions: AutocompleteSuggestions{"--config", "--debug", "--help", "--output", "--profile", "-D", "-c", "-h", "-o", "-p"}}))
151151
t.Run("scw test -o j", run(&testCase{Suggestions: AutocompleteSuggestions{"json"}}))
152152
t.Run("scw test flower -o ", run(&testCase{Suggestions: AutocompleteSuggestions{PrinterTypeHuman.String(), PrinterTypeJSON.String(), PrinterTypeTemplate.String(), PrinterTypeYAML.String()}}))
153-
t.Run("scw test flower -o json create -", run(&testCase{Suggestions: AutocompleteSuggestions{"--debug", "--help", "--output", "--profile", "--wait", "-D", "-h", "-p", "-w"}}))
153+
t.Run("scw test flower -o json create -", run(&testCase{Suggestions: AutocompleteSuggestions{"--config", "--debug", "--help", "--output", "--profile", "--wait", "-D", "-c", "-h", "-p", "-w"}}))
154154
t.Run("scw test flower create name=p -o j", run(&testCase{Suggestions: AutocompleteSuggestions{"json"}}))
155155
t.Run("scw test flower create name=p -o json ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.", "size=", "species="}}))
156156
t.Run("scw test flower create name=p -o=json ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.", "size=", "species="}}))
@@ -161,7 +161,7 @@ func TestAutocomplete(t *testing.T) {
161161
t.Run("scw test --profile xxxx flower create name=p ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.", "size=", "species="}}))
162162
t.Run("scw test flower create name=p --profile xxxx", run(&testCase{Suggestions: nil}))
163163

164-
t.Run("scw test flower -o json delete -", run(&testCase{Suggestions: AutocompleteSuggestions{"--debug", "--help", "--output", "--profile", "-D", "-h", "-p"}}))
164+
t.Run("scw test flower -o json delete -", run(&testCase{Suggestions: AutocompleteSuggestions{"--config", "--debug", "--help", "--output", "--profile", "-D", "-c", "-h", "-p"}}))
165165
t.Run("scw test flower delete -o ", run(&testCase{Suggestions: AutocompleteSuggestions{PrinterTypeHuman.String(), PrinterTypeJSON.String(), PrinterTypeTemplate.String(), PrinterTypeYAML.String()}}))
166166
t.Run("scw test flower delete -o j", run(&testCase{Suggestions: AutocompleteSuggestions{"json"}}))
167167
t.Run("scw test flower delete -o json ", run(&testCase{Suggestions: AutocompleteSuggestions{"anemone", "hibiscus", "with-leaves="}}))

internal/core/bootstrap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
196196

197197
// These flag are already handle at the beginning of this function but we keep this
198198
// declaration in order for them to be shown in the cobra usage documentation.
199-
rootCmd.PersistentFlags().StringVarP(&configPathFlag, "profile", "p", "", "The config profile to use")
200-
rootCmd.PersistentFlags().StringVarP(&profileFlag, "config", "c", "", "The path to the config file")
199+
rootCmd.PersistentFlags().StringVarP(&profileFlag, "profile", "p", "", "The config profile to use")
200+
rootCmd.PersistentFlags().StringVarP(&configPathFlag, "config", "c", "", "The path to the config file")
201201
rootCmd.PersistentFlags().StringVarP(&outputFlag, "output", "o", "human", "Output format: json or human, see 'scw help output' for more info")
202202
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "Enable debug mode")
203203
rootCmd.SetArgs(config.Args[1:])

internal/core/client.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"strings"
@@ -11,17 +12,20 @@ import (
1112
)
1213

1314
// createClient creates a Scaleway SDK client.
14-
func createClient(httpClient *http.Client, buildInfo *BuildInfo, profileName string) (*scw.Client, error) {
15+
func createClient(ctx context.Context, httpClient *http.Client, buildInfo *BuildInfo, profileName string) (*scw.Client, error) {
1516
_, err := scw.MigrateLegacyConfig()
1617
if err != nil {
1718
return nil, err
1819
}
1920

20-
config, err := scw.LoadConfig()
21-
// If the config file do not exist, don't return an error as we may find config in ENV or flags.
22-
if _, isNotFoundError := err.(*scw.ConfigFileNotFoundError); isNotFoundError {
23-
config = &scw.Config{}
24-
} else if err != nil {
21+
// Default path is based on the following priority order:
22+
// * The config file's path provided via --config flag
23+
// * $SCW_CONFIG_PATH
24+
// * $XDG_CONFIG_HOME/scw/config.yaml
25+
// * $HOME/.config/scw/config.yaml
26+
// * $USERPROFILE/.config/scw/config.yaml
27+
config, err := scw.LoadConfigFromPath(ExtractConfigPath(ctx))
28+
if err != nil {
2529
return nil, err
2630
}
2731

internal/core/cobra_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
2121

2222
// If command requires authentication and the client was not directly provided in the bootstrap config, we create a new client and overwrite the existing one
2323
if !cmd.AllowAnonymousClient && !meta.isClientFromBootstrapConfig {
24-
client, err := createClient(meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
24+
client, err := createClient(ctx, meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
2525
if err != nil {
2626
return err
2727
}

internal/core/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func ReloadClient(ctx context.Context) error {
160160
if meta.isClientFromBootstrapConfig {
161161
return nil
162162
}
163-
meta.Client, err = createClient(meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
163+
meta.Client, err = createClient(ctx, meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
164164
return err
165165
}
166166

0 commit comments

Comments
 (0)