Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-quere committed Apr 22, 2020
1 parent 2a708df commit 10b8196
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 25 deletions.
17 changes: 5 additions & 12 deletions internal/core/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion internal/core/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/core/cobra_usage_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<value>` and rewrite it to `<arg-name>=<value>`.
if err = handlePositionalArg(cmd, rawArgs, meta); err != nil {
if err = handlePositionalArg(meta.BinaryName, cmd, rawArgs); err != nil {
return err
}

Expand Down Expand Up @@ -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.
Expand All @@ -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),
}
}
}
Expand All @@ -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.
Expand All @@ -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, " ")
}

Expand Down
1 change: 0 additions & 1 deletion internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions internal/namespaces/autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 10b8196

Please sign in to comment.