-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
101 lines (91 loc) · 3.36 KB
/
config.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
package main
import (
"fmt"
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util"
"github.com/BytemarkHosting/bytemark-client/util/log"
"github.com/urfave/cli"
"strings"
)
func init() {
commands = append(commands, cli.Command{
Name: "config",
Usage: "manage the bytemark client's configuration",
UsageText: "config [ set | unset ]",
Description: `view and manipulate the bytemark-client configuration
When invoked with no subcommand, outputs the current values of all variables and what source they were derived from.
The set and unset subcommands can be used to set and unset such variables.
Available variables:
account - the default account, used when you do not explicitly state an account - defaults to the same as your user name
token - the token used for authentication
user - the user that you log in as by default
group - the default group, used when you do not explicitly state a group (defaults to 'default')
debug-level - the default debug level. Set to 0 unless you like lots of output.
auth-endpoint - the endpoint to authenticate to. https://auth.bytemark.co.uk is the default.
endpoint - the brain endpoint to connect to. https://uk0.bigv.io is the default.
billing-endpoint - the billing API endpoint to connect to. https://bmbilling.bytemark.co.uk is the default.
spp-endpoint - the SPP endpoint to use. https://spp-submissions.bytemark.co.uk is the default.`,
Subcommands: []cli.Command{
{
Name: "set",
UsageText: "bytemark config set <variable> <value>",
Usage: "sets a bytemark client configuration request",
Description: "Sets the named variable to the given value. See `bytemark help config` for which variables are available",
Action: With(func(ctx *Context) error {
varname, err := ctx.NextArg()
if err != nil {
return err
}
varname = strings.ToLower(varname)
if !util.IsConfigVar(varname) {
return ctx.Help(fmt.Sprintf("%s is not a valid variable name", varname))
}
oldVar, err := global.Config.GetV(varname)
if err != nil {
return err
}
value, err := ctx.NextArg()
if err != nil {
return err
}
err = global.Config.SetPersistent(varname, value, "CMD set")
if err != nil {
return err
}
if oldVar.Source == "config" {
log.Logf("%s has been changed.\r\nOld value: %s\r\nNew value: %s\r\n", varname, oldVar.Value, global.Config.GetIgnoreErr(varname))
} else {
log.Logf("%s has been set. \r\nNew value: %s\r\n", varname, global.Config.GetIgnoreErr(varname))
}
return nil
}),
}, {
Name: "unset",
UsageText: "bytemark config unset <variable>",
Usage: "unsets a bytemark client configuration option",
Description: "Unsets the named variable.",
Action: With(func(ctx *Context) error {
varname, err := ctx.NextArg()
if err != nil {
return err
}
varname = strings.ToLower(varname)
return global.Config.Unset(varname)
}),
},
},
Action: func(ctx *cli.Context) error {
if ctx.Bool("help") {
cli.ShowSubcommandHelp(ctx)
return nil
}
vars, err := global.Config.GetAll()
if err != nil {
return err
}
for _, v := range vars {
log.Logf("%s\t: '%s' (%s)\r\n", v.Name, v.Value, v.Source)
}
return nil
},
})
}