Skip to content

Commit

Permalink
Factorize resource names completion functions in new util/completion
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-riccardi committed Oct 27, 2022
1 parent 8115edc commit e8f52f2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
25 changes: 2 additions & 23 deletions pkg/kubectl-argo-rollouts/cmd/get/get_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"github.com/juju/ansiterm"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/argoproj/argo-rollouts/pkg/apiclient/rollout"
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/cmd/signals"
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/info"
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/options"
completionutil "github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/util/completion"
"github.com/argoproj/argo-rollouts/pkg/kubectl-argo-rollouts/viewcontroller"
)

Expand Down Expand Up @@ -76,28 +76,7 @@ func NewCmdGetRollout(o *options.ArgoRolloutsOptions) *cobra.Command {
}
return nil
},
ValidArgsFunction: 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
},
ValidArgsFunction: completionutil.RolloutNameCompletionFunc(o),
}
cmd.Flags().BoolVarP(&getOptions.Watch, "watch", "w", false, "Watch live updates to the rollout")
cmd.Flags().BoolVar(&getOptions.NoColor, "no-color", false, "Do not colorize output")
Expand Down
37 changes: 37 additions & 0 deletions pkg/kubectl-argo-rollouts/util/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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
}
}

0 comments on commit e8f52f2

Please sign in to comment.