generated from cybozu-go/neco-template
/
completion.go
49 lines (41 loc) · 1.14 KB
/
completion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cmd
import (
"context"
"strings"
mocov1beta2 "github.com/cybozu-go/moco/api/v1beta2"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func mysqlClusterCandidates(ctx context.Context, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
var namespace string
if cmd.Flags().Changed("namespace") {
ns, err := cmd.Flags().GetString("namespace")
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
namespace = ns
} else {
ns, _, err := kubeConfigFlags.ToRawKubeConfigLoader().Namespace()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
namespace = ns
}
clusters := &mocov1beta2.MySQLClusterList{}
if err := kubeClient.List(ctx, clusters, &client.ListOptions{
Namespace: namespace,
}); err != nil {
return nil, cobra.ShellCompDirectiveError
}
var candidates []string
for _, c := range clusters.Items {
if !strings.HasPrefix(c.Name, toComplete) {
continue
}
candidates = append(candidates, c.Name)
}
return candidates, cobra.ShellCompDirectiveNoFileComp
}