-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_key.go
213 lines (175 loc) · 5.86 KB
/
api_key.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cmd
import (
"fmt"
"github.com/rockset/rockset-go-client/openapi"
"github.com/rockset/rockset-go-client/option"
"github.com/spf13/cobra"
"github.com/rockset/cli/completion"
"github.com/rockset/cli/config"
"github.com/rockset/cli/flag"
"github.com/rockset/cli/format"
"github.com/rockset/cli/sort"
)
// TODO when implementing create apikey:
// the role should have auto-completion for the list of roles the user has access to
func NewListAPIKeysCmd() *cobra.Command {
cmd := cobra.Command{
Use: "apikeys [USER]",
Aliases: []string{"ak", "api", "apikey"},
Args: cobra.RangeArgs(0, 1),
Short: "list apikeys for the current user, or the specified USER",
Annotations: group("apikey"),
ValidArgsFunction: completion.Alias(Version),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
logger.Info("args", "args", args)
var opts []option.APIKeyOption
if len(args) > 0 {
opts = append(opts, option.ForUser(args[0]))
}
keys, err := rs.ListAPIKeys(ctx, opts...)
if err != nil {
return err
}
ms := sort.Multi[openapi.ApiKey]{
LessFuncs: []func(p1 *openapi.ApiKey, p2 *openapi.ApiKey) bool{
sort.ByName[*openapi.ApiKey],
},
}
ms.Sort(keys)
return formatList(cmd, format.ToInterfaceArray(keys))
},
}
return &cmd
}
func NewGetAPIKeyCmd() *cobra.Command {
cmd := cobra.Command{
Use: "apikey NAME",
Aliases: []string{"ak"},
Args: cobra.ExactArgs(1),
Short: "get apikey information",
Annotations: group("apikey"),
ValidArgsFunction: completion.Alias(Version),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
var options []option.APIKeyOption
if email, _ := cmd.Flags().GetString(flag.Email); email != "" {
options = append(options, option.ForUser(email))
}
apikey, err := rs.GetAPIKey(ctx, args[0], options...)
if err != nil {
return err
}
return formatOne(cmd, apikey)
},
}
cmd.Flags().String(flag.Email, "", "the email address of the user who's key to get, defaults to self")
_ = cmd.RegisterFlagCompletionFunc(flag.Email, completion.Alias(Version))
return &cmd
}
func NewDeleteAPIKeyCmd() *cobra.Command {
cmd := cobra.Command{
Use: "apikey NAME",
Aliases: []string{"ak"},
Args: cobra.ExactArgs(1),
Short: "delete an apikey",
Annotations: group("apikey"),
ValidArgsFunction: completion.Alias(Version),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
var options []option.APIKeyOption
if email, _ := cmd.Flags().GetString(flag.Email); email != "" {
options = append(options, option.ForUser(email))
}
err = rs.DeleteAPIKey(ctx, args[0], options...)
if err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "apikey %s deleted\n", args[0])
return nil
},
}
cmd.Flags().String(flag.Email, "", "the email address of the user who's key to delete, defaults to self")
_ = cmd.RegisterFlagCompletionFunc(flag.Email, completion.Alias(Version))
return &cmd
}
func NewCreateAPIKeyCmd() *cobra.Command {
cmd := cobra.Command{
Use: "apikey NAME",
Aliases: []string{"ak"},
Args: cobra.ExactArgs(1),
Short: "create apikey",
Annotations: group("apikey"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
var options []option.APIKeyRoleOption
if role, _ := cmd.Flags().GetString(flag.Role); role != "" {
options = append(options, option.WithRole(role))
}
apikey, err := rs.CreateAPIKey(ctx, args[0], options...)
if err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "apikey %s created\n", apikey.GetName())
return nil
},
}
cmd.Flags().String(flag.Role, "", "role for the apikey")
_ = cmd.RegisterFlagCompletionFunc(flag.Role, completion.Role(Version))
return &cmd
}
func newUpdateAPIKeyCmd() *cobra.Command {
cmd := cobra.Command{
Use: "apikey NAME",
Aliases: []string{"ak"},
Args: cobra.ExactArgs(1),
Short: "update the state of an apikey",
Annotations: group("apikey"),
ValidArgsFunction: completion.Alias(Version),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
var options []option.APIKeyOption
if email, _ := cmd.Flags().GetString(flag.Email); email != "" {
options = append(options, option.ForUser(email))
}
state, _ := cmd.Flags().GetString(flag.State)
options = append(options, option.State(option.KeyState(state)))
apikey, err := rs.UpdateAPIKey(ctx, args[0], options...)
if err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "apikey %s updated to %s\n", apikey.GetName(), apikey.GetState())
return nil
},
}
cmd.Flags().String(flag.Email, "", "the email address of the user who's key to update, defaults to self")
_ = cmd.RegisterFlagCompletionFunc(flag.Email, completion.Alias(Version))
cmd.Flags().String(flag.State, "",
fmt.Sprintf("the state of the apikey, either %s or %s", option.KeyActive, option.KeySuspended))
_ = cmd.RegisterFlagCompletionFunc(flag.State,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{string(option.KeyActive), string(option.KeySuspended)}, cobra.ShellCompDirectiveNoFileComp
})
_ = cobra.MarkFlagRequired(cmd.Flags(), flag.State)
return &cmd
}