-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): dynamic shell completion for main resources names (rollout…
…s, experiments, analysisrun) (#2379) * PoC dynamic shell completion for 'kubectl-argo-rollouts get rollout [TAB]' works toward #2015 use ValidArgsFunction from cobra to list candidates for completion: https://github.com/spf13/cobra/blob/main/shell_completions.md#dynamic-completion-of-nouns use v2 of GenBashCompletion from cobra: https://github.com/spf13/cobra/blob/main/shell_completions.md#bash-completion-v2 - I chose to disable descriptions for completion (as a bash user I'm not used to that), but it's enabled for fish it seems, we can enable it if desired, I have no strong opinion on it Signed-off-by: Thomas Riccardi <[email protected]> * Factorize resource names completion functions in new util/completion Signed-off-by: Thomas Riccardi <[email protected]> * Add dynamic auto-completion to all ROLLOUT_NAME locations in CLI ...accoding to the Cobra auto-generated CLI documentation. Signed-off-by: Thomas Riccardi <[email protected]> * Add dynamic auto-completion to all EXPERIMENT_NAME locations in CLI ...accoding to the Cobra auto-generated CLI documentation. Signed-off-by: Thomas Riccardi <[email protected]> * Add dynamic auto-completion to all ANALYSISRUN_NAME locations in CLI ...accoding to the Cobra auto-generated CLI documentation. Signed-off-by: Thomas Riccardi <[email protected]> * WIP unit-test dynamic completion Signed-off-by: Thomas Riccardi <[email protected]> * Add unit-tests for new completion package - inspired from kubectl own completion package tests - fake cmd to avoid circular import - reuse info/testdata but we could build our own data for more isolated tests if needed - test all 3 types (Rollout, Experiment, AnalysisRun) - test both first argument completion and second argument Signed-off-by: Thomas Riccardi <[email protected]> * Revert "WIP unit-test dynamic completion" Like in kubectl own completion tests, we now have an independent 'completion' package with its own tests. Stop testing in cmd/ callers packages. This reverts commit 75eb5ae. Signed-off-by: Thomas Riccardi <[email protected]> Signed-off-by: Thomas Riccardi <[email protected]>
- Loading branch information
1 parent
8d668c5
commit 3f02c92
Showing
14 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package completion | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options" | ||
) | ||
|
||
// RolloutNameCompletionFunc Returns a completion function that completes as a first argument | ||
// the Rollouts names that match the toComplete prefix. | ||
func RolloutNameCompletionFunc(o *options.ArgoRolloutsOptions) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { | ||
return func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) != 0 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
// list Rollouts names | ||
ctx := c.Context() | ||
opts := metav1.ListOptions{} | ||
rolloutIf := o.RolloutsClientset().ArgoprojV1alpha1().Rollouts(o.Namespace()) | ||
rolloutList, err := rolloutIf.List(ctx, opts) | ||
if err != nil { | ||
return []string{}, cobra.ShellCompDirectiveError | ||
} | ||
|
||
var rolloutNames []string | ||
for _, ro := range rolloutList.Items { | ||
if strings.HasPrefix(ro.Name, toComplete) { | ||
rolloutNames = append(rolloutNames, ro.Name) | ||
} | ||
} | ||
|
||
return rolloutNames, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
} | ||
|
||
// ExperimentNameCompletionFunc Returns a completion function that completes as a first argument | ||
// the Experiments names that match the toComplete prefix. | ||
func ExperimentNameCompletionFunc(o *options.ArgoRolloutsOptions) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { | ||
return func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) != 0 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
// list Experiments names | ||
ctx := c.Context() | ||
opts := metav1.ListOptions{} | ||
expIf := o.RolloutsClientset().ArgoprojV1alpha1().Experiments(o.Namespace()) | ||
expList, err := expIf.List(ctx, opts) | ||
if err != nil { | ||
return []string{}, cobra.ShellCompDirectiveError | ||
} | ||
|
||
var expNames []string | ||
for _, exp := range expList.Items { | ||
if strings.HasPrefix(exp.Name, toComplete) { | ||
expNames = append(expNames, exp.Name) | ||
} | ||
} | ||
|
||
return expNames, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
} | ||
|
||
// AnalysisRunNameCompletionFunc Returns a completion function that completes as a first argument | ||
// the AnalysisRuns names that match the toComplete prefix. | ||
func AnalysisRunNameCompletionFunc(o *options.ArgoRolloutsOptions) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { | ||
return func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) != 0 { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
// list AnalysisRuns names | ||
ctx := c.Context() | ||
opts := metav1.ListOptions{} | ||
arIf := o.RolloutsClientset().ArgoprojV1alpha1().AnalysisRuns(o.Namespace()) | ||
arList, err := arIf.List(ctx, opts) | ||
if err != nil { | ||
return []string{}, cobra.ShellCompDirectiveError | ||
} | ||
|
||
var arNames []string | ||
for _, ar := range arList.Items { | ||
if strings.HasPrefix(ar.Name, toComplete) { | ||
arNames = append(arNames, ar.Name) | ||
} | ||
} | ||
|
||
return arNames, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
} |
Oops, something went wrong.