From be1e515c1baf88e7353af4c57d27ed252979793b Mon Sep 17 00:00:00 2001 From: Yacine FODIL Date: Fri, 26 Aug 2022 18:23:52 +0200 Subject: [PATCH 1/2] fix: config flag --- internal/core/autocomplete.go | 6 ++++++ internal/core/bootstrap.go | 4 ++-- internal/core/client.go | 16 ++++++++++------ internal/core/cobra_utils.go | 2 +- internal/core/context.go | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/internal/core/autocomplete.go b/internal/core/autocomplete.go index df603a1aae..3e4a971e44 100644 --- a/internal/core/autocomplete.go +++ b/internal/core/autocomplete.go @@ -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", }) diff --git a/internal/core/bootstrap.go b/internal/core/bootstrap.go index 976eb9befd..3d9bfdcebc 100644 --- a/internal/core/bootstrap.go +++ b/internal/core/bootstrap.go @@ -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:]) diff --git a/internal/core/client.go b/internal/core/client.go index 03bde70fcd..8443992d77 100644 --- a/internal/core/client.go +++ b/internal/core/client.go @@ -1,6 +1,7 @@ package core import ( + "context" "fmt" "net/http" "strings" @@ -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 } diff --git a/internal/core/cobra_utils.go b/internal/core/cobra_utils.go index d9a12c4e2e..95026f73f4 100644 --- a/internal/core/cobra_utils.go +++ b/internal/core/cobra_utils.go @@ -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 } diff --git a/internal/core/context.go b/internal/core/context.go index 0448d6d372..34da31f0e4 100644 --- a/internal/core/context.go +++ b/internal/core/context.go @@ -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 } From 293cb258a6fb66f1fdf496a5ad9d49176d6d48b2 Mon Sep 17 00:00:00 2001 From: Yacine FODIL Date: Mon, 29 Aug 2022 09:36:07 +0200 Subject: [PATCH 2/2] fix: update test --- internal/core/autocomplete_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/core/autocomplete_test.go b/internal/core/autocomplete_test.go index c01281311c..84b909f9d7 100644 --- a/internal/core/autocomplete_test.go +++ b/internal/core/autocomplete_test.go @@ -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="}})) @@ -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="}}))