-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apptypes.go
414 lines (368 loc) · 7.18 KB
/
apptypes.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package cmd
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"time"
"git.parallelcoin.io/dev/9/cmd/nine"
)
type App struct {
Name string
Tagline string
About string
Version func() string
Default func(ctx *App) int
Cats Cats
Commands Commands
Config *nine.Config
}
func (app *App) SaveConfig() {
if app == nil {
return
}
datadir, ok := app.Cats["app"]["datadir"].Value.Get().(string)
if !ok {
return
}
configFile := CleanAndExpandPath(filepath.Join(
datadir, "config"), "")
if EnsureDir(configFile) {
}
fh, err := os.Create(configFile)
if err != nil {
panic(err)
}
j, e := json.MarshalIndent(app, "", "\t")
if e != nil {
panic(e)
}
_, err = fmt.Fprint(fh, string(j))
if err != nil {
panic(err)
}
}
type Line struct {
Value interface{} `json:"value"`
Default interface{} `json:"default,omitempty"`
Min int `json:"min,omitempty"`
Max int `json:"max,omitempty"`
Usage string `json:"usage"`
}
type CatJSON map[string]Line
type CatsJSON map[string]CatJSON
func (r *App) MarshalJSON() ([]byte, error) {
out := make(CatsJSON)
for i, x := range r.Cats {
out[i] = make(CatJSON)
for j, y := range x {
min, _ := y.Min.Get().(int)
max, _ := y.Max.Get().(int)
out[i][j] = Line{
Value: y.Value.Get(),
Default: y.Default.Get(),
Min: min,
Max: max,
Usage: y.Usage,
}
}
}
return json.Marshal(out)
}
func (r *App) UnmarshalJSON(data []byte) error {
out := make(CatsJSON)
e := json.Unmarshal(data, &out)
if e != nil {
return e
}
for i, x := range out {
for j, y := range x {
R := r.Cats[i][j]
if y.Value != nil {
switch R.Type {
case "int", "port":
y.Value = int(y.Value.(float64))
case "duration":
y.Value = time.Duration(int(y.Value.(float64)))
case "stringslice":
rt, ok := y.Value.([]string)
ro := []string{}
if ok {
for _, z := range rt {
R.Validate(R, z)
ro = append(ro, z)
}
// R.Value.Put(ro)
}
// break
case "float":
}
}
R.Validate(R, y.Value)
// R.Value.Put(y.Value)
}
}
return nil
}
type AppGenerator func(ctx *App)
type AppGenerators []AppGenerator
func (r *AppGenerators) RunAll(app *App) {
for _, x := range *r {
x(app)
}
return
}
type Cats map[string]Cat
func (r *Cats) getValue(cat, item string) (out *interface{}) {
if r == nil {
return
} else if C, ok := (*r)[cat]; !ok {
return
} else if cc, ok := C[item]; !ok {
return
} else {
o := cc.Value.Get()
return &o
}
}
// Str returns the pointer to a value in the category map
func (r *Cats) Str(cat, item string) (out *string) {
cv := r.getValue(cat, item)
if cv == nil {
return
}
CC := *cv
if ci, ok := CC.(string); !ok {
return
} else {
return &ci
}
}
// Tags returns the pointer to a value in the category map
func (r *Cats) Tags(cat, item string) (out *[]string) {
cv := r.getValue(cat, item)
if cv == nil {
return
}
CC := *cv
if ci, ok := CC.([]string); !ok {
return
} else {
return &ci
}
}
// Map returns the pointer to a value in the category map
func (r *Cats) Map(cat, item string) (out *nine.Mapstringstring) {
cv := r.getValue(cat, item)
if cv == nil {
return
}
CC := *cv
if ci, ok := CC.(nine.Mapstringstring); !ok {
return
} else {
return &ci
}
}
// Int returns the pointer to a value in the category map
func (r *Cats) Int(cat, item string) (out *int) {
cv := r.getValue(cat, item)
if cv == nil {
return
}
CC := *cv
if ci, ok := CC.(int); !ok {
return
} else {
return &ci
}
}
// Bool returns the pointer to a value in the category map
func (r *Cats) Bool(cat, item string) (out *bool) {
cv := r.getValue(cat, item)
if cv == nil {
return
}
CC := *cv
if ci, ok := CC.(bool); !ok {
return
} else {
return &ci
}
}
// Float returns the pointer to a value in the category map
func (r *Cats) Float(cat, item string) (out *float64) {
cv := r.getValue(cat, item)
if cv == nil {
return
}
CC := *cv
if ci, ok := CC.(float64); !ok {
return
} else {
return &ci
}
}
// Duration returns the pointer to a value in the category map
func (r *Cats) Duration(cat, item string) (out *time.Duration) {
cv := r.getValue(cat, item)
if cv == nil {
return
}
CC := *cv
if ci, ok := CC.(time.Duration); !ok {
return
} else {
return &ci
}
}
type Cat map[string]*Row
type CatGenerator func(ctx *Cat)
type CatGenerators []CatGenerator
func (r *CatGenerators) RunAll(cat Cat) {
for _, x := range *r {
x(&cat)
}
return
}
type Iface struct {
Data *interface{}
}
func NewIface() *Iface {
return &Iface{Data: new(interface{})}
}
func (i *Iface) Get() interface{} {
if i == nil {
return nil
}
if i.Data == nil {
return nil
}
return *i.Data
}
func (i *Iface) Put(in interface{}) *Iface {
if i == nil {
i = NewIface()
}
if i.Data == nil {
i.Data = new(interface{})
}
*i.Data = in
return i
}
type Row struct {
Name string
Type string
Opts []string
Value *Iface
Default *Iface
Min *Iface
Max *Iface
Init func(*Row)
Get func() interface{}
Put func(interface{}) bool
Validate func(*Row, interface{}) bool
String string
Usage string
App *App
}
func (r *Row) Bool() bool {
return r.Value.Get().(bool)
}
func (r *Row) Int() int {
return r.Value.Get().(int)
}
func (r *Row) Float() float64 {
return r.Value.Get().(float64)
}
func (r *Row) Duration() time.Duration {
return r.Value.Get().(time.Duration)
}
func (r *Row) Tag() string {
return r.Value.Get().(string)
}
func (r *Row) Tags() []string {
return r.Value.Get().([]string)
}
type RowGenerator func(ctx *Row)
type RowGenerators []RowGenerator
func (r *RowGenerators) RunAll(row *Row) {
for _, x := range *r {
x(row)
}
}
// Token is a struct that ties together CLI invocation to the Command it
// relates to
type Token struct {
Value string
Cmd Command
}
type Tokens map[string]Token
type Optional []string
type Precedent []string
type CommandHandler func(args []string, tokens Tokens, app *App) int
type Command struct {
Name string
Pattern string
RE *regexp.Regexp
Short string
Detail string
Opts Optional
Precedent Precedent
Handler CommandHandler
}
type CommandGenerator func(ctx *Command)
type CommandGenerators []CommandGenerator
type Commands map[string]*Command
func (r *CommandGenerators) RunAll() *Command {
c := &Command{}
for _, x := range *r {
x(c)
}
return c
}
// GetSortedKeys returns the keys of a map in alphabetical order
func (r *CatJSON) GetSortedKeys() (out []string) {
for i := range *r {
out = append(out, i)
}
sort.Strings(out)
return
}
func (r *CatsJSON) GetSortedKeys() (out []string) {
for i := range *r {
out = append(out, i)
}
sort.Strings(out)
return
}
func (r *Cats) GetSortedKeys() (out []string) {
for i := range *r {
out = append(out, i)
}
sort.Strings(out)
return
}
func (r Cat) GetSortedKeys() (out []string) {
for i := range r {
out = append(out, i)
}
sort.Strings(out)
return
}
func (r *Tokens) GetSortedKeys() (out []string) {
for i := range *r {
out = append(out, i)
}
sort.Strings(out)
return
}
func (r *Commands) GetSortedKeys() (out []string) {
for i := range *r {
out = append(out, i)
}
sort.Strings(out)
return
}