-
Notifications
You must be signed in to change notification settings - Fork 1
/
removed.go
172 lines (158 loc) · 3.85 KB
/
removed.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package tfclean
import (
"bytes"
"fmt"
"strings"
"text/scanner"
"unicode"
"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
type RemovedBlock struct {
From string
Lifecycle *LifecycleBlock
}
type LifecycleBlock struct {
Destroy string
}
func (app *App) processRemovedBlock(block *hclsyntax.Block, state *tfstate.TFState, data []byte) ([]byte, error) {
from, _ := app.getValueFromAttribute(block.Body.Attributes["from"])
if state != nil {
isApplied, err := app.removedBlockIsApplied(state, from)
if err != nil {
return data, err
}
if isApplied {
data, err = app.cutRemovedBlock(data, from)
if err != nil {
return data, err
}
}
} else {
data, err := app.cutRemovedBlock(data, from)
if err != nil {
return data, err
}
return data, nil
}
return data, nil
}
func (app *App) removedBlockIsApplied(state *tfstate.TFState, from string) (bool, error) {
if len(strings.Split(from, ".")) == 2 {
names, err := state.List()
if err != nil {
return false, err
}
existsFrom := false
for _, name := range names {
if strings.HasPrefix(name, from+".") {
existsFrom = true
break
}
}
if !existsFrom {
return true, nil
}
return false, nil
} else {
// from and to is resource
fromAttrs, err := state.Lookup(from)
if err != nil {
return false, err
}
if fromAttrs.String() == "null" {
return true, nil
}
return false, nil
}
}
func (app *App) cutRemovedBlock(data []byte, from string) ([]byte, error) {
s := &scanner.Scanner{}
s.Init(bytes.NewReader(data))
s.Mode = scanner.ScanIdents | scanner.ScanFloats
s.IsIdentRune = func(ch rune, i int) bool {
return ch == '-' || ch == '_' || ch == '.' || ch == '"' || ch == '[' || ch == ']' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}
var lastPos int
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
if s.TokenText() == "removed" && isAtLineStart(data, lastPos, s.Position.Offset) {
found, data, err := app.readRemovedBlock(s, data, from, s.Offset)
if err != nil {
return nil, err
}
if found {
return data, nil
}
}
lastPos = s.Position.Offset
}
return nil, nil
}
func (app *App) readRemovedBlock(s *scanner.Scanner, data []byte, from string, lastPos int) (bool, []byte, error) {
var spos, epos int
movedBlock := &RemovedBlock{}
var current string
spos = lastPos
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch s.TokenText() {
case "{":
// Ignore
case "}":
// Remove moved block that includes `}` and newline
epos = s.Offset + 2
if movedBlock.From == from {
data = bytes.Join([][]byte{data[:spos], data[epos:]}, []byte(""))
return true, data, nil
}
return false, nil, nil
case "from":
current = "from"
case "lifecycle":
lb, err := app.readLifecycleBlock(s)
if err != nil {
return false, nil, err
}
movedBlock.Lifecycle = lb
case "=":
// Ignore
default:
switch current {
case "from":
movedBlock.From = s.TokenText()
default:
return false, nil, fmt.Errorf("unexpected token: " + s.TokenText())
}
}
}
return false, nil, nil
}
func (app *App) readLifecycleBlock(s *scanner.Scanner) (*LifecycleBlock, error) {
lifecycleBlock := &LifecycleBlock{}
var current string
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch s.TokenText() {
case "{":
// Ignore
case "}":
return lifecycleBlock, nil
case "destroy":
current = "destroy"
case "=":
// Ignore
default:
switch current {
case "destroy":
lifecycleBlock.Destroy = s.TokenText()
default:
return nil, fmt.Errorf("unexpected token: " + s.TokenText())
}
}
}
return lifecycleBlock, nil
}
func isAtLineStart(data []byte, lastPos int, currentPos int) bool {
if lastPos == 0 {
return true
}
return bytes.LastIndexByte(data[lastPos:currentPos], '\n') == len(data[lastPos:currentPos])-1
}