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

Use indentation of 2 when rewriting auto-migrated config file #4312

Merged
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
18 changes: 14 additions & 4 deletions pkg/utils/yaml_utils/yaml_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package yaml_utils

import (
"bytes"
"errors"
"fmt"

Expand Down Expand Up @@ -34,7 +35,7 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
}

// Convert the updated YAML node back to YAML bytes.
updatedYAMLBytes, err := yaml.Marshal(body)
updatedYAMLBytes, err := yamlMarshal(body)
if err != nil {
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
}
Expand Down Expand Up @@ -126,7 +127,7 @@ func TransformNode(yamlBytes []byte, path []string, transform func(node *yaml.No
}

// Convert the updated YAML node back to YAML bytes.
updatedYAMLBytes, err := yaml.Marshal(body)
updatedYAMLBytes, err := yamlMarshal(body)
if err != nil {
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
}
Expand Down Expand Up @@ -170,7 +171,7 @@ func RenameYamlKey(yamlBytes []byte, path []string, newKey string) ([]byte, erro
}

// Convert the updated YAML node back to YAML bytes.
updatedYAMLBytes, err := yaml.Marshal(body)
updatedYAMLBytes, err := yamlMarshal(body)
if err != nil {
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
}
Expand Down Expand Up @@ -227,7 +228,7 @@ func Walk(yamlBytes []byte, callback func(node *yaml.Node, path string) bool) ([
}

// Convert the updated YAML node back to YAML bytes.
updatedYAMLBytes, err := yaml.Marshal(body)
updatedYAMLBytes, err := yamlMarshal(body)
if err != nil {
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
}
Expand Down Expand Up @@ -273,3 +274,12 @@ func walk(node *yaml.Node, path string, callback func(*yaml.Node, string) bool)

return didChange, nil
}

func yamlMarshal(node *yaml.Node) ([]byte, error) {
var buffer bytes.Buffer
encoder := yaml.NewEncoder(&buffer)
encoder.SetIndent(2)

err := encoder.Encode(node)
return buffer.Bytes(), err
}
45 changes: 20 additions & 25 deletions pkg/utils/yaml_utils/yaml_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,19 @@ func TestUpdateYamlValue(t *testing.T) {
expectedErr: "",
},
{
name: "nested update",
in: "foo:\n bar: baz\n",
path: []string{"foo", "bar"},
value: "qux",
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
expectedOut: "foo:\n bar: qux\n",
name: "nested update",
in: "foo:\n bar: baz\n",
path: []string{"foo", "bar"},
value: "qux",
expectedOut: "foo:\n bar: qux\n",
expectedErr: "",
},
{
name: "nested where parents doesn't exist yet",
in: "",
path: []string{"foo", "bar", "baz"},
value: "qux",
expectedOut: "foo:\n bar:\n baz: qux\n",
expectedOut: "foo:\n bar:\n baz: qux\n",
expectedErr: "",
},
{
Expand Down Expand Up @@ -134,21 +133,19 @@ func TestRenameYamlKey(t *testing.T) {
expectedErr: "",
},
{
name: "rename key, nested",
in: "foo:\n bar: 5\n",
path: []string{"foo", "bar"},
newKey: "baz",
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
expectedOut: "foo:\n baz: 5\n",
name: "rename key, nested",
in: "foo:\n bar: 5\n",
path: []string{"foo", "bar"},
newKey: "baz",
expectedOut: "foo:\n baz: 5\n",
expectedErr: "",
},
{
name: "rename non-scalar key",
in: "foo:\n bar: 5\n",
path: []string{"foo"},
newKey: "qux",
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
expectedOut: "qux:\n bar: 5\n",
name: "rename non-scalar key",
in: "foo:\n bar: 5\n",
path: []string{"foo"},
newKey: "qux",
expectedOut: "qux:\n bar: 5\n",
expectedErr: "",
},
{
Expand Down Expand Up @@ -288,8 +285,7 @@ func TestWalk_inPlaceChanges(t *testing.T) {
}
return false
},
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
expectedOut: "x:\n y: 7\n",
expectedOut: "x:\n y: 7\n",
},
{
name: "change array value",
Expand All @@ -301,8 +297,7 @@ func TestWalk_inPlaceChanges(t *testing.T) {
}
return false
},
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
expectedOut: "x:\n - y: 7\n",
expectedOut: "x:\n - y: 7\n",
},
}

Expand Down Expand Up @@ -366,8 +361,8 @@ foo:
path: []string{"foo", "bar"},
transform: transformIntValueToString,
expectedOut: `foo:
bar: "2"
`, // Note the indentiation change and newlines because of how it re-marshalls
bar: "2"
`, // Note the trailing newline changes because of how it re-marshalls
},
{
name: "Does nothing when already transformed",
Expand Down
Loading