diff --git a/internal/core/autocomplete.go b/internal/core/autocomplete.go index ebfc7f4c99..13182a975c 100644 --- a/internal/core/autocomplete.go +++ b/internal/core/autocomplete.go @@ -6,8 +6,6 @@ import ( "sort" "strconv" "strings" - - "github.com/scaleway/scaleway-sdk-go/logger" ) // AutocompleteSuggestions is a list of words to be set to the shell as autocomplete suggestions. @@ -53,10 +51,6 @@ type FlagSpec struct { } func (node *AutoCompleteNode) addGlobalFlags() { - node.Children["--access-key"] = NewAutoCompleteFlagNode(node, &FlagSpec{ - Name: "--access-key", - HasVariableValue: true, - }) node.Children["-D"] = NewAutoCompleteFlagNode(node, &FlagSpec{ Name: "-D", }) @@ -78,13 +72,11 @@ func (node *AutoCompleteNode) addGlobalFlags() { EnumValues: []string{"json", "human"}, }) node.Children["-p"] = NewAutoCompleteFlagNode(node, &FlagSpec{ - Name: "-p", + Name: "-p", + HasVariableValue: true, }) node.Children["--profile"] = NewAutoCompleteFlagNode(node, &FlagSpec{ - Name: "--profile", - }) - node.Children["--secret-key"] = NewAutoCompleteFlagNode(node, &FlagSpec{ - Name: "--secret-key", + Name: "--profile", HasVariableValue: true, }) } @@ -273,7 +265,6 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string // When a flag is found it populates `completedFlags`. // When an argument is found it populates `completedArgs`. for i, word := range append(leftWords, rightWords...) { - logger.Debugf("word: '%v'", word) switch { // handle --flag=value and --flag case isFlag(word): diff --git a/internal/core/autocomplete_test.go b/internal/core/autocomplete_test.go index dfa7da5a1d..af6892b107 100644 --- a/internal/core/autocomplete_test.go +++ b/internal/core/autocomplete_test.go @@ -129,19 +129,19 @@ func TestAutocomplete(t *testing.T) { t.Run("scw test flower delete flower f", run(&testCase{Suggestions: nil})) // TODO: t.Run("scw test flower create leaves.0.size=", run(&testCase{Suggestions: AutocompleteSuggestions{"L", "M", "S", "XL", "XXL"}})) - t.Run("scw -", run(&testCase{Suggestions: AutocompleteSuggestions{"--access-key", "--debug", "--help", "--output", "--profile", "--secret-key", "-D", "-h", "-o", "-p"}})) + t.Run("scw -", run(&testCase{Suggestions: AutocompleteSuggestions{"--debug", "--help", "--output", "--profile", "-D", "-h", "-o", "-p"}})) t.Run("scw test -o j", run(&testCase{Suggestions: AutocompleteSuggestions{"json"}})) t.Run("scw test flower -o ", run(&testCase{Suggestions: AutocompleteSuggestions{"human", "json"}})) - t.Run("scw test flower -o json create -", run(&testCase{Suggestions: AutocompleteSuggestions{"--access-key", "--debug", "--help", "--output", "--profile", "--secret-key", "--wait", "-D", "-h", "-p", "-w"}})) + 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 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.0.size=", "size=", "species="}})) t.Run("scw test flower create name=p -o=json ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.0.size=", "size=", "species="}})) t.Run("scw test flower create name=p -o=jso", run(&testCase{Suggestions: nil})) t.Run("scw test flower create name=p -o", run(&testCase{Suggestions: AutocompleteSuggestions{"-o"}})) t.Run("scw test -o json flower create ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.0.size=", "name=", "size=", "species="}})) - t.Run("scw test flower create name=p --secret-key xxxx ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.0.size=", "size=", "species="}})) - t.Run("scw test --secret-key xxxx flower create name=p ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.0.size=", "size=", "species="}})) - t.Run("scw test flower create name=p --secret-key xxxx", run(&testCase{Suggestions: nil})) + t.Run("scw test flower create name=p --profile xxxx ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.0.size=", "size=", "species="}})) + t.Run("scw test --profile xxxx flower create name=p ", run(&testCase{Suggestions: AutocompleteSuggestions{"colours.0=", "leaves.0.size=", "size=", "species="}})) + t.Run("scw test flower create name=p --profile xxxx", run(&testCase{Suggestions: nil})) } func TestWordIndex(t *testing.T) { diff --git a/internal/core/client.go b/internal/core/client.go index ec796c1200..a6a6fe64a8 100644 --- a/internal/core/client.go +++ b/internal/core/client.go @@ -67,14 +67,13 @@ func createClient(meta *meta) (*scw.Client, error) { } // configErrorDetails generate a detailed error message for an invalid client option. -func configErrorDetails(configKey, varEnv, flagName string) string { +func configErrorDetails(configKey, varEnv string) string { // TODO: update the more info link return fmt.Sprintf(`%s can be initialised using the command "scw init". After initialisation, there are three ways to provide %s: - with the Scaleway config file, in the %s key: %s; - with the %s environement variable; -- with %s global CLI flag. Note that the last method has the highest priority. @@ -84,7 +83,6 @@ More info: https://github.com/scaleway/scaleway-sdk-go/tree/master/scw#scaleway- configKey, scw.GetConfigPath(), varEnv, - flagName, ) } @@ -95,7 +93,7 @@ func validateProfile(profile *scw.Profile) error { if profile.AccessKey == nil || *profile.AccessKey == "" { return &CliError{ Err: fmt.Errorf("access key is required"), - Details: configErrorDetails("access_key", "SCW_ACCESS_KEY", "access-key"), + Details: configErrorDetails("access_key", "SCW_ACCESS_KEY"), Hint: credentialsHint, } } @@ -110,7 +108,7 @@ func validateProfile(profile *scw.Profile) error { if profile.SecretKey == nil || *profile.SecretKey == "" { return &CliError{ Err: fmt.Errorf("secret key is required"), - Details: configErrorDetails("secret_key", "SCW_SECRET_KEY", "secret-key"), + Details: configErrorDetails("secret_key", "SCW_SECRET_KEY"), Hint: credentialsHint, } } @@ -125,7 +123,7 @@ func validateProfile(profile *scw.Profile) error { if profile.DefaultOrganizationID == nil || *profile.DefaultOrganizationID == "" { return &CliError{ Err: fmt.Errorf("organization ID is required"), - Details: configErrorDetails("default_organization_id", "SCW_DEFAULT_ORGANIZATION_ID", "organization-id"), + Details: configErrorDetails("default_organization_id", "SCW_DEFAULT_ORGANIZATION_ID"), Hint: credentialsHint, } } @@ -140,7 +138,7 @@ func validateProfile(profile *scw.Profile) error { if profile.DefaultZone == nil || *profile.DefaultZone == "" { return &CliError{ Err: fmt.Errorf("zone is required"), - Details: configErrorDetails("default_zone", "SCW_DEFAULT_ZONE", "zone"), + Details: configErrorDetails("default_zone", "SCW_DEFAULT_ZONE"), Hint: credentialsHint, } } diff --git a/internal/core/cobra_builder.go b/internal/core/cobra_builder.go index 14a96c5c21..0c11cab25d 100644 --- a/internal/core/cobra_builder.go +++ b/internal/core/cobra_builder.go @@ -4,13 +4,11 @@ import ( "strings" "github.com/spf13/cobra" - flag "github.com/spf13/pflag" ) func init() { // we disable cobra command sorting to position important commands at the top when looking at the usage. cobra.EnableCommandSorting = false - cobra.AddTemplateFunc("removeClientFlags", removeClientFlags) } // cobraBuilder will transform a []*Command to a valid Cobra root command. @@ -155,7 +153,7 @@ FLAGS: {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} GLOBAL FLAGS: -{{ (removeClientFlags .InheritedFlags .Annotations.NoClient).FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .Annotations.SeeAlsos}} +{{ .InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .Annotations.SeeAlsos}} SEE ALSO: {{.Annotations.SeeAlsos}}{{end}}{{if .HasHelpSubCommands}} @@ -166,14 +164,6 @@ Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}} Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}} ` -func removeClientFlags(flags *flag.FlagSet, noClient string) *flag.FlagSet { - if noClient == "true" { - panicOnError(flags.MarkHidden("access-key")) - panicOnError(flags.MarkHidden("secret-key")) - } - return flags -} - func panicOnError(err error) { if err != nil { panic(err)