/
app.go
73 lines (65 loc) 路 1.5 KB
/
app.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
package cli
import (
"log"
"github.com/mitchellh/cli"
"github.com/moorara/gocert/pki"
)
// App represents a cli app
type App struct {
name string
version string
init cli.Command
root cli.Command
interm cli.Command
server cli.Command
client cli.Command
sign cli.Command
verify cli.Command
}
// NewApp creates a new cli app
func NewApp(name, version string) *App {
return &App{
name: name,
version: version,
init: NewInitCommand(),
root: NewReqCommand(pki.Cert{Type: pki.CertTypeRoot}),
interm: NewReqCommand(pki.Cert{Type: pki.CertTypeInterm}),
server: NewReqCommand(pki.Cert{Type: pki.CertTypeServer}),
client: NewReqCommand(pki.Cert{Type: pki.CertTypeClient}),
sign: NewSignCommand(),
verify: NewVerifyCommand(),
}
}
// Run executes the cli app
func (a *App) Run(args []string) int {
app := cli.NewCLI(a.name, a.version)
app.Args = args
app.Commands = map[string]cli.CommandFactory{
"init": func() (cli.Command, error) {
return a.init, nil
},
"root": func() (cli.Command, error) {
return a.root, nil
},
"intermediate": func() (cli.Command, error) {
return a.interm, nil
},
"server": func() (cli.Command, error) {
return a.server, nil
},
"client": func() (cli.Command, error) {
return a.client, nil
},
"sign": func() (cli.Command, error) {
return a.sign, nil
},
"verify": func() (cli.Command, error) {
return a.verify, nil
},
}
status, err := app.Run()
if err != nil {
log.Println(err)
}
return status
}