-
Notifications
You must be signed in to change notification settings - Fork 928
/
env.go
166 lines (138 loc) · 4.43 KB
/
env.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
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
package application
import (
"encoding/json"
"sort"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/api/applications"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type Env struct {
ui terminal.UI
config core_config.Reader
appRepo applications.ApplicationRepository
}
func NewEnv(ui terminal.UI, config core_config.Reader, appRepo applications.ApplicationRepository) (cmd *Env) {
cmd = new(Env)
cmd.ui = ui
cmd.config = config
cmd.appRepo = appRepo
return
}
func (cmd *Env) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "env",
ShortName: "e",
Description: T("Show all env variables for an app"),
Usage: T("CF_NAME env APP"),
}
}
func (cmd *Env) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) ([]requirements.Requirement, error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}
return []requirements.Requirement{requirementsFactory.NewLoginRequirement()}, nil
}
func (cmd *Env) Run(c *cli.Context) {
app, err := cmd.appRepo.Read(c.Args()[0])
if notFound, ok := err.(*errors.ModelNotFoundError); ok {
cmd.ui.Failed(notFound.Error())
}
cmd.ui.Say(T("Getting env variables for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
map[string]interface{}{
"AppName": terminal.EntityNameColor(app.Name),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
"Username": terminal.EntityNameColor(cmd.config.Username())}))
env, err := cmd.appRepo.ReadEnv(app.Guid)
if err != nil {
cmd.ui.Failed(err.Error())
}
cmd.ui.Ok()
cmd.ui.Say("")
cmd.displaySystemiAndAppProvidedEnvironment(env.System, env.Application)
cmd.ui.Say("")
cmd.displayUserProvidedEnvironment(env.Environment)
cmd.ui.Say("")
cmd.displayRunningEnvironment(env.Running)
cmd.ui.Say("")
cmd.displayStagingEnvironment(env.Staging)
cmd.ui.Say("")
}
func (cmd *Env) displaySystemiAndAppProvidedEnvironment(env map[string]interface{}, app map[string]interface{}) {
var vcapServices string
var vcapApplication string
servicesAsMap, ok := env["VCAP_SERVICES"].(map[string]interface{})
if ok && len(servicesAsMap) > 0 {
jsonBytes, err := json.MarshalIndent(env, "", " ")
if err != nil {
cmd.ui.Failed(err.Error())
}
vcapServices = string(jsonBytes)
}
applicationAsMap, ok := app["VCAP_APPLICATION"].(map[string]interface{})
if ok && len(applicationAsMap) > 0 {
jsonBytes, err := json.MarshalIndent(app, "", " ")
if err != nil {
cmd.ui.Failed(err.Error())
}
vcapApplication = string(jsonBytes)
}
if len(vcapServices) == 0 && len(vcapApplication) == 0 {
cmd.ui.Say(T("No system-provided env variables have been set"))
return
}
cmd.ui.Say(terminal.EntityNameColor(T("System-Provided:")))
cmd.ui.Say(vcapServices)
cmd.ui.Say("")
cmd.ui.Say(vcapApplication)
}
func (cmd *Env) displayUserProvidedEnvironment(envVars map[string]interface{}) {
if len(envVars) == 0 {
cmd.ui.Say(T("No user-defined env variables have been set"))
return
}
keys := make([]string, 0, len(envVars))
for key, _ := range envVars {
keys = append(keys, key)
}
sort.Strings(keys)
cmd.ui.Say(terminal.EntityNameColor(T("User-Provided:")))
for _, key := range keys {
cmd.ui.Say("%s: %v", key, envVars[key])
}
}
func (cmd *Env) displayRunningEnvironment(envVars map[string]interface{}) {
if len(envVars) == 0 {
cmd.ui.Say(T("No running env variables have been set"))
return
}
keys := make([]string, 0, len(envVars))
for key, _ := range envVars {
keys = append(keys, key)
}
sort.Strings(keys)
cmd.ui.Say(terminal.EntityNameColor(T("Running Environment Variable Groups:")))
for _, key := range keys {
cmd.ui.Say("%s: %v", key, envVars[key])
}
}
func (cmd *Env) displayStagingEnvironment(envVars map[string]interface{}) {
if len(envVars) == 0 {
cmd.ui.Say(T("No staging env variables have been set"))
return
}
keys := make([]string, 0, len(envVars))
for key, _ := range envVars {
keys = append(keys, key)
}
sort.Strings(keys)
cmd.ui.Say(terminal.EntityNameColor(T("Staging Environment Variable Groups:")))
for _, key := range keys {
cmd.ui.Say("%s: %v", key, envVars[key])
}
}