-
Notifications
You must be signed in to change notification settings - Fork 880
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factorize resource names completion functions in new util/completion
- Loading branch information
1 parent
8115edc
commit e8f52f2
Showing
2 changed files
with
39 additions
and
23 deletions.
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
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 | ||
} | ||
} |