-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
143 lines (132 loc) · 3.35 KB
/
app.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package tfclean
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
type App struct {
hclParser *hclparse.Parser
CLI *CLI
}
func New(cli *CLI) *App {
return &App{
hclParser: hclparse.NewParser(),
CLI: cli,
}
}
func (app *App) Run(ctx context.Context) error {
var err error
var state *tfstate.TFState
if app.CLI.Tfstate != "" {
state, err = tfstate.ReadURL(ctx, app.CLI.Tfstate)
if err != nil {
return err
}
}
files, err := os.ReadDir(app.CLI.Dir)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
continue
}
if filepath.Ext(file.Name()) == ".tf" {
path := filepath.Join(app.CLI.Dir, file.Name())
err := app.processFile(path, state)
if err != nil {
return err
}
}
}
return nil
}
func (app *App) processFile(path string, state *tfstate.TFState) error {
hclFile, diags := app.hclParser.ParseHCLFile(path)
if diags.HasErrors() {
return fmt.Errorf("error parsing HCL hclFile: %s", diags)
}
body, ok := hclFile.Body.(*hclsyntax.Body)
if !ok {
return fmt.Errorf("not an HCL syntax body")
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
data, _ := os.ReadFile(path)
file.Close()
for _, block := range body.Blocks {
switch block.Type {
case "moved":
data, err = app.processMovedBlock(block, state, data)
if err != nil {
return err
}
case "import":
data, err = app.processImportBlock(block, state, data)
if err != nil {
return err
}
case "removed":
data, err = app.processRemovedBlock(block, state, data)
if err != nil {
return err
}
}
}
return os.WriteFile(path, data, 0644)
}
func (app *App) getValueFromAttribute(attr *hclsyntax.Attribute) (string, error) {
switch attr.Expr.(type) {
case *hclsyntax.TemplateExpr:
for _, part := range attr.Expr.(*hclsyntax.TemplateExpr).Parts {
switch part.(type) {
case *hclsyntax.LiteralValueExpr:
return part.(*hclsyntax.LiteralValueExpr).Val.AsString(), nil
default:
return "", fmt.Errorf("unexpected type: %T", part)
}
}
case *hclsyntax.ScopeTraversalExpr:
valueSlice := []string{}
for _, traversals := range attr.Expr.(*hclsyntax.ScopeTraversalExpr).Variables() {
tl := len(traversals)
for i, traversal := range traversals {
switch traversal.(type) {
case hcl.TraverseRoot:
valueSlice = append(valueSlice, traversal.(hcl.TraverseRoot).Name)
valueSlice = append(valueSlice, ".")
if i == tl-1 {
valueSlice = valueSlice[:len(valueSlice)-1]
return strings.Join(valueSlice, ""), nil
}
case hcl.TraverseAttr:
valueSlice = append(valueSlice, traversal.(hcl.TraverseAttr).Name)
valueSlice = append(valueSlice, ".")
if i == tl-1 {
valueSlice = valueSlice[:len(valueSlice)-1]
return strings.Join(valueSlice, ""), nil
}
case hcl.TraverseIndex:
valueSlice = valueSlice[:len(valueSlice)-1]
valueSlice = append(valueSlice, fmt.Sprintf("[\"%s\"]", traversal.(hcl.TraverseIndex).Key.AsString()))
if i == tl-1 {
return strings.Join(valueSlice, ""), nil
}
}
}
}
return strings.Join(valueSlice, ""), nil
default:
return "", fmt.Errorf("unexpected type: %T", attr.Expr)
}
return "", nil
}