File: README.md

package info (click to toggle)
golang-github-audriusbutkevicius-recli 0.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 128 kB
  • sloc: makefile: 2
file content (390 lines) | stat: -rw-r--r-- 8,652 bytes parent folder | download
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
recli - Reflection based CLI (command line interface) generator for Golang
--------------------------------------------------------------------------

[![GoDoc](https://godoc.org/github.com/AudriusButkevicius/recli?status.svg)](https://godoc.org/github.com/AudriusButkevicius/recli)

For a given struct, builds a set of [urfave/cli](https://github.com/urfave/cli) commands which allows you
to modify it from the command line.

Useful for generating command line clients for your application configuration that is stored in a Go struct.

## Features

* Nested struct support
* Enum/Custom complex type support via MarshalText/UnmarshalText
* Slice support, including complex types
* Slice indexing by struct field
* Map support
* Default primitive value support when adding items to slices

## Known limitations

* Adding new struct to a slice only allows setting primitive fields (use add-json as a work-around)
* Only primitive types supported for map keys and values
* No defaults for maps


## Examples

Example config

```go
type Config struct {
	Address          string `usage:"Address on which to listen"` // Description printed in -help
	AuthMode         AuthMode                                    // Enum support
	ThreadingOptions ThreadingOptions                            // Nested struct support
	Backends         []Backend                                   // Slice support
	EnvVars          map[string]string                           // Map support
}

type Backend struct {
	Hostname         string `recli:"id"`         // Constructs commands for indexing into the array based on the value of this field
	Port             int    `default:"2019"`     // Default support
	BackoffIntervals []int  `default:"10,20"`    // Slice default support
	IPAddressCached  net.IP `recli:"-" json:"-"` // Skips the field
}

type ThreadingOptions struct {
	MaxThreads int
}
```

Sample input data
```json
{
   "Address":"http://website.com",
   "AuthMode":"static",
   "ThreadingOptions":{
      "MaxThreads":10
   },
   "Backends":[
      {
         "Hostname":"backend1.com",
         "Port":1010
      },
      {
         "Hostname":"backend2.com",
         "Port":2020
      }
   ],
   "EnvVars":{
      "CC":"/usr/bin/gcc"
   }
}
```

<details>
 <summary>Full example code</summary>

```go
package main

import (
	"encoding/json"
	"fmt"
	"net"
	"os"

	"github.com/AudriusButkevicius/recli"
	"github.com/urfave/cli"
)

type Config struct {
	Address          string `usage:"Address on which to listen"` // Description printed in -help
	AuthMode         AuthMode                                    // Enum support
	ThreadingOptions ThreadingOptions                            // Nested struct support
	Backends         []Backend                                   // Slice support
	EnvVars          map[string]string                           // Map support
}

type Backend struct {
	Hostname         string `recli:"id"`         // Constructs commands for indexing into the array based on the value of this field
	Port             int    `default:"2019"`     // Default support
	BackoffIntervals []int  `default:"10,20"`    // Slice default support
	IPAddressCached  net.IP `recli:"-" json:"-"` // Skips the field
}

type ThreadingOptions struct {
	MaxThreads int
}

type AuthMode int

const (
	AuthModeStatic AuthMode = iota // default is static
	AuthModeLDAP
)

func (t AuthMode) MarshalText() ([]byte, error) {
	switch t {
	case AuthModeStatic:
		return []byte("static"), nil
	case AuthModeLDAP:
		return []byte("ldap"), nil
	}
	return nil, fmt.Errorf("unknown value: %s", t)
}

func (t *AuthMode) UnmarshalText(bs []byte) error {
	switch string(bs) {
	case "ldap":
		*t = AuthModeLDAP
	case "static":
		*t = AuthModeStatic
	default:
		return fmt.Errorf("unknown value: %s", string(bs))
	}
	return nil
}

const (
	sampleData = `
{
   "Address":"http://website.com",
   "AuthMode":"static",
   "ThreadingOptions":{
      "MaxThreads":10
   },
   "Backends":[
      {
         "Hostname":"backend1.com",
         "Port":1010
      },
      {
         "Hostname":"backend2.com",
         "Port":2020
      }
   ],
   "EnvVars":{
      "CC":"/usr/bin/gcc"
   }
}`
)

func main() {
	cfg := &Config{}

	if err := json.Unmarshal([]byte(sampleData), cfg); err != nil {
		panic(err)
	}

	cmds, err := recli.Default.Construct(cfg)
	if err != nil {
		panic(err)
	}

	dump := false

	app := cli.NewApp()
	app.Commands = cmds
	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:        "dump",
			Destination: &dump,
		},
	}

	if err := app.Run(os.Args); err != nil {
		panic(err)
	}

	if dump {
		bs, err := json.MarshalIndent(&cfg, "", "    ")
		if err != nil {
			panic(err)
		}

		fmt.Print(string(bs))
	}
}
```
</details>

<details>
 <summary>Get a field</summary>

```bash
$ go run main.go address get
http://website.com
```
</details>

<details>
 <summary>Set a field</summary>

```bash
$ go run main.go -dump address set foo
{
    "Address": "foo",
    "AuthMode": "static",
    "ThreadingOptions": {
        "MaxThreads": 10
    },
    "Backends": [
        {
            "Hostname": "backend1.com",
            "Port": 1010,
            "BackoffIntervals": null
        },
        {
            "Hostname": "backend2.com",
            "Port": 2020,
            "BackoffIntervals": null
        }
    ],
    "EnvVars": {
        "CC": "/usr/bin/gcc"
    }
}
```
</details>

<details>
 <summary>Set a nested field</summary>

```bash
$ go run main.go -dump threading-options max-threads set 9000
{
    "Address": "http://website.com",
    "AuthMode": "static",
    "ThreadingOptions": {
        "MaxThreads": 9000
    },
    "Backends": [
        {
            "Hostname": "backend1.com",
            "Port": 1010,
            "BackoffIntervals": null
        },
        {
            "Hostname": "backend2.com",
            "Port": 2020,
            "BackoffIntervals": null
        }
    ],
    "EnvVars": {
        "CC": "/usr/bin/gcc"
    }
}
```
</details>

<details>
 <summary>Listing available slice items (with a custom slice index key)</summary>

```bash
$ go run main.go backends
NAME:
   main.exe backends -

USAGE:
   main.exe backends command [command options] [arguments...]

COMMANDS:
  ACTIONS:
     add           Add a new item to collection
     add-json      Add a new item to collection deserialised from JSON

  ITEMS:
     backend1.com
     backend2.com
     
OPTIONS:
   --help, -h  show help

```
</details>

<details>
 <summary>Deleting a slice item</summary>

```bash
$ go run main.go -dump backends backend1.com delete
{
    "Address": "http://website.com",
    "AuthMode": "static",
    "ThreadingOptions": {
        "MaxThreads": 10
    },
    "Backends": [
        {
            "Hostname": "backend2.com",
            "Port": 2020,
            "BackoffIntervals": null
        }
    ],
    "EnvVars": {
        "CC": "/usr/bin/gcc"
    }
}
```
</details>

<details>
 <summary>Adding a slice item (with defaults)</summary>

```bash
$ go run main.go -dump backends add -hostname="testback.end"
{
    "Address": "http://website.com",
    "AuthMode": "static",
    "ThreadingOptions": {
        "MaxThreads": 10
    },
    "Backends": [
        {
            "Hostname": "backend1.com",
            "Port": 1010,
            "BackoffIntervals": null
        },
        {
            "Hostname": "backend2.com",
            "Port": 2020,
            "BackoffIntervals": null
        },
        {
            "Hostname": "testback.end",
            "Port": 2019,
            "BackoffIntervals": [
                10,
                20
            ]
        }
    ],
    "EnvVars": {
        "CC": "/usr/bin/gcc"
    }
}
```
</details>

<details>
 <summary>Setting map keys</summary>

```bash
$ go run main.go -dump env-vars set GCC /usr/bin/true
{
    "Address": "http://website.com",
    "AuthMode": "static",
    "ThreadingOptions": {
        "MaxThreads": 10
    },
    "Backends": [
        {
            "Hostname": "backend1.com",
            "Port": 1010,
            "BackoffIntervals": null
        },
        {
            "Hostname": "backend2.com",
            "Port": 2020,
            "BackoffIntervals": null
        }
    ],
    "EnvVars": {
        "CC": "/usr/bin/gcc",
        "GCC": "/usr/bin/true"
    }
}
```
</details>