forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
99 lines (83 loc) · 2.62 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
package commands
import (
"fmt"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/flag_helpers"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type ConfigCommands struct {
ui terminal.UI
config core_config.ReadWriter
}
func NewConfig(ui terminal.UI, config core_config.ReadWriter) ConfigCommands {
return ConfigCommands{ui: ui, config: config}
}
func (cmd ConfigCommands) Metadata() command_metadata.CommandMetadata {
return command_metadata.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: []cli.Flag{
flag_helpers.NewIntFlag("async-timeout", T("Timeout for async HTTP requests")),
flag_helpers.NewStringFlag("trace", T("Trace HTTP requests")),
flag_helpers.NewStringFlag("color", T("Enable or disable color")),
flag_helpers.NewStringFlag("locale", "Set default locale. If LOCALE is CLEAR, previous locale is deleted."),
},
}
}
func (cmd ConfigCommands) GetRequirements(_ requirements.Factory, _ *cli.Context) ([]requirements.Requirement, error) {
return nil, nil
}
func (cmd ConfigCommands) Run(context *cli.Context) {
if !context.IsSet("trace") && !context.IsSet("async-timeout") && !context.IsSet("color") && !context.IsSet("locale") {
cmd.ui.FailWithUsage(context)
return
}
if context.IsSet("async-timeout") {
asyncTimeout := context.Int("async-timeout")
if asyncTimeout < 0 {
cmd.ui.FailWithUsage(context)
}
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.FailWithUsage(context)
}
}
if context.IsSet("locale") {
locale := context.String("locale")
if locale == "CLEAR" {
cmd.config.SetLocale("")
return
}
foundLocale := false
for _, value := range SUPPORTED_LOCALES {
if value == locale {
cmd.config.SetLocale(locale)
foundLocale = true
break
}
}
if !foundLocale {
cmd.ui.Say(fmt.Sprintf("Could not find locale %s. The known locales are:", locale))
cmd.ui.Say("")
for _, locale := range SUPPORTED_LOCALES {
cmd.ui.Say(locale)
}
}
}
}