Skip to content

Commit

Permalink
fix: config flag (#2476)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil authored Aug 29, 2022
1 parent d91da82 commit 7079569
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
6 changes: 6 additions & 0 deletions internal/core/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (node *AutoCompleteNode) addGlobalFlags() {
PrinterTypeTemplate.String(),
}

node.Children["-c"] = NewAutoCompleteFlagNode(node, &FlagSpec{
Name: "-c",
})
node.Children["--config"] = NewAutoCompleteFlagNode(node, &FlagSpec{
Name: "--config",
})
node.Children["-D"] = NewAutoCompleteFlagNode(node, &FlagSpec{
Name: "-D",
})
Expand Down
6 changes: 3 additions & 3 deletions internal/core/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ func TestAutocomplete(t *testing.T) {
t.Run("scw test flower delete hibiscus with-leaves=tr", run(&testCase{Suggestions: AutocompleteSuggestions{"with-leaves=true"}}))
t.Run("scw test flower delete hibiscus with-leaves=yes", run(&testCase{Suggestions: nil}))
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"}}))
t.Run("scw -", run(&testCase{Suggestions: AutocompleteSuggestions{"--debug", "--help", "--output", "--profile", "-D", "-h", "-o", "-p"}}))
t.Run("scw -", run(&testCase{Suggestions: AutocompleteSuggestions{"--config", "--debug", "--help", "--output", "--profile", "-D", "-c", "-h", "-o", "-p"}}))
t.Run("scw test -o j", run(&testCase{Suggestions: AutocompleteSuggestions{"json"}}))
t.Run("scw test flower -o ", run(&testCase{Suggestions: AutocompleteSuggestions{PrinterTypeHuman.String(), PrinterTypeJSON.String(), PrinterTypeTemplate.String(), PrinterTypeYAML.String()}}))
t.Run("scw test flower -o json create -", run(&testCase{Suggestions: AutocompleteSuggestions{"--debug", "--help", "--output", "--profile", "--wait", "-D", "-h", "-p", "-w"}}))
t.Run("scw test flower -o json create -", run(&testCase{Suggestions: AutocompleteSuggestions{"--config", "--debug", "--help", "--output", "--profile", "--wait", "-D", "-c", "-h", "-p", "-w"}}))
t.Run("scw test flower create name=p -o j", run(&testCase{Suggestions: AutocompleteSuggestions{"json"}}))
t.Run("scw test flower create name=p -o json ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.", "size=", "species="}}))
t.Run("scw test flower create name=p -o=json ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.", "size=", "species="}}))
Expand All @@ -161,7 +161,7 @@ func TestAutocomplete(t *testing.T) {
t.Run("scw test --profile xxxx flower create name=p ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.", "size=", "species="}}))
t.Run("scw test flower create name=p --profile xxxx", run(&testCase{Suggestions: nil}))

t.Run("scw test flower -o json delete -", run(&testCase{Suggestions: AutocompleteSuggestions{"--debug", "--help", "--output", "--profile", "-D", "-h", "-p"}}))
t.Run("scw test flower -o json delete -", run(&testCase{Suggestions: AutocompleteSuggestions{"--config", "--debug", "--help", "--output", "--profile", "-D", "-c", "-h", "-p"}}))
t.Run("scw test flower delete -o ", run(&testCase{Suggestions: AutocompleteSuggestions{PrinterTypeHuman.String(), PrinterTypeJSON.String(), PrinterTypeTemplate.String(), PrinterTypeYAML.String()}}))
t.Run("scw test flower delete -o j", run(&testCase{Suggestions: AutocompleteSuggestions{"json"}}))
t.Run("scw test flower delete -o json ", run(&testCase{Suggestions: AutocompleteSuggestions{"anemone", "hibiscus", "with-leaves="}}))
Expand Down
4 changes: 2 additions & 2 deletions internal/core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e

// These flag are already handle at the beginning of this function but we keep this
// declaration in order for them to be shown in the cobra usage documentation.
rootCmd.PersistentFlags().StringVarP(&configPathFlag, "profile", "p", "", "The config profile to use")
rootCmd.PersistentFlags().StringVarP(&profileFlag, "config", "c", "", "The path to the config file")
rootCmd.PersistentFlags().StringVarP(&profileFlag, "profile", "p", "", "The config profile to use")
rootCmd.PersistentFlags().StringVarP(&configPathFlag, "config", "c", "", "The path to the config file")
rootCmd.PersistentFlags().StringVarP(&outputFlag, "output", "o", "human", "Output format: json or human, see 'scw help output' for more info")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "Enable debug mode")
rootCmd.SetArgs(config.Args[1:])
Expand Down
16 changes: 10 additions & 6 deletions internal/core/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"context"
"fmt"
"net/http"
"strings"
Expand All @@ -11,17 +12,20 @@ import (
)

// createClient creates a Scaleway SDK client.
func createClient(httpClient *http.Client, buildInfo *BuildInfo, profileName string) (*scw.Client, error) {
func createClient(ctx context.Context, httpClient *http.Client, buildInfo *BuildInfo, profileName string) (*scw.Client, error) {
_, err := scw.MigrateLegacyConfig()
if err != nil {
return nil, err
}

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

Expand Down
2 changes: 1 addition & 1 deletion internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)

// 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
if !cmd.AllowAnonymousClient && !meta.isClientFromBootstrapConfig {
client, err := createClient(meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
client, err := createClient(ctx, meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func ReloadClient(ctx context.Context) error {
if meta.isClientFromBootstrapConfig {
return nil
}
meta.Client, err = createClient(meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
meta.Client, err = createClient(ctx, meta.httpClient, meta.BuildInfo, ExtractProfileName(ctx))
return err
}

Expand Down

0 comments on commit 7079569

Please sign in to comment.