Skip to content

Commit d5e38dd

Browse files
authored
Do not template/translate literal values in environment variable output (cloudfoundry#3011)
1 parent 74d5d07 commit d5e38dd

6 files changed

Lines changed: 93 additions & 1 deletion

File tree

command/commandfakes/fake_ui.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

command/ui.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type UI interface {
3030
DisplayPasswordPrompt(template string, templateValues ...map[string]interface{}) (string, error)
3131
DisplayTableWithHeader(prefix string, table [][]string, padding int)
3232
DisplayText(template string, data ...map[string]interface{})
33+
DisplayTextLiteral(text string)
3334
DisplayTextMenu(choices []string, promptTemplate string, templateValues ...map[string]interface{}) (string, error)
3435
DisplayTextPrompt(template string, templateValues ...map[string]interface{}) (string, error)
3536
DisplayTextWithBold(text string, keys ...map[string]interface{})

command/v7/env_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (cmd EnvCommand) displayEnvGroup(group map[string]interface{}) {
9393
keys := sortKeys(group)
9494

9595
for _, key := range keys {
96-
cmd.UI.DisplayText(fmt.Sprintf("%s: %v", key, group[key]))
96+
cmd.UI.DisplayTextLiteral(fmt.Sprintf("%s: %v", key, group[key]))
9797
}
9898
}
9999

command/v7/env_command_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,42 @@ var _ = Describe("env Command", func() {
232232
Expect(testUI.Err).To(Say("get-warning-2"))
233233
})
234234
})
235+
236+
When("getting the environment returns env vars with special templating characters", func() {
237+
BeforeEach(func() {
238+
envGroups := v7action.EnvironmentVariableGroups{
239+
System: map[string]interface{}{"system-name": map[string]interface{}{"mysql": []string{"system-value"}, "password": "{{test<3"}},
240+
Application: map[string]interface{}{"application-name": "{{application-value"},
241+
EnvironmentVariables: map[string]interface{}{"user-name": "{{user-value"},
242+
Running: map[string]interface{}{"running-name": "{{running-value"},
243+
Staging: map[string]interface{}{"staging-name": "{{staging-value"},
244+
}
245+
fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(envGroups, nil, nil)
246+
})
247+
248+
It("displays the environment variable and value pair", func() {
249+
Expect(executeErr).ToNot(HaveOccurred())
250+
251+
Expect(testUI.Out).To(Say(`Getting env variables for app some-app in org some-org / space some-space as banana\.\.\.`))
252+
Expect(testUI.Out).To(Say("System-Provided:"))
253+
Expect(testUI.Out).To(Say("system-name: {"))
254+
Expect(testUI.Out).To(Say(`"mysql": \[`))
255+
Expect(testUI.Out).To(Say(`"system-value"`))
256+
Expect(testUI.Out).To(Say(`\],`))
257+
Expect(testUI.Out).To(Say(`"password": "{{test<3"`))
258+
Expect(testUI.Out).To(Say("}"))
259+
Expect(testUI.Out).To(Say(`application-name: "{{application-value"`))
260+
261+
Expect(testUI.Out).To(Say("User-Provided:"))
262+
Expect(testUI.Out).To(Say(`user-name: {{user-value`))
263+
264+
Expect(testUI.Out).To(Say("Running Environment Variable Groups:"))
265+
Expect(testUI.Out).To(Say(`running-name: {{running-value`))
266+
267+
Expect(testUI.Out).To(Say("Staging Environment Variable Groups:"))
268+
Expect(testUI.Out).To(Say(`staging-name: {{staging-value`))
269+
})
270+
})
235271
})
236272
})
237273
})

util/ui/ui.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ func (ui *UI) DisplayText(template string, templateValues ...map[string]interfac
248248
fmt.Fprintf(ui.Out, "%s\n", ui.TranslateText(template, templateValues...))
249249
}
250250

251+
// DisplayTextLiteral outputs the text to ui.Out without modification.
252+
// This function should only be used when no translation or templating is required.
253+
func (ui *UI) DisplayTextLiteral(text string) {
254+
ui.terminalLock.Lock()
255+
defer ui.terminalLock.Unlock()
256+
257+
fmt.Fprintf(ui.Out, "%s\n", text)
258+
}
259+
251260
// DisplayTextWithBold translates the template, bolds the templateValues,
252261
// substitutes templateValues into the template, and outputs
253262
// the result to ui.Out. Only the first map in templateValues is used.

util/ui/ui_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ var _ = Describe("UI", func() {
186186
})
187187
})
188188

189+
Describe("DisplayTextLiteral", func() {
190+
It("displays the text into ui.Out with a newline", func() {
191+
ui.DisplayTextLiteral("some text")
192+
Expect(out).To(Say("some text\n"))
193+
})
194+
})
195+
189196
Describe("Display JSON", func() {
190197
It("displays the indented JSON object", func() {
191198
obj := map[string]interface{}{

0 commit comments

Comments
 (0)