Skip to content

Commit

Permalink
Add dynamic auto-completion to all ANALYSISRUN_NAME locations in CLI
Browse files Browse the repository at this point in the history
...accoding to the Cobra auto-generated CLI documentation.

Signed-off-by: Thomas Riccardi <[email protected]>
  • Loading branch information
thomas-riccardi committed Oct 27, 2022
1 parent d3f9fd3 commit 855832d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/kubectl-argo-rollouts/cmd/terminate/terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func NewCmdTerminateAnalysisRun(o *options.ArgoRolloutsOptions) *cobra.Command {
}
return nil
},
ValidArgsFunction: completionutil.AnalysisRunNameCompletionFunc(o),
}
return cmd
}
Expand Down
31 changes: 29 additions & 2 deletions pkg/kubectl-argo-rollouts/util/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func RolloutNameCompletionFunc(o *options.ArgoRolloutsOptions) func(*cobra.Comma
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
// list rollouts names
// list Rollouts names
ctx := c.Context()
opts := metav1.ListOptions{}
rolloutIf := o.RolloutsClientset().ArgoprojV1alpha1().Rollouts(o.Namespace())
Expand All @@ -43,7 +43,7 @@ func ExperimentNameCompletionFunc(o *options.ArgoRolloutsOptions) func(*cobra.Co
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
// list experiments names
// list Experiments names
ctx := c.Context()
opts := metav1.ListOptions{}
expIf := o.RolloutsClientset().ArgoprojV1alpha1().Experiments(o.Namespace())
Expand All @@ -62,3 +62,30 @@ func ExperimentNameCompletionFunc(o *options.ArgoRolloutsOptions) func(*cobra.Co
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
}
}

0 comments on commit 855832d

Please sign in to comment.