This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
prefs.go
70 lines (64 loc) · 1.95 KB
/
prefs.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
package cli
import (
"fmt"
"github.com/spf13/cobra"
)
func init() {
addRegistrar(registerPref)
}
func registerPref(app *cobra.Command) {
tree := addPrefCommands()
app.AddCommand(tree)
}
func addPrefCommands() (res *cobra.Command) {
res = &cobra.Command{
Use: "prefs",
Short: "List and set DigitalRebar Provision operational preferences",
}
commands := []*cobra.Command{}
commands = append(commands, &cobra.Command{
Use: "list",
Short: "List all preferences",
RunE: func(c *cobra.Command, args []string) error {
prefs := map[string]string{}
if err := Session.Req().UrlFor("prefs").Do(&prefs); err != nil {
return generateError(err, "Error listing prefs")
}
return prettyPrint(prefs)
},
})
prefsMap := map[string]string{}
commands = append(commands, &cobra.Command{
Use: "set [- | JSON or YAML Map of strings | pairs of string args]",
Short: "Set preferences",
Long: `WARNING! Changing baseTokenSecret or systemGrantorSecret
will invalidate existing sessions and tokens. This will require users to
reauthenticate and may cause active WSS sessions to hang. Do not change
these values unless you are able to reset all client sessions.`,
Args: func(c *cobra.Command, args []string) error {
prefsMap = map[string]string{}
if len(args) == 1 {
if err := into(args[0], &prefsMap); err != nil {
return fmt.Errorf("Invalid prefs: %v\n", err)
}
return nil
}
if len(args) != 0 && len(args)%2 == 0 {
for i := 0; i < len(args); i += 2 {
prefsMap[args[i]] = args[i+1]
}
return nil
}
return fmt.Errorf("prefs set either takes a single argument or a multiple of two, not %d", len(args))
},
RunE: func(c *cobra.Command, args []string) error {
prefs := map[string]string{}
if err := Session.Req().Post(prefsMap).UrlFor("prefs").Do(&prefs); err != nil {
return generateError(err, "Error setting prefs")
}
return prettyPrint(prefs)
},
})
res.AddCommand(commands...)
return res
}