-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd.go
312 lines (253 loc) · 6.9 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
package cli
import (
"context"
"fmt"
"runtime/debug"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/anchordotdev/cli/ui"
"github.com/joeshaw/envdecode"
"github.com/mcuadros/go-defaults"
"github.com/spf13/cobra"
)
type CmdDef struct {
Name string
Use string
Args cobra.PositionalArgs
Short string
Long 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 <subcommand> [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: "clean",
Use: "clean [flags]",
Args: cobra.NoArgs,
Short: "Clean lcl.host CA Certificates from the Local Trust Store(s)",
},
{
Name: "config",
Use: "config [flags]",
Args: cobra.NoArgs,
Short: "Configure System for lcl.host Local Development",
},
{
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: "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",
},
},
}
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 {
cfg := new(Config)
defaults.SetDefaults(cfg)
if err := envdecode.Decode(cfg); err != nil && err != envdecode.ErrNoTargetFieldsAreSet {
panic(err)
}
cmd := &cobra.Command{
Use: def.Use,
Args: def.Args,
Short: def.Short,
Long: def.Long,
SilenceUsage: true,
}
ctx := ContextWithConfig(context.Background(), cfg)
cmd.SetContext(ctx)
cmd.SetErrPrefix(ui.Error("Error!"))
fn(cmd)
cmd.RunE = func(cmd *cobra.Command, args []string) (returnedError error) {
cfg := ConfigFromCmd(cmd)
if cfg.Test.SkipRunE {
return nil
}
var t T
switch any(t).(type) {
case ShowHelp:
cmd.HelpFunc()(cmd, args)
return nil
}
ctx, cancel := context.WithCancelCause(cmd.Context())
defer cancel(nil)
drv, prg := ui.NewDriverTUI(ctx)
errc := make(chan error)
go func() {
defer close(errc)
_, err := prg.Run()
cancel(err)
errc <- err
}()
defer Cleanup(&returnedError, errc, ctx, drv, cmd, args)
if err := t.UI().RunTUI(ctx, drv); err != nil && err != context.Canceled {
return err
}
return nil
}
return cmd
}
cmd := constructor()
if parent != nil {
parent.AddCommand(cmd)
}
cmdDefByCommands[cmd] = def
constructorByCommands[cmd] = constructor
return cmd
}
func Cleanup(returnedError *error, errc chan error, ctx context.Context, drv *ui.Driver, cmd *cobra.Command, args []string) {
var stack string
panicMsg := recover()
if panicMsg != nil {
lines := strings.Split(string(debug.Stack()), "\n")
// omit lines: 0 go routine, 2-3 stack call, 4-5 cleanup call
stack = strings.Join(lines[5:], "\n")
*returnedError = fmt.Errorf("%s", panicMsg)
}
if *returnedError != nil {
ReportError(ctx, drv, cmd, args, *returnedError, stack)
}
// release/restore
drv.Program.Quit()
drv.Program.Wait()
// FIXME: handle UI errors?
if errc != nil {
*returnedError = <-errc
}
}
func NewTestCmd(cmd *cobra.Command) *cobra.Command {
return constructorByCommands[cmd]()
}