-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcmd.go
409 lines (338 loc) · 8.67 KB
/
cmd.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package cli
import (
"context"
"errors"
"github.com/MakeNowJust/heredoc"
"github.com/anchordotdev/cli/stacktrace"
"github.com/anchordotdev/cli/ui"
"github.com/spf13/cobra"
)
type CmdDef struct {
Name string
Aliases []string
Args cobra.PositionalArgs
Long string
Short string
Use string
SubDefs []CmdDef
}
var rootDef = CmdDef{
Name: "anchor",
Use: "anchor <command> <subcommand> [flags]",
Args: cobra.NoArgs,
Short: "anchor - a CLI interface for anchor.dev",
Long: heredoc.Doc(`
anchor is a command line interface for the Anchor certificate management platform.
It provides a developer friendly interface for certificate management.
`),
SubDefs: []CmdDef{
{
Name: "auth",
Use: "auth [flags]",
Args: cobra.NoArgs,
Short: "Manage Anchor.dev Authentication",
SubDefs: []CmdDef{
{
Name: "signin",
Use: "signin [flags]",
Args: cobra.NoArgs,
Short: "Authenticate With Your Account",
Long: heredoc.Doc(`
Sign into your Anchor account for your local system user.
Generate a new Personal Access Token (PAT) and store it in the system keychain
for the local system user.
`),
},
{
Name: "signout",
Use: "signout [flags]",
Args: cobra.NoArgs,
Short: "Invalidate Local Anchor Session",
Long: heredoc.Doc(`
Sign out of your Anchor account for your local system user.
Remove your Personal Access Token (PAT) from the system keychain for your local
system user.
`),
},
{
Name: "whoami",
Use: "whoami [flags]",
Args: cobra.NoArgs,
Short: "Identify Current Anchor.dev Account",
Long: heredoc.Doc(`
Print the details of the Anchor account for your local system user.
`),
},
},
},
{
Name: "lcl",
Use: "lcl [flags]",
Args: cobra.NoArgs,
Short: "Manage lcl.host Local Development Environment",
SubDefs: []CmdDef{
{
Name: "audit",
Use: "audit [flags]",
Args: cobra.NoArgs,
Short: "Audit lcl.host HTTPS Local Development Environment",
},
{
Name: "bootstrap",
Aliases: []string{"config"},
Use: "bootstrap [flags]",
Args: cobra.NoArgs,
Short: "Initial System Configuration for lcl.host Local Development",
},
{
Name: "clean",
Use: "clean [flags]",
Args: cobra.NoArgs,
Short: "Clean lcl.host CA Certificates from the Local Trust Store(s)",
},
{
Name: "env",
Use: "env [flags]",
Args: cobra.NoArgs,
Short: "Generate lcl.host ENV",
},
{
Name: "mkcert",
Use: "mkcert [flags]",
Args: cobra.NoArgs,
Short: "Provision Certificate for lcl.host Local Development",
},
{
Name: "setup",
Use: "setup [flags]",
Args: cobra.NoArgs,
Short: "Setup lcl.host Application",
},
{
Name: "trust",
Use: "trust [flags]",
Args: cobra.NoArgs,
Short: "Install CA Certificates for lcl.host Local Development",
},
},
},
{
Name: "org",
Use: "org [flags]",
Args: cobra.NoArgs,
Short: "Manage Organizations",
SubDefs: []CmdDef{
{
Name: "create",
Use: "create [flags]",
Args: cobra.NoArgs,
Short: "Create New Organization",
},
},
},
{
Name: "service",
Use: "service [flags]",
Args: cobra.NoArgs,
Short: "Manage services",
SubDefs: []CmdDef{
{
Name: "env",
Use: "env [flags]",
Args: cobra.NoArgs,
Short: "Fetch Environment Variables for Service",
},
{
Name: "verify",
Use: "verify <service> [flags]",
Args: cobra.NoArgs,
Short: "Verify Service TLS Setup and Configuration",
},
},
},
{
Name: "trust",
Use: "trust [flags]",
Args: cobra.NoArgs,
Short: "Manage CA Certificates in your Local Trust Store(s)",
Long: heredoc.Doc(`
Install the AnchorCA certificates of a target organization, realm, or CA into
your local system's trust store. The default target is the localhost realm of
your personal organization.
After installation of the AnchorCA certificates, Leaf certificates under the
AnchorCA certificates will be trusted by browsers and programs on your system.
`),
SubDefs: []CmdDef{
{
Name: "audit",
Use: "audit [flags]",
Args: cobra.NoArgs,
Short: "Audit CA Certificates in Your Local Trust Store(s)",
Long: heredoc.Doc(`
Perform an audit of the local trust store(s) and report any expected, missing,
or extra CA certificates per store. A set of expected CAs is fetched for the
target org and (optional) realm. The default stores to audit are system, nss,
and homebrew.
CA certificate states:
* VALID: an expected CA certificate is present in every trust store.
* MISSING: an expected CA certificate is missing in one or more stores.
* EXTRA: an unexpected CA certificate is present in one or more stores.
`),
},
{
Name: "clean",
Use: "clean [flags]",
Args: cobra.NoArgs,
Short: "Clean CA Certificates from your Local Trust Store(s)",
},
},
},
{
Name: "version",
Use: "version",
Args: cobra.NoArgs,
Short: "Show Version Info",
SubDefs: []CmdDef{
{
Name: "upgrade",
Use: "upgrade",
Args: cobra.NoArgs,
Short: "Check for Upgrade",
},
},
},
},
}
var cmdDefByCommands = map[*cobra.Command]*CmdDef{}
var constructorByCommands = map[*cobra.Command]func() *cobra.Command{}
type UIer interface {
UI() UI
}
func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command)) *cobra.Command {
var def, parentDef *CmdDef
if parent != nil {
var ok bool
if parentDef, ok = cmdDefByCommands[parent]; !ok {
panic("unregistered parent command")
}
for _, sub := range parentDef.SubDefs {
if sub.Name == name {
def = &sub
break
}
}
if def == nil {
panic("missing subcommand definition")
}
} else {
def = &rootDef
}
constructor := func() *cobra.Command {
cmd := &cobra.Command{
Aliases: def.Aliases,
Args: def.Args,
Long: def.Long,
Short: def.Short,
Use: def.Use,
SilenceUsage: true,
}
cmd.SetErrPrefix(ui.Danger("Error!"))
ctx := ContextWithConfig(context.Background(), defaultConfig())
cmd.SetContext(ctx)
fn(cmd)
cmd.RunE = func(cmd *cobra.Command, args []string) (returnedError error) {
ctx := cmd.Context()
cfg := new(Config)
if err := cfg.Load(ctx); err != nil {
return err
}
if cfg.Test.SkipRunE {
return nil
}
ctx = ContextWithConfig(cmd.Context(), cfg)
cmd.SetContext(ctx)
var t T
switch any(t).(type) {
case ShowHelp:
cmd.HelpFunc()(cmd, args)
return nil
}
ctx, cancel := context.WithCancelCause(cmd.Context())
defer cancel(nil)
ctx = ContextWithCalledAs(ctx, cmd.CalledAs())
drv, prg := ui.NewDriverTUI(ctx)
defer func() {
// release/restore
drv.Program.Quit()
drv.Program.Wait()
}()
errc := make(chan error)
go func() {
defer close(errc)
_, err := prg.Run()
cancel(err)
errc <- err
}()
if err := stacktrace.CapturePanic(func() error { return t.UI().RunTUI(ctx, drv) }); err != nil {
var uierr ui.Error
if errors.As(err, &uierr) {
drv.Activate(context.Background(), uierr.Model)
err = uierr.Err
}
if err != nil && isReportable(err) {
ReportError(ctx, err, drv, cmd, args)
}
return err
}
drv.Program.Quit()
if err := <-errc; err != nil {
if isReportable(err) {
ReportError(ctx, err, drv, cmd, args)
}
return err
}
return nil
}
new(cmdWrapper).wrap(cmd)
return cmd
}
cmd := constructor()
if parent != nil {
parent.AddCommand(cmd)
}
cmdDefByCommands[cmd] = def
constructorByCommands[cmd] = constructor
return cmd
}
func NewTestCmd(cmd *cobra.Command) *cobra.Command {
return constructorByCommands[cmd]()
}
type cmdWrapper struct {
err error
}
func (w *cmdWrapper) wrap(cmd *cobra.Command) {
if fn := cmd.PersistentPreRunE; fn != nil {
cmd.PersistentPreRunE = w.runFunc(fn)
}
if fn := cmd.PreRunE; fn != nil {
cmd.PreRunE = w.runFunc(fn)
}
if fn := cmd.RunE; fn != nil {
cmd.RunE = w.runFunc(fn)
}
if fn := cmd.PostRunE; fn != nil {
cmd.PostRunE = w.runFunc(fn)
}
if fn := cmd.PersistentPostRunE; fn != nil {
cmd.PersistentPostRunE = w.runFunc(fn)
}
}
func (w *cmdWrapper) runFunc(fn func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
if w.err != nil {
return w.err
}
w.err = fn(cmd, args)
return w.err
}
}