-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
107 lines (91 loc) · 2.28 KB
/
main.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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"text/tabwriter"
"github.com/eventstore/es-gencert-cli/certificates"
"github.com/mitchellh/cli"
)
var version = "0.0.0"
func main() {
ui := &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
appName := "Event Store Certificate Generation CLI"
args := os.Args[1:]
c := cli.NewCLI(appName, version)
c.Args = args
flags := flag.NewFlagSet("config", flag.ContinueOnError)
if !c.IsVersion() && !c.IsHelp() {
err := flags.Parse(os.Args[1:])
if err != nil {
ui.Error(err.Error())
}
args = flags.Args()
}
c = cli.NewCLI(appName, version)
c.Args = args
c.Commands = map[string]cli.CommandFactory{
"create-ca": func() (cli.Command, error) {
return certificates.NewCreateCA(&cli.ColoredUi{
Ui: ui,
OutputColor: cli.UiColorBlue,
}), nil
},
"create-node": func() (cli.Command, error) {
return certificates.NewCreateNode(
&cli.ColoredUi{
Ui: ui,
OutputColor: cli.UiColorBlue,
},
), nil
},
"create-certs": func() (cli.Command, error) {
return certificates.NewCreateCerts(&cli.ColoredUi{
Ui: ui,
OutputColor: cli.UiColorBlue,
},
), nil
},
"create-user": func() (cli.Command, error) {
return certificates.NewCreateUser(&cli.ColoredUi{
Ui: ui,
OutputColor: cli.UiColorBlue,
},
), nil
},
}
c.HelpFunc = createGeneralHelpFunc(appName, flags)
exitStatus, err := c.Run()
if err != nil {
log.Println(err)
}
os.Exit(exitStatus)
}
func createGeneralHelpFunc(appName string, flags *flag.FlagSet) cli.HelpFunc {
return func(cf map[string]cli.CommandFactory) string {
buf := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buf, 0, 8, 4, '\t', 0)
fmt.Fprintf(w, "usage: %s [<options>] <command> [<args>]\n\n", appName)
fmt.Fprintln(w, "Available options are:")
fmt.Fprintln(w, "--version\tGet the version of Event Store CLI")
fmt.Fprintln(w, "--help\tDisplay help")
flags.VisitAll(func(fl *flag.Flag) {
fmt.Fprintf(w, "--%s\t%s\n", fl.Name, fl.Usage)
})
fmt.Fprintln(w)
fmt.Fprintln(w, "Available commands are:")
for key, cmdF := range cf {
cmd, _ := cmdF()
fmt.Fprintf(w, "%s\t%s\n", key, cmd.Synopsis())
}
w.Flush()
return buf.String()
}
}