/
tenetcommands.go
99 lines (85 loc) · 1.75 KB
/
tenetcommands.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
package commands
import (
flg "flag"
"fmt"
)
type tenetCommand struct {
Name string
// A short description of the usage of this command
Usage string
// A longer explanation of how the command works
Description string
Flags flg.FlagSet // TODO: Can this be private once drivers are in?
}
func tenetCommands() []*tenetCommand {
return []*tenetCommand{
reviewCMD(),
helpCMD(),
infoCMD(),
descriptionCMD(),
versionCMD(),
}
}
func reviewCMD() *tenetCommand {
cmd := &tenetCommand{
Name: "review",
Usage: "review target code against this tenet",
}
cmd.addFlags(
Flag{"options", "{}", "json serialized tenet options"},
)
return cmd
}
func descriptionCMD() *tenetCommand {
return &tenetCommand{
Name: "description",
Usage: "gives extended information about this tenet",
}
}
func helpCMD() *tenetCommand {
return &tenetCommand{
Name: "help",
Usage: "show help",
}
}
func infoCMD() *tenetCommand {
return &tenetCommand{
Name: "info",
Usage: "show information about this tenet",
}
}
func versionCMD() *tenetCommand {
return &tenetCommand{
Name: "version",
Usage: "diplays the version of this tenet",
}
}
type Flag struct {
Name string
Value interface{}
Usage string
}
func (c *tenetCommand) GetFlag(name string) string {
f := c.Flags.Lookup(name)
if f == nil {
panic(fmt.Sprintf("flag %q not set", name))
}
return f.Value.String()
}
func (c *tenetCommand) addFlag(f Flag) *tenetCommand {
switch v := f.Value.(type) {
case bool:
c.Flags.Bool(f.Name, v, f.Usage)
case string:
c.Flags.String(f.Name, v, f.Usage)
default:
panic(fmt.Sprintf("unrecognised flag value type: %T", v))
}
return c
}
func (c *tenetCommand) addFlags(flgs ...Flag) *tenetCommand {
for _, f := range flgs {
c.addFlag(f)
}
return c
}