forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
103 lines (85 loc) · 3.02 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
102
103
package commands
import (
"fmt"
"sort"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/simonleung8/flags"
"github.com/simonleung8/flags/flag"
. "github.com/cloudfoundry/cli/cf/i18n"
)
type ConfigCommands struct {
ui terminal.UI
config core_config.ReadWriter
}
func init() {
command_registry.Register(&ConfigCommands{})
}
func (cmd *ConfigCommands) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["async-timeout"] = &cliFlags.IntFlag{Name: "async-timeout", Usage: T("Timeout for async HTTP requests")}
fs["trace"] = &cliFlags.StringFlag{Name: "trace", Usage: T("Trace HTTP requests")}
fs["color"] = &cliFlags.StringFlag{Name: "color", Usage: T("Enable or disable color")}
fs["locale"] = &cliFlags.StringFlag{Name: "locale", Usage: T("Set default locale. If LOCALE is CLEAR, previous locale is deleted.")}
return command_registry.CommandMetadata{
Name: "config",
Description: T("write default values to the config"),
Usage: T("CF_NAME config [--async-timeout TIMEOUT_IN_MINUTES] [--trace true | false | path/to/file] [--color true | false] [--locale (LOCALE | CLEAR)]"),
Flags: fs,
}
}
func (cmd *ConfigCommands) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
return nil, nil
}
func (cmd *ConfigCommands) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
return cmd
}
func (cmd *ConfigCommands) Execute(context flags.FlagContext) {
if !context.IsSet("trace") && !context.IsSet("async-timeout") && !context.IsSet("color") && !context.IsSet("locale") {
cmd.ui.Failed(T("Incorrect Usage\n\n") + command_registry.Commands.CommandUsage("config"))
return
}
if context.IsSet("async-timeout") {
asyncTimeout := context.Int("async-timeout")
if asyncTimeout < 0 {
cmd.ui.Failed(T("Incorrect Usage\n\n") + command_registry.Commands.CommandUsage("config"))
}
cmd.config.SetAsyncTimeout(uint(asyncTimeout))
}
if context.IsSet("trace") {
cmd.config.SetTrace(context.String("trace"))
}
if context.IsSet("color") {
value := context.String("color")
switch value {
case "true":
cmd.config.SetColorEnabled("true")
case "false":
cmd.config.SetColorEnabled("false")
default:
cmd.ui.Failed(T("Incorrect Usage\n\n") + command_registry.Commands.CommandUsage("config"))
}
}
if context.IsSet("locale") {
locale := context.String("locale")
if locale == "CLEAR" {
cmd.config.SetLocale("")
return
}
if IsSupportedLocale(locale) {
cmd.config.SetLocale(locale)
return
}
cmd.ui.Say(fmt.Sprintf("Could not find locale '%s'. The known locales are:", locale))
cmd.ui.Say("")
supportedLocales := SupportedLocales()
sort.Strings(supportedLocales)
for i := range supportedLocales {
cmd.ui.Say(supportedLocales[i])
}
}
}