-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
183 lines (169 loc) · 3.97 KB
/
cli.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
package main
import (
"fmt"
"github.com/Bedrock-OSS/go-burrito/burrito"
"strconv"
"strings"
)
type flag struct {
Flag
Type string
BoolDestination *bool
StringDestination *string
IntDestination *int64
StringSliceDestination *[]string
Found bool
}
type Flag struct {
Name string
Usage string
Description string
Required bool
OnSet func()
}
type Action struct {
Name string
Hidden bool
Usage string
Description string
Function func(args []string) error
}
type App struct {
flags []flag
actions map[string]Action
}
func (a *App) BoolFlag(f Flag, destination *bool) {
a.flags = append(a.flags, flag{
Flag: f,
Type: "bool",
BoolDestination: destination,
})
}
func (a *App) StringFlag(f Flag, destination *string) {
a.flags = append(a.flags, flag{
Flag: f,
Type: "string",
StringDestination: destination,
})
}
func (a *App) IntFlag(f Flag, destination *int64) {
a.flags = append(a.flags, flag{
Flag: f,
Type: "int",
IntDestination: destination,
})
}
func (a *App) StringSliceFlag(f Flag, destination *[]string) {
a.flags = append(a.flags, flag{
Flag: f,
Type: "stringSlice",
StringSliceDestination: destination,
})
}
func (a *App) Action(action Action) {
if a.actions == nil {
a.actions = make(map[string]Action)
}
a.actions[action.Name] = action
}
func (a *App) Run(args []string, onParse func()) error {
cleanArgs := make([]string, 0)
for i := 1; i < len(args); i++ {
arg := args[i]
if strings.HasPrefix(arg, "--") {
found := false
for _, flag := range a.flags {
if arg == "--"+flag.Name {
found = true
switch flag.Type {
case "bool":
if flag.Found {
return burrito.WrappedErrorf("flag --%s is already set", flag.Name)
}
*flag.BoolDestination = true
flag.Found = true
case "string":
if flag.Found {
return burrito.WrappedErrorf("flag --%s is already set", flag.Name)
}
*flag.StringDestination = args[i+1]
flag.Found = true
i++
break
case "stringSlice":
*flag.StringSliceDestination = append(*flag.StringSliceDestination, args[i+1])
i++
flag.Found = true
break
case "int":
if flag.Found {
return burrito.WrappedErrorf("flag --%s is already set", flag.Name)
}
k, err := strconv.ParseInt(args[i+1], 10, 64)
if err != nil {
return burrito.WrappedErrorf("flag %s is not an integer", args[i+1])
}
*flag.IntDestination = k
flag.Found = true
i++
break
default:
return burrito.WrappedErrorf("unknown flag type: %s", flag.Type)
}
}
}
if !found {
a.PrintHelp()
return burrito.WrappedErrorf("unknown flag: %s", arg)
}
} else {
cleanArgs = append(cleanArgs, arg)
}
}
if onParse != nil {
onParse()
}
for i, f := range a.flags {
if f.Required && !f.Found {
return fmt.Errorf("flag --%s is required", f.Name)
}
if f.Found && f.OnSet != nil {
f.OnSet()
}
a.flags[i] = f
}
if len(cleanArgs) == 0 {
a.PrintHelp()
return nil
}
// This is a fix for regolith, that puts the settings as the first argument
if strings.HasPrefix(cleanArgs[0], "{") {
cleanArgs = cleanArgs[1:]
}
action, ok := a.actions[cleanArgs[0]]
if !ok {
a.PrintHelp()
return burrito.WrappedErrorf("unknown command: %s", cleanArgs[0])
}
return action.Function(cleanArgs[1:])
}
func (a *App) PrintHelp() {
fmt.Println("jsonte - JSON Templating Engine")
fmt.Println("")
fmt.Println("Usage: jsonte <command> [arguments...] [options]")
fmt.Println("")
for _, flag := range a.flags {
fmt.Printf(" --%s\t%s\n", flag.Name, flag.Usage)
}
fmt.Println("")
fmt.Println("Commands:")
for _, action := range a.actions {
if action.Hidden {
continue
}
fmt.Printf(" %s\t%s\n", action.Name, action.Usage)
}
}
func NewApp() *App {
return &App{}
}