summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorJörg Thalheim <joerg@thalheim.io>2022-12-20 12:22:01 +0100
committerJörg Thalheim <joerg@thalheim.io>2022-12-20 12:28:06 +0100
commit9e37c66148b4dd12f873376500821ed5daeb3137 (patch)
tree7c7c7ef105fe298783b0f686ee0e8d2152595a57 /cmd
parent27df1ec5fcbc64f6799707e98c7d0433b05ef140 (diff)
hclfmt: avoid rewrites when there are no changes in a file
this is faster for large trees and also helps to make tools like [treefmt](https://github.com/numtide/treefmt) to work, since it does not update the mtime of the file.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/hclfmt/main.go8
1 files changed, 7 insertions, 1 deletions
diff --git a/cmd/hclfmt/main.go b/cmd/hclfmt/main.go
index 01a8d41..41901cf 100644
--- a/cmd/hclfmt/main.go
+++ b/cmd/hclfmt/main.go
@@ -106,6 +106,7 @@ func processFiles() error {
func processFile(fn string, in *os.File) error {
var err error
+ var hasLocalChanges bool = false
if in == nil {
in, err = os.Open(fn)
if err != nil {
@@ -131,10 +132,15 @@ func processFile(fn string, in *os.File) error {
if !bytes.Equal(inSrc, outSrc) {
changed = append(changed, fn)
+ hasLocalChanges = true
}
if *overwrite {
- return ioutil.WriteFile(fn, outSrc, 0644)
+ if hasLocalChanges {
+ return ioutil.WriteFile(fn, outSrc, 0644)
+ } else {
+ return nil
+ }
}
_, err = os.Stdout.Write(outSrc)