-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
202 lines (168 loc) · 4.97 KB
/
config.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
package cmd
import (
"errors"
"fmt"
"io"
"os"
"sort"
"github.com/rockset/rockset-go-client"
"github.com/spf13/cobra"
"github.com/rockset/cli/config"
"github.com/rockset/cli/flag"
)
// config is the file containing the auth contexts
func newListContextsCmd() *cobra.Command {
cmd := cobra.Command{
Use: "contexts",
Aliases: []string{"context", "ctx"},
Annotations: group("context"),
Args: cobra.NoArgs,
Short: "list authentication contexts",
Long: fmt.Sprintf(`list authentication contexts and show the currently selected one. YAML file located in %s of the format`, config.FileName),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("config file %s not readable available: %v", config.FileName, err)
}
return err
}
// TODO redo as tui
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Available Authentication Contexts:\n")
if len(cfg.Keys) > 0 {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "apikeys:\n")
listContext(cmd.OutOrStdout(), cfg.Current, cfg.Keys)
}
if len(cfg.Tokens) > 0 {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "bearer tokens:\n")
listContext(cmd.OutOrStdout(), cfg.Current, cfg.Tokens)
}
return nil
},
}
return &cmd
}
type apiserver interface {
APIServer() string
}
func listContext[T apiserver](out io.Writer, current string, m map[string]T) {
var names []string
for name := range m {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
var arrow = " "
if current == name {
arrow = "->"
}
_, _ = fmt.Fprintf(out, "%s %s (%s)\n", arrow, name, m[name].APIServer())
}
}
func newDeleteContextCmd() *cobra.Command {
cmd := cobra.Command{
Use: "context NAME",
Aliases: []string{"ctx"},
Short: "delete an authentication context",
Annotations: group("context"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return err
}
if err = cfg.DeleteContext(args[0]); err != nil {
return err
}
if err = config.Store(cfg); err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "context %s deleted\n", args[0])
return nil
},
}
return &cmd
}
func newCreateContextCmd() *cobra.Command {
cmd := cobra.Command{
Use: "context NAME",
Aliases: []string{"ctx"},
Short: "create a new authentication context",
Long: "create a new authentication context and save it in the config file",
Annotations: group("context"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
key, _ := cmd.Flags().GetString("apikey")
server, _ := cmd.Flags().GetString("server")
force, _ := cmd.Flags().GetBool(flag.Force)
if key == "" || server == "" {
// TODO open up a form
return fmt.Errorf("both --apikey and --server are required")
}
cfg, err := config.Load()
if err != nil {
return err
}
// use --force to add anyway
if _, found := cfg.Keys[args[0]]; found {
if force {
logger.Info("context already exist, adding anyway")
} else {
return fmt.Errorf("configuration %s already exists", args[0])
}
}
if !force {
rs, err := rockset.NewClient(rockset.WithAPIKey(key), rockset.WithAPIServer(server))
if err != nil {
return fmt.Errorf("failed to create Rockset client using the new credentials: %v", err)
}
org, err := rs.GetOrganization(cmd.Context())
if err != nil {
return fmt.Errorf("failed to get org info using the new credentials: %v", err)
}
logger.Info("connected to Rockset", "org", org.DisplayName)
} else {
logger.Warn("skipping auth check due to --force option")
}
cfg.Keys[args[0]] = config.APIKey{
Key: key,
Server: server,
}
if err = config.Store(cfg); err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "config %s created\n", args[0])
return nil
},
}
cmd.Flags().String("server", "", "api server name")
cmd.Flags().String("apikey", "", "apikey")
cmd.Flags().Bool(flag.Force, false, "force add the context even if the name exists or the credentials can't be used connect to the API server")
return &cmd
}
func newUseContextCmd() *cobra.Command {
cmd := cobra.Command{
Use: "context NAME",
Aliases: []string{"ctx"},
Short: "use auth context",
Long: "use authentication context",
Annotations: group("context"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return err
}
// TODO if len(args) == it should open a tui and let the user select
if err = cfg.Use(args[0]); err != nil {
return err
}
if err = config.Store(cfg); err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "using config %s\n", args[0])
return nil
},
}
return &cmd
}