Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] - Don't escape special characters when printing JSON #7593

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@

### Bug Fixes

- [sdk/go] - Fix target and replace options for the Automation API
- [sdk/go] - Fix target and replace options for the Automation API.
[#7426](https://github.com/pulumi/pulumi/pull/7426)

- [cli] - Don't escape special characters when printing JSON.
[#7593](https://github.com/pulumi/pulumi/pull/7593)
3 changes: 1 addition & 2 deletions pkg/cmd/pulumi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,11 +791,10 @@ func listConfig(stack backend.Stack, showSecrets bool, jsonOut bool) error {

configValues[key.String()] = entry
}
out, err := json.MarshalIndent(configValues, "", " ")
err := printJSON(configValues)
if err != nil {
return err
}
fmt.Println(string(out))
} else {
rows := []cmdutil.TableRow{}
for _, key := range keys {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/pulumi/stack_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ func TestStringifyOutput(t *testing.T) {
"baz": true,
},
}
specialChar := "pass&word"

assert.Equal(t, "42", stringifyOutput(num))
assert.Equal(t, "ABC", stringifyOutput(str))
assert.Equal(t, "[\"hello\",\"goodbye\"]", stringifyOutput(arr))
assert.Equal(t, "{\"bar\":{\"baz\":true},\"foo\":42}", stringifyOutput(obj))
assert.Equal(t, "pass&word", stringifyOutput(specialChar))
}
15 changes: 13 additions & 2 deletions pkg/cmd/pulumi/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,24 @@ func (cancellationScopeSource) NewScope(events chan<- engine.Event, isPreview bo
return c
}

func makeJSONString(v interface{}) (string, error) {
var out bytes.Buffer
encoder := json.NewEncoder(&out)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(v); err != nil {
return "", err
}
return out.String(), nil
}

// printJSON simply prints out some object, formatted as JSON, using standard indentation.
func printJSON(v interface{}) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: perhaps put "indent" in the name of the func to further emphasize what it does.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function already existed and is used in many places in the CLI I'd rather not change them all here :)

out, err := json.MarshalIndent(v, "", " ")
jsonStr, err := makeJSONString(v)
if err != nil {
return err
}
fmt.Println(string(out))
fmt.Print(jsonStr)
return nil
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/cmd/pulumi/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,38 @@ func TestReadingGitLabMetadata(t *testing.T) {
assertEnvValue(t, test, backend.VCSRepoKind, gitutil.GitLabHostName)
}
}

func Test_makeJSONString(t *testing.T) {
tests := []struct {
name string
input interface{}
expected string
}{
{
"simple-string",
map[string]interface{}{"my_password": "password"},
`{
"my_password": "password"
}
`},
{
"special-char-string",
map[string]interface{}{"special_password": "pass&word"},
`{
"special_password": "pass&word"
}
`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeJSONString(tt.input)
if err != nil {
t.Errorf("makeJSONString() error = %v", err)
return
}
if got != tt.expected {
t.Errorf("makeJSONString() got = %v, expected %v", got, tt.expected)
}
})
}
}