This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
348 lines (286 loc) · 8.32 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package omnictl
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"
"github.com/siderolabs/gen/maps"
"github.com/spf13/cobra"
"github.com/siderolabs/omni-client/pkg/omnictl/config"
"github.com/siderolabs/omni-client/pkg/omnictl/internal/access"
)
// configCmd represents the config command.
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage the client configuration file (omniconfig)",
Long: ``,
}
// configURLCmd represents the `config url` command.
var configURLCmd = &cobra.Command{
Use: "url <url>",
Short: "Set the URL for the current context",
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, false)
if err != nil {
return err
}
context, err := conf.GetContext(access.CmdFlags.Context)
if err != nil {
return err
}
context.URL = args[0]
return conf.Save()
},
}
// configBasicAuthCmd represents the `config basic-auth` command.
var configBasicAuthCmd = &cobra.Command{
Use: "basic-auth <username> <password>",
Short: "Set the basic auth credentials",
Long: ``,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, false)
if err != nil {
return err
}
context, err := conf.GetContext(access.CmdFlags.Context)
if err != nil {
return err
}
context.Auth.Basic = args[0] + ":" + args[1]
return conf.Save()
},
}
// configIdentityCmd represents the `config identity` command.
var configIdentityCmd = &cobra.Command{
Use: "identity <identity>",
Short: "Set the auth identity for the current context",
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, false)
if err != nil {
return err
}
context, err := conf.GetContext(access.CmdFlags.Context)
if err != nil {
return err
}
context.Auth.SideroV1.Identity = args[0]
return conf.Save()
},
}
// configContextCmd represents the `config context` command.
var configContextCmd = &cobra.Command{
Use: "context <context>",
Short: "Set the current context",
Aliases: []string{"use-context"},
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, false)
if err != nil {
return err
}
context := args[0]
conf.Context = context
return conf.Save()
},
ValidArgsFunction: CompleteConfigContext,
}
// configAddCmdFlags represents the `config add` command flags.
var configAddCmdFlags struct {
url string
httpURL string
identity string
basicAuth string
}
// configAddCmd represents the `config add` command.
var configAddCmd = &cobra.Command{
Use: "add <context>",
Short: "Add a new context",
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, true)
if err != nil {
return err
}
name := args[0]
_, alreadyExists := conf.Contexts[name]
if alreadyExists {
return fmt.Errorf("context %s already exists", name)
}
newContext := config.Context{
URL: configAddCmdFlags.url,
Auth: config.Auth{
Basic: configAddCmdFlags.basicAuth,
SideroV1: config.SideroV1{
Identity: configAddCmdFlags.identity,
},
},
}
conf.Contexts[name] = &newContext
return conf.Save()
},
}
// configGetContextsCmd represents the `config contexts` command.
var configGetContextsCmd = &cobra.Command{
Use: "contexts",
Short: "List defined contexts",
Aliases: []string{"get-contexts"},
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, false)
if err != nil {
return err
}
keys := maps.Keys(conf.Contexts)
sort.Strings(keys)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
defer w.Flush() //nolint:errcheck
_, err = fmt.Fprintln(w, "CURRENT\tNAME\tURL")
if err != nil {
return err
}
for _, name := range keys {
context := conf.Contexts[name]
var current string
if name == conf.Context {
current = "*"
}
_, err = fmt.Fprintf(w, "%s\t%s\t%s\n", current, name, context.URL)
if err != nil {
return err
}
}
return nil
},
}
// configMergeCmd represents the `config merge` command.
var configMergeCmd = &cobra.Command{
Use: "merge <from>",
Short: "Merge additional contexts from another client configuration file",
Long: "Contexts with the same name are renamed while merging configs.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, true)
if err != nil {
return err
}
renames, err := conf.Merge(args[0])
if err != nil {
return err
}
for _, rename := range renames {
fmt.Printf("renamed omniconfig context %s\n", rename.String())
}
return conf.Save()
},
}
// configNewCmdFlags represents the `config new` command flags.
var configNewCmdFlags struct {
url string
httpURL string
basicAuth string
identity string
}
// configNewCmd represents the `config new` command.
var configNewCmd = &cobra.Command{
Use: "new [<path>]",
Short: "Generate a new client configuration file",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
path := ""
if len(args) > 0 {
path = args[0]
}
conf, err := config.Init(path, true)
if err != nil {
return err
}
context, err := conf.GetContext(access.CmdFlags.Context)
if err != nil {
return err
}
context.URL = configNewCmdFlags.url
context.Auth.Basic = configNewCmdFlags.basicAuth
context.Auth.SideroV1.Identity = configNewCmdFlags.identity
return conf.Save()
},
}
// configInfoCmdTemplate represents the `config info` command output template.
var configInfoCmdTemplate = template.Must(template.New("configInfoCmdTemplate").
Option("missingkey=error").
Parse(strings.TrimSpace(`
Current context: {{ .Context }}
URL: {{ .APIURL }}
Identity: {{ .Identity }}
`)))
// configInfoCmd represents the `config info` command.
var configInfoCmd = &cobra.Command{
Use: "info",
Short: "Show information about the current context",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Init(access.CmdFlags.Omniconfig, false)
if err != nil {
return err
}
var result string
context, err := conf.GetContext(access.CmdFlags.Context)
if err != nil {
return err
}
var buf bytes.Buffer
err = configInfoCmdTemplate.Execute(&buf, map[string]string{
"Context": conf.Context,
"APIURL": context.URL,
"Identity": context.Auth.SideroV1.Identity,
})
if err != nil {
return err
}
result = buf.String() + "\n"
fmt.Print(result)
return nil
},
}
// CompleteConfigContext represents tab completion for `--context` argument and `config context` command.
func CompleteConfigContext(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
conf, err := config.Init(access.CmdFlags.Omniconfig, false)
if err != nil {
return nil, 0
}
contextNames := maps.Keys(conf.Contexts)
sort.Strings(contextNames)
return contextNames, cobra.ShellCompDirectiveNoFileComp
}
func init() {
configCmd.AddCommand(
configURLCmd,
configIdentityCmd,
configBasicAuthCmd,
configContextCmd,
configAddCmd,
configGetContextsCmd,
configMergeCmd,
configNewCmd,
configInfoCmd,
)
configAddCmd.Flags().StringVar(&configAddCmdFlags.url, "url", config.DefaultContext.URL, "URL of the server")
configAddCmd.Flags().StringVar(&configAddCmdFlags.basicAuth, "basic-auth", "", "basic auth credentials")
configAddCmd.Flags().StringVar(&configAddCmdFlags.identity, "identity", "", "identity to use for authentication")
configNewCmd.Flags().StringVar(&configNewCmdFlags.url, "url", config.DefaultContext.URL, "URL of the server")
configNewCmd.Flags().StringVar(&configNewCmdFlags.basicAuth, "basic-auth", "", "basic auth credentials")
configNewCmd.Flags().StringVar(&configNewCmdFlags.identity, "identity", "", "identity to use for authentication")
RootCmd.AddCommand(configCmd)
}