Skip to content
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
15 changes: 3 additions & 12 deletions internal/core/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
})
Expand All @@ -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,
})
}
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions internal/core/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions internal/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -84,7 +83,6 @@ More info: https://github.com/scaleway/scaleway-sdk-go/tree/master/scw#scaleway-
configKey,
scw.GetConfigPath(),
varEnv,
flagName,
)
}

Expand All @@ -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,
}
}
Expand All @@ -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,
}
}
Expand All @@ -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,
}
}
Expand All @@ -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,
}
}
Expand Down
12 changes: 1 addition & 11 deletions internal/core/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}}
Expand All @@ -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)
Expand Down