-
Notifications
You must be signed in to change notification settings - Fork 72
/
completions.go
83 lines (67 loc) · 2.94 KB
/
completions.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package flagutil
import (
"context"
"github.com/redhat-developer/app-services-cli/pkg/api/rbac"
"github.com/redhat-developer/app-services-cli/pkg/api/rbac/rbacutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)
var (
cachedServiceAccounts []string
)
// EnableStaticFlagCompletion enables autocompletion for flags with predefined valid values
func EnableStaticFlagCompletion(cmd *cobra.Command, flagName string, validValues []string) {
_ = cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return validValues, cobra.ShellCompDirectiveNoSpace
})
}
// EnableOutputFlagCompletion enables autocompletion for output flag
func EnableOutputFlagCompletion(cmd *cobra.Command) {
_ = cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return ValidOutputFormats, cobra.ShellCompDirectiveNoSpace
})
}
// RegisterUserCompletionFunc adds the user list to flag dynamic completion
func RegisterUserCompletionFunc(cmd *cobra.Command, flagName string, f *factory.Factory) error {
return cmd.RegisterFlagCompletionFunc(flagName, func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var usernames []string
directive := cobra.ShellCompDirectiveNoSpace
conn, err := f.Connection()
if err != nil {
return usernames, directive
}
queryParams := []rbac.QueryParam{rbac.WithQueryParam("match_criteria", "partial"), rbac.WithQueryParam("usernames", toComplete)}
principals, err := rbacutil.FetchAllUsers(context.Background(), conn.API().RBAC().PrincipalAPI, queryParams...)
if err != nil || len(principals) == 0 {
return usernames, directive
}
for _, p := range principals {
usernames = append(usernames, p.Username)
}
return usernames, directive
})
}
// RegisterServiceAccountCompletionFunc adds the service account list to flag dynamic completion
func RegisterServiceAccountCompletionFunc(cmd *cobra.Command, f *factory.Factory) error {
return cmd.RegisterFlagCompletionFunc("service-account", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var emptyList []string
directive := cobra.ShellCompDirectiveNoSpace
conn, err := f.Connection()
if err != nil {
return emptyList, directive
}
// There is no server-side pagination for service accounts, so we will always
// have the full list after the first time
if len(cachedServiceAccounts) > 0 {
return cachedServiceAccounts, directive
}
serviceAccounts, _, err := conn.API().ServiceAccountMgmt().GetServiceAccounts(cmd.Context()).Execute()
if err != nil || len(serviceAccounts) == 0 {
return emptyList, directive
}
for _, serviceAcct := range serviceAccounts {
cachedServiceAccounts = append(cachedServiceAccounts, serviceAcct.GetClientId())
}
return cachedServiceAccounts, directive
})
}