-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (85 loc) · 2.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"log"
"os"
"path/filepath"
"slices"
"strings"
"github.com/cockroachdb/errors"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
)
func getRefactoringBlocks() []string {
return []string{"moved", "import", "removed"}
}
func main() {
const terraformDir = "./" //TODO: あとで引数で弄れるようにしたい
err := filepath.Walk(terraformDir, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, "Error on filepath.Walk")
}
// '.tf' 拡張子でなければスキップ
if !strings.HasSuffix(info.Name(), ".tf") {
return nil
}
src, err := os.ReadFile(filePath)
if err != nil {
return errors.Wrap(err, "Error on os.ReadFile")
}
file, diags := hclwrite.ParseConfig(src, filePath, hcl.InitialPos)
if diags.HasErrors() {
return errors.Wrap(diags, "Error on hclwrite.ParseConfig")
}
body := file.Body()
ret := tfrbac(body)
if err = os.WriteFile(filePath, ret.Bytes(), info.Mode()); err != nil {
return errors.Wrap(err, "Error on os.WriteFile")
}
return nil
})
if err != nil {
log.Fatalf("Error walking through Terraform directory: %+v\n", err)
}
}
func tfrbac(body *hclwrite.Body) hclwrite.Tokens {
deleteTokens := make([]hclwrite.Tokens, 0)
for _, v := range body.Blocks() {
if slices.Contains(getRefactoringBlocks(), v.Type()) {
deleteTokens = append(deleteTokens, v.BuildTokens(nil))
}
}
tokens := body.BuildTokens(nil)
ret := make(hclwrite.Tokens, 0, len(tokens))
startTokenPos := 0
for i := 0; i < len(tokens); i++ {
if len(deleteTokens) == 0 {
break
}
deleteToken := deleteTokens[0]
find := true
for j := 0; j < len(deleteToken) && i+j < len(tokens); j++ {
if deleteToken[j] != tokens[i+j] {
find = false
break
}
}
if !find {
continue
}
endTokenPos := i
i += len(deleteToken) - 1
if i+1 < len(tokens) && tokens[i+1].Type == hclsyntax.TokenNewline {
i++ // 後ろに改行がある場合はそれを削除
} else if endTokenPos-2 > startTokenPos &&
tokens[endTokenPos-1].Type == hclsyntax.TokenNewline &&
tokens[endTokenPos-2].Type == hclsyntax.TokenNewline {
endTokenPos-- // 後ろに改行はないけど、上に二つ以上改行がある場合、一つ削除
}
deleteTokens = deleteTokens[1:]
ret = tokens[startTokenPos:endTokenPos].BuildTokens(ret)
startTokenPos = i + 1
}
ret = tokens[startTokenPos:].BuildTokens(ret)
return ret
}