completion: add custom completion for 'get clusters'

This commit is contained in:
iwilltry42 2020-06-05 08:54:27 +02:00
parent 76b7450b02
commit 17c1fccd9c
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110

View File

@ -58,6 +58,7 @@ func NewCmdGetCluster() *cobra.Command {
clusters := buildClusterList(cmd.Context(), args)
PrintClusters(clusters, clusterFlags)
},
ValidArgsFunction: validArgsFunc,
}
// add flags
@ -70,6 +71,31 @@ func NewCmdGetCluster() *cobra.Command {
return cmd
}
// validArgsFunc is used for shell completion: proposes the list of existing clusters
func validArgsFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var completions []string
var clusters []*k3d.Cluster
clusters, err := k3cluster.GetClusters(context.Background(), runtimes.SelectedRuntime)
if err != nil {
log.Errorln("Failed to get list of clusters for shell completion")
return nil, cobra.ShellCompDirectiveError
}
clusterLoop:
for _, cluster := range clusters {
for _, arg := range args {
if arg == cluster.Name { // only clusters, that are not in the args yet
continue clusterLoop
}
}
if strings.HasPrefix(cluster.Name, toComplete) {
completions = append(completions, cluster.Name)
}
}
return completions, cobra.ShellCompDirectiveDefault
}
func buildClusterList(ctx context.Context, args []string) []*k3d.Cluster {
var clusters []*k3d.Cluster
var err error