Skip to content

Commit

Permalink
Do not template/translate literal values in environment variable outp…
Browse files Browse the repository at this point in the history
…ut (#3011)
  • Loading branch information
Samze authored Jul 11, 2024
1 parent 74d5d07 commit d5e38dd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
39 changes: 39 additions & 0 deletions command/commandfakes/fake_ui.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions command/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type UI interface {
DisplayPasswordPrompt(template string, templateValues ...map[string]interface{}) (string, error)
DisplayTableWithHeader(prefix string, table [][]string, padding int)
DisplayText(template string, data ...map[string]interface{})
DisplayTextLiteral(text string)
DisplayTextMenu(choices []string, promptTemplate string, templateValues ...map[string]interface{}) (string, error)
DisplayTextPrompt(template string, templateValues ...map[string]interface{}) (string, error)
DisplayTextWithBold(text string, keys ...map[string]interface{})
Expand Down
2 changes: 1 addition & 1 deletion command/v7/env_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (cmd EnvCommand) displayEnvGroup(group map[string]interface{}) {
keys := sortKeys(group)

for _, key := range keys {
cmd.UI.DisplayText(fmt.Sprintf("%s: %v", key, group[key]))
cmd.UI.DisplayTextLiteral(fmt.Sprintf("%s: %v", key, group[key]))
}
}

Expand Down
36 changes: 36 additions & 0 deletions command/v7/env_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,42 @@ var _ = Describe("env Command", func() {
Expect(testUI.Err).To(Say("get-warning-2"))
})
})

When("getting the environment returns env vars with special templating characters", func() {
BeforeEach(func() {
envGroups := v7action.EnvironmentVariableGroups{
System: map[string]interface{}{"system-name": map[string]interface{}{"mysql": []string{"system-value"}, "password": "{{test<3"}},
Application: map[string]interface{}{"application-name": "{{application-value"},
EnvironmentVariables: map[string]interface{}{"user-name": "{{user-value"},
Running: map[string]interface{}{"running-name": "{{running-value"},
Staging: map[string]interface{}{"staging-name": "{{staging-value"},
}
fakeActor.GetEnvironmentVariablesByApplicationNameAndSpaceReturns(envGroups, nil, nil)
})

It("displays the environment variable and value pair", func() {
Expect(executeErr).ToNot(HaveOccurred())

Expect(testUI.Out).To(Say(`Getting env variables for app some-app in org some-org / space some-space as banana\.\.\.`))
Expect(testUI.Out).To(Say("System-Provided:"))
Expect(testUI.Out).To(Say("system-name: {"))
Expect(testUI.Out).To(Say(`"mysql": \[`))
Expect(testUI.Out).To(Say(`"system-value"`))
Expect(testUI.Out).To(Say(`\],`))
Expect(testUI.Out).To(Say(`"password": "{{test<3"`))
Expect(testUI.Out).To(Say("}"))
Expect(testUI.Out).To(Say(`application-name: "{{application-value"`))

Expect(testUI.Out).To(Say("User-Provided:"))
Expect(testUI.Out).To(Say(`user-name: {{user-value`))

Expect(testUI.Out).To(Say("Running Environment Variable Groups:"))
Expect(testUI.Out).To(Say(`running-name: {{running-value`))

Expect(testUI.Out).To(Say("Staging Environment Variable Groups:"))
Expect(testUI.Out).To(Say(`staging-name: {{staging-value`))
})
})
})
})
})
9 changes: 9 additions & 0 deletions util/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ func (ui *UI) DisplayText(template string, templateValues ...map[string]interfac
fmt.Fprintf(ui.Out, "%s\n", ui.TranslateText(template, templateValues...))
}

// DisplayTextLiteral outputs the text to ui.Out without modification.
// This function should only be used when no translation or templating is required.
func (ui *UI) DisplayTextLiteral(text string) {
ui.terminalLock.Lock()
defer ui.terminalLock.Unlock()

fmt.Fprintf(ui.Out, "%s\n", text)
}

// DisplayTextWithBold translates the template, bolds the templateValues,
// substitutes templateValues into the template, and outputs
// the result to ui.Out. Only the first map in templateValues is used.
Expand Down
7 changes: 7 additions & 0 deletions util/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ var _ = Describe("UI", func() {
})
})

Describe("DisplayTextLiteral", func() {
It("displays the text into ui.Out with a newline", func() {
ui.DisplayTextLiteral("some text")
Expect(out).To(Say("some text\n"))
})
})

Describe("Display JSON", func() {
It("displays the indented JSON object", func() {
obj := map[string]interface{}{
Expand Down

0 comments on commit d5e38dd

Please sign in to comment.