Skip to content

Commit

Permalink
Merge pull request #133 from criteo/filter-auto-complete-options
Browse files Browse the repository at this point in the history
Filter auto complete options
  • Loading branch information
bhou-crto authored Mar 15, 2024
2 parents 33483c3 + ab5ff78 commit 5fe617f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
23 changes: 16 additions & 7 deletions internal/frontend/default-frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,20 @@ func (self *defaultFrontend) addExecutableCommands() {
}
})
}
output, err := self.executeValidArgsOfCommand(group, name, originalArgs)
output, err := self.executeValidArgsOfCommand(group, name, originalArgs, toComplete)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
parts := strings.Split(output, "\n")
if len(parts) > 0 {
if strings.HasPrefix(parts[0], "#") { // skip the first control line, for further controls
// filter the parts that starts with toComplete
filteredParts := []string{}
for idx, p := range parts {
if strings.HasPrefix(p, toComplete) || (idx == 0 && strings.HasPrefix(p, "#")) {
filteredParts = append(filteredParts, p)
}
}
if len(filteredParts) > 0 {
if strings.HasPrefix(filteredParts[0], "#") { // skip the first control line, for further controls
// the first line starting with # is the control line, it controls the completion behavior when the return body is empty
shellDirective := cobra.ShellCompDirectiveNoFileComp
switch strings.TrimSpace(strings.TrimLeft(parts[0], "#")) {
Expand All @@ -185,9 +192,9 @@ func (self *defaultFrontend) addExecutableCommands() {
case "no-file-completion":
shellDirective = cobra.ShellCompDirectiveNoFileComp
}
return parts[1:], shellDirective
return filteredParts[1:], shellDirective
}
return parts, cobra.ShellCompDirectiveNoFileComp
return filteredParts, cobra.ShellCompDirectiveNoFileComp
}
}
if len(validArgs) > 0 {
Expand Down Expand Up @@ -281,13 +288,15 @@ func (self *defaultFrontend) executeCommand(group, name string, args []string, i
}

// execute the valid args command of the cdt command
func (self *defaultFrontend) executeValidArgsOfCommand(group, name string, args []string) (string, error) {
func (self *defaultFrontend) executeValidArgsOfCommand(group, name string, args []string, toComplete string) (string, error) {
iCmd, err := self.getExecutableCommand(group, name)
if err != nil {
return "", err
}

envCtx := self.getCmdEnvContext([]string{}, []string{})
envCtx := self.getCmdEnvContext([]string{
fmt.Sprintf("%s=%s", self.appCtx.EnvVarName("TO_COMPLETE"), toComplete),
}, []string{})

_, output, err := iCmd.ExecuteValidArgsCmd(envCtx, args...)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions test/integration/test-auto-complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ else
exit 1
fi

echo "> test dynamic argument auto-complete with prefix filter"
RESULT=$($CL_PATH __complete greeting saybonjour Jo)
echo "$RESULT" | grep -q "John"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO - should auto-complete dynamic arguments"
exit 1
fi
echo "$RESULT" | grep -q "Kate"
if [ $? -eq 0 ]; then
echo "KO - should only return options starts with the prefix"
exit 1
else
echo "OK"
fi
echo "$RESULT" | grep -q "Jo$"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO - should successfully inject TO_COMPLETE envrionment variable"
exit 1
fi

echo "> test flag name auto-complete"
RESULT=$($CL_PATH __complete bonjour -)
echo "$RESULT" | grep -q "\-\-lang"
Expand Down
2 changes: 2 additions & 0 deletions test/packages-src/bonjour/auto-complete.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

ECHO %*

ECHO %CL_TO_COMPLETE%

ECHO John
ECHO Kate
1 change: 1 addition & 0 deletions test/packages-src/bonjour/auto-complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# first line print all the arguments to this script to check if all necessary arguments are passed to it
echo "$@"

echo $CL_TO_COMPLETE

echo "John"
echo "Kate"
1 change: 0 additions & 1 deletion test/packages-src/bonjour/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@
"args": [],
"validArgsCmd": [ "{{.PackageDir}}/auto-complete.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}" ]
}

]
}

0 comments on commit 5fe617f

Please sign in to comment.