Skip to content

Commit 2807861

Browse files
authored
Merge pull request #11 from dailymotion/yaml-indent
feat: yaml indent can now be configured
2 parents 710ea8b + f3f507a commit 2807861

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

update/updater_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ func TestParse(t *testing.T) {
7878
},
7979
{
8080
name: "complex yaml updater",
81-
updates: []string{"yaml(file=values.yaml,path='array.(key==prefix*).**.subkey',trim=true)=1.2.3"},
81+
updates: []string{"yaml(file=values.yaml,path='array.(key==prefix*).**.subkey',trim=true,indent=4)=1.2.3"},
8282
expected: []Updater{
8383
&yaml.YamlUpdater{
8484
FilePath: "values.yaml",
8585
Path: "array.(key==prefix*).**.subkey",
8686
Valuer: value.StringValuer("1.2.3"),
8787
Trim: true,
88+
Indent: 4,
8889
},
8990
},
9091
},

update/yaml/yaml.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type YamlUpdater struct {
2828
AutoCreate bool
2929
Style string
3030
Trim bool
31+
Indent int
3132
Valuer value.Valuer
3233
}
3334

@@ -44,6 +45,11 @@ func NewUpdater(params map[string]string, valuer value.Valuer) (*YamlUpdater, er
4445
return nil, errors.New("missing path parameter")
4546
}
4647

48+
updater.Indent, _ = strconv.Atoi(params["indent"])
49+
if updater.Indent <= 0 {
50+
updater.Indent = 2
51+
}
52+
4753
updater.AutoCreate, _ = strconv.ParseBool(params["create"])
4854
updater.Trim, _ = strconv.ParseBool(params["trim"])
4955
updater.Style = params["style"]
@@ -104,10 +110,18 @@ func (u *YamlUpdater) Update(ctx context.Context, repoPath string) (bool, error)
104110
return false, fmt.Errorf("failed to update YAML file %s: %w", filePath, err)
105111
}
106112

107-
updatedData, err := yaml.Marshal(&rootNode)
113+
var buffer bytes.Buffer
114+
encoder := yaml.NewEncoder(&buffer)
115+
encoder.SetIndent(u.Indent)
116+
err = encoder.Encode(&rootNode)
117+
if err != nil {
118+
return false, fmt.Errorf("failed to encode updated YAML content for %s: %w", filePath, err)
119+
}
120+
err = encoder.Close()
108121
if err != nil {
109-
return false, fmt.Errorf("failed to marshal updated YAML content for %s: %w", filePath, err)
122+
return false, fmt.Errorf("failed to close YAML encoder for %s: %w", filePath, err)
110123
}
124+
updatedData := buffer.Bytes()
111125

112126
if u.Trim {
113127
updatedData = bytes.TrimSpace(updatedData)

update/yaml/yaml_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ func TestNewUpdater(t *testing.T) {
2929
"create": "true",
3030
"style": "double",
3131
"trim": "true",
32+
"indent": "4",
3233
},
3334
expected: &YamlUpdater{
3435
FilePath: "values.yaml",
3536
Path: "level1.level2",
3637
AutoCreate: true,
3738
Style: "double",
3839
Trim: true,
40+
Indent: 4,
3941
},
4042
},
4143
{
@@ -44,12 +46,14 @@ func TestNewUpdater(t *testing.T) {
4446
"file": "values.yaml",
4547
"path": "level1.level2",
4648
"create": "maybe",
49+
"indent": "not-an-int",
4750
},
4851
expected: &YamlUpdater{
4952
FilePath: "values.yaml",
5053
Path: "level1.level2",
5154
AutoCreate: false,
5255
Trim: false,
56+
Indent: 2,
5357
},
5458
},
5559
{
@@ -61,6 +65,7 @@ func TestNewUpdater(t *testing.T) {
6165
expected: &YamlUpdater{
6266
FilePath: "**/values.yaml",
6367
Path: "level1.level2",
68+
Indent: 2,
6469
},
6570
},
6671
{
@@ -127,17 +132,18 @@ object:
127132
FilePath: "basic-values.yaml",
128133
Path: "object.mykey",
129134
Valuer: value.StringValuer("updated-value"),
135+
Indent: 2,
130136
},
131137
expected: true,
132138
expectedFiles: map[string]string{
133139
"basic-values.yaml": `# a simple key
134140
key: value
135141
# an object
136142
object:
137-
# with a key we want to update
138-
mykey: updated-value
139-
# and another one we don't care about
140-
anotherkey: another-value
143+
# with a key we want to update
144+
mykey: updated-value
145+
# and another one we don't care about
146+
anotherkey: another-value
141147
`,
142148
},
143149
},

vendor/gopkg.in/yaml.v3/go.mod

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

vendor/gopkg.in/yaml.v3/go.sum

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

0 commit comments

Comments
 (0)