From 10b8196d3211f470449248eb63966b1fb05cc787 Mon Sep 17 00:00:00 2001 From: Jerome Quere Date: Wed, 22 Apr 2020 19:28:24 +0200 Subject: [PATCH] fix --- internal/core/autocomplete.go | 17 +++++------------ internal/core/cobra_builder.go | 2 +- internal/core/cobra_usage_builder.go | 4 ++-- internal/core/cobra_utils.go | 12 ++++++------ internal/core/testing.go | 1 - .../namespaces/autocomplete/autocomplete.go | 6 +++--- scripts/install.sh | 1 + 7 files changed, 18 insertions(+), 25 deletions(-) diff --git a/internal/core/autocomplete.go b/internal/core/autocomplete.go index 764cad8b46..641e033d6d 100644 --- a/internal/core/autocomplete.go +++ b/internal/core/autocomplete.go @@ -194,12 +194,11 @@ func (node *AutoCompleteNode) isLeafCommand() bool { } // BuildAutoCompleteTree builds the autocomplete tree from the commands, subcommands and arguments -func BuildAutoCompleteTree(commands *Commands, meta *meta) *AutoCompleteNode { +func BuildAutoCompleteTree(commands *Commands) *AutoCompleteNode { root := NewAutoCompleteCommandNode() - scwCommand := root.GetChildOrCreate(meta.BinaryName) - scwCommand.addGlobalFlags() + root.addGlobalFlags() for _, cmd := range commands.commands { - node := scwCommand + node := root // Creates nodes for namespaces, resources, verbs for _, part := range []string{cmd.Namespace, cmd.Resource, cmd.Verb} { @@ -246,23 +245,17 @@ func BuildAutoCompleteTree(commands *Commands, meta *meta) *AutoCompleteNode { // eg: scw test flower create name=p -o=jso func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string, rightWords []string) *AutocompleteResponse { commands := ExtractCommands(ctx) - meta := extractMeta(ctx) // Create AutoComplete Tree - commandTreeRoot := BuildAutoCompleteTree(commands, meta) + commandTreeRoot := BuildAutoCompleteTree(commands) // For each left word that is not a flag nor an argument, we try to go deeper in the autocomplete tree and store the current node in `node`. node := commandTreeRoot // nodeIndexInWords is the rightmost word index, before the cursor, that contains either a namespace or verb or resource or flag or flag value. // see test 'scw test flower delete f' nodeIndexInWords := 0 - for i, word := range leftWords { - if i == 0 { - // override the command name with the real one - // useful when command is renamed or behind a shell function - word = meta.BinaryName - } + for i, word := range leftWords[1:] { children, childrenExists := node.Children[word] if !childrenExists { children, childrenExists = node.Children[positionalValueNodeID] diff --git a/internal/core/cobra_builder.go b/internal/core/cobra_builder.go index fb86203b8c..3be0184ac4 100644 --- a/internal/core/cobra_builder.go +++ b/internal/core/cobra_builder.go @@ -104,7 +104,7 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) { } if cmd.Examples != nil { - cobraCmd.Annotations["Examples"] = buildExamples(cmd, b.meta) + cobraCmd.Annotations["Examples"] = buildExamples(cmd, b.meta.BinaryName) } if cmd.SeeAlsos != nil { diff --git a/internal/core/cobra_usage_builder.go b/internal/core/cobra_usage_builder.go index 73d192054b..bc95be464f 100644 --- a/internal/core/cobra_usage_builder.go +++ b/internal/core/cobra_usage_builder.go @@ -70,7 +70,7 @@ func _buildArgShort(as *ArgSpec) string { // buildExamples builds usage examples string. // This string will be used by cobra usage template. -func buildExamples(cmd *Command, meta *meta) string { +func buildExamples(cmd *Command, binaryName string) string { // Build the examples array. var examples []string @@ -110,7 +110,7 @@ func buildExamples(cmd *Command, meta *meta) string { // Build command line example. commandParts := []string{ - meta.BinaryName, + binaryName, cmd.Namespace, cmd.Resource, cmd.Verb, diff --git a/internal/core/cobra_utils.go b/internal/core/cobra_utils.go index 406de40637..f39e724372 100644 --- a/internal/core/cobra_utils.go +++ b/internal/core/cobra_utils.go @@ -26,7 +26,7 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string) cmdArgs := reflect.New(cmd.ArgsType).Interface() // Handle positional argument by catching first argument `` and rewrite it to `=`. - if err = handlePositionalArg(cmd, rawArgs, meta); err != nil { + if err = handlePositionalArg(meta.BinaryName, cmd, rawArgs); err != nil { return err } @@ -113,7 +113,7 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string) // - no positional argument is found. // - an unknown positional argument exists in the comand. // - an argument duplicates a positional argument. -func handlePositionalArg(cmd *Command, rawArgs []string, meta *meta) error { +func handlePositionalArg(binaryName string, cmd *Command, rawArgs []string) error { positionalArg := cmd.ArgSpecs.GetPositionalArg() // Command does not have a positional argument. @@ -131,7 +131,7 @@ func handlePositionalArg(cmd *Command, rawArgs []string, meta *meta) error { otherArgs := append(rawArgs[:i], rawArgs[i+1:]...) return &CliError{ Err: fmt.Errorf("a positional argument is required for this command"), - Hint: positionalArgHint(cmd, argumentValue, otherArgs, positionalArgumentFound, meta), + Hint: positionalArgHint(binaryName, cmd, argumentValue, otherArgs, positionalArgumentFound), } } } @@ -145,12 +145,12 @@ func handlePositionalArg(cmd *Command, rawArgs []string, meta *meta) error { // No positional argument found. return &CliError{ Err: fmt.Errorf("a positional argument is required for this command"), - Hint: positionalArgHint(cmd, "<"+positionalArg.Name+">", rawArgs, false, meta), + Hint: positionalArgHint(binaryName, cmd, "<"+positionalArg.Name+">", rawArgs, false), } } // positionalArgHint formats the positional argument error hint. -func positionalArgHint(cmd *Command, hintValue string, otherArgs []string, positionalArgumentFound bool, meta *meta) string { +func positionalArgHint(binaryName string, cmd *Command, hintValue string, otherArgs []string, positionalArgumentFound bool) string { suggestedArgs := []string{} // If no positional argument exists, suggest one. @@ -161,7 +161,7 @@ func positionalArgHint(cmd *Command, hintValue string, otherArgs []string, posit // Suggest to use the other arguments. suggestedArgs = append(suggestedArgs, otherArgs...) - suggestedCommand := append([]string{meta.BinaryName, cmd.GetCommandLine()}, suggestedArgs...) + suggestedCommand := append([]string{binaryName, cmd.GetCommandLine()}, suggestedArgs...) return "Try running: " + strings.Join(suggestedCommand, " ") } diff --git a/internal/core/testing.go b/internal/core/testing.go index 5112f7cfd0..31724f693b 100644 --- a/internal/core/testing.go +++ b/internal/core/testing.go @@ -189,7 +189,6 @@ func getTestClient(t *testing.T, testConfig *TestConfig) (client *scw.Client, cl // Run a CLI integration test. See TestConfig for configuration option func Test(config *TestConfig) func(t *testing.T) { return func(t *testing.T) { - os.Args[0] = "scw" // to enable the right binary name on tests if !config.DisableParallel { t.Parallel() diff --git a/internal/namespaces/autocomplete/autocomplete.go b/internal/namespaces/autocomplete/autocomplete.go index 323a39eae3..3341327ac3 100644 --- a/internal/namespaces/autocomplete/autocomplete.go +++ b/internal/namespaces/autocomplete/autocomplete.go @@ -64,7 +64,7 @@ func autocompleteScripts(binaryName string) map[string]autocompleteScript { } complete -F _%[1]s %[1]s `, binaryName), - CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=bash)"`, os.Args[0]), + CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=bash)"`, binaryName), ShellConfigurationFile: map[string]string{ "darwin": path.Join(homePath, ".bash_profile"), "linux": path.Join(homePath, ".bashrc"), @@ -88,7 +88,7 @@ func autocompleteScripts(binaryName string) map[string]autocompleteScript { complete --command %[1]s --no-files; complete --command %[1]s --arguments '(%[1]s autocomplete complete fish -- (commandline) (commandline --cursor) (commandline --current-token) (commandline --current-process --tokenize --cut-at-cursor))'; `, binaryName), - CompleteScript: fmt.Sprintf(`eval (%s autocomplete script shell=fish)`, os.Args[0]), + CompleteScript: fmt.Sprintf(`eval (%s autocomplete script shell=fish)`, binaryName), ShellConfigurationFile: map[string]string{ "darwin": path.Join(homePath, ".config/fish/config.fish"), "linux": path.Join(homePath, ".config/fish/config.fish"), @@ -110,7 +110,7 @@ func autocompleteScripts(binaryName string) map[string]autocompleteScript { } compdef _%[1]s %[1]s `, binaryName), - CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=zsh)"`, os.Args[0]), + CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=zsh)"`, binaryName), ShellConfigurationFile: map[string]string{ "darwin": path.Join(homePath, ".zshrc"), "linux": path.Join(homePath, ".zshrc"), diff --git a/scripts/install.sh b/scripts/install.sh index 6ff4ed293c..d434cdeff7 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -17,3 +17,4 @@ LDFLAGS=( export CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go install -ldflags "${LDFLAGS[*]}" $ROOT_DIR/cmd/scw/ +mv $GOPATH/bin/scw $GOPATH/bin/toto