Skip to content

Commit f2ce26a

Browse files
committed
Write output stream even if no changes and return whether updated or not
1 parent 18436e5 commit f2ce26a

3 files changed

Lines changed: 57 additions & 39 deletions

File tree

‎tfupdate/file.go‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,36 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7-
"log"
87
"os"
8+
9+
"github.com/hashicorp/hcl2/hclwrite"
910
)
1011

1112
// UpdateFile updates version constraints in a single file.
1213
func UpdateFile(filename string, o Option) error {
13-
log.Printf("[DEBUG] Open file: %s", filename)
1414
r, err := os.Open(filename)
1515
if err != nil {
1616
return fmt.Errorf("failed to open file: %s", err)
1717
}
1818
defer r.Close()
1919

2020
w := &bytes.Buffer{}
21-
err = UpdateHCL(r, w, filename, o)
21+
isUpdated, err := UpdateHCL(r, w, filename, o)
2222
if err != nil {
2323
return err
2424
}
2525

2626
// Write contents back to source file if changed.
27-
result := w.Bytes()
28-
if len(result) > 0 {
29-
log.Printf("[DEBUG] Detect changes. Write file: %s", filename)
27+
if isUpdated {
28+
updated := w.Bytes()
29+
// We should be able to choose whether to format output or not.
30+
// However, the current implementation of (*hclwrite.Body).SetAttributeValue()
31+
// does not seem to preserve an original SpaceBefore value of attribute.
32+
// So, we need to format output here.
33+
result := hclwrite.Format(updated)
3034
if err = ioutil.WriteFile(filename, result, os.ModePerm); err != nil {
3135
return fmt.Errorf("failed to write file: %s", err)
3236
}
33-
} else {
34-
log.Printf("[DEBUG] No changes. Skip writing file: %s", filename)
3537
}
3638

3739
return nil

‎tfupdate/update.go‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,31 @@ func NewOption(updateType string, target string) Option {
5858
// UpdateHCL reads HCL from io.Reader, updates version constraints
5959
// and writes updated contents to io.Writer.
6060
// Note that a filename is used only for an error message.
61-
// If input HCL doesn't match a target of option, nothing is written to the output.
62-
func UpdateHCL(r io.Reader, w io.Writer, filename string, o Option) error {
63-
src, err := ioutil.ReadAll(r)
61+
// If contents changed successfully, it returns true, or otherwise returns false.
62+
// If an error occurs, Nothing is written to the output stream.
63+
func UpdateHCL(r io.Reader, w io.Writer, filename string, o Option) (bool, error) {
64+
input, err := ioutil.ReadAll(r)
6465
if err != nil {
65-
return fmt.Errorf("failed to read input: %s", err)
66+
return false, fmt.Errorf("failed to read input: %s", err)
6667
}
6768

68-
f, diags := hclwrite.ParseConfig(src, filename, hcl.Pos{Line: 1, Column: 1})
69+
f, diags := hclwrite.ParseConfig(input, filename, hcl.Pos{Line: 1, Column: 1})
6970
if diags.HasErrors() {
70-
return fmt.Errorf("failed to parse input: %s", diags)
71+
return false, fmt.Errorf("failed to parse input: %s", diags)
7172
}
7273

7374
u, err := NewUpdater(o)
7475
if err != nil {
75-
return err
76+
return false, err
7677
}
7778

7879
u.Update(f)
79-
updated := f.BuildTokens(nil).Bytes()
80+
output := f.BuildTokens(nil).Bytes()
8081

81-
// Write contents to buffer if changed.
82-
if !bytes.Equal(src, updated) {
83-
result := hclwrite.Format(updated)
84-
85-
if _, err := w.Write(result); err != nil {
86-
return fmt.Errorf("failed to write output: %s", err)
87-
}
82+
if _, err := w.Write(output); err != nil {
83+
return false, fmt.Errorf("failed to write output: %s", err)
8884
}
8985

90-
return nil
86+
isUpdated := !bytes.Equal(input, output)
87+
return isUpdated, nil
9188
}

‎tfupdate/update_test.go‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ func TestNewUpdater(t *testing.T) {
6969

7070
func TestUpdateHCL(t *testing.T) {
7171
cases := []struct {
72-
src string
73-
o Option
74-
want string
75-
ok bool
72+
src string
73+
o Option
74+
want string
75+
isUpdated bool
76+
ok bool
7677
}{
7778
{
7879
src: `
@@ -84,12 +85,18 @@ terraform {
8485
updateType: "terraform",
8586
target: "0.12.7",
8687
},
88+
// Note the lack of space here.
89+
// the current implementation of (*hclwrite.Body).SetAttributeValue()
90+
// does not seem to preserve an original SpaceBefore value of attribute.
91+
// This is a bug of upstream.
92+
// We avoid this by formating the output of this function.
8793
want: `
8894
terraform {
89-
required_version = "0.12.7"
95+
required_version ="0.12.7"
9096
}
9197
`,
92-
ok: true,
98+
isUpdated: true,
99+
ok: true,
93100
},
94101
{
95102
src: `
@@ -103,10 +110,11 @@ provider "aws" {
103110
},
104111
want: `
105112
provider "aws" {
106-
version = "2.23.0"
113+
version ="2.23.0"
107114
}
108115
`,
109-
ok: true,
116+
isUpdated: true,
117+
ok: true,
110118
},
111119
{
112120
src: `
@@ -118,8 +126,13 @@ provider "aws" {
118126
updateType: "provider",
119127
target: "hoge@2.23.0",
120128
},
121-
want: "",
122-
ok: true,
129+
want: `
130+
provider "aws" {
131+
version = "2.11.0"
132+
}
133+
`,
134+
isUpdated: false,
135+
ok: true,
123136
},
124137
{
125138
src: `
@@ -129,8 +142,9 @@ provider "invalid" {
129142
updateType: "provider",
130143
target: "hoge@2.23.0",
131144
},
132-
want: "",
133-
ok: false,
145+
want: "",
146+
isUpdated: false,
147+
ok: false,
134148
},
135149
{
136150
src: `
@@ -142,15 +156,16 @@ provider "aws" {
142156
updateType: "hoge",
143157
target: "0.0.1",
144158
},
145-
want: "",
146-
ok: false,
159+
want: "",
160+
isUpdated: false,
161+
ok: false,
147162
},
148163
}
149164

150165
for _, tc := range cases {
151166
r := bytes.NewBufferString(tc.src)
152167
w := &bytes.Buffer{}
153-
err := UpdateHCL(r, w, "test", tc.o)
168+
isUpdated, err := UpdateHCL(r, w, "test", tc.o)
154169
if tc.ok && err != nil {
155170
t.Errorf("UpdateHCL() with src = %s, o = %#v returns unexpected err: %+v", tc.src, tc.o, err)
156171
}
@@ -159,6 +174,10 @@ provider "aws" {
159174
t.Errorf("UpdateHCL() with src = %s, o = %#v expects to return an error, but no error: %+v", tc.src, tc.o, err)
160175
}
161176

177+
if isUpdated != tc.isUpdated {
178+
t.Errorf("UpdateHCL() with src = %s, o = %#v expects to return isUpdated = %t, but want = %t", tc.src, tc.o, isUpdated, tc.isUpdated)
179+
}
180+
162181
got := string(w.Bytes())
163182
if got != tc.want {
164183
t.Errorf("UpdateHCL() with src = %s, o = %#v returns %s, but want = %s", tc.src, tc.o, got, tc.want)

0 commit comments

Comments
 (0)
⌂ 换网址