-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.go
122 lines (110 loc) · 2.86 KB
/
import.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
package tfclean
import (
"bytes"
"fmt"
"text/scanner"
"unicode"
"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
type ImportBlock struct {
To string
Id string
}
func (app *App) processImportBlock(block *hclsyntax.Block, state *tfstate.TFState, data []byte) ([]byte, error) {
to, _ := app.getValueFromAttribute(block.Body.Attributes["to"])
id, _ := app.getValueFromAttribute(block.Body.Attributes["id"])
if state != nil {
isApplied, err := app.movedImportIsApplied(state, to)
if err != nil {
return data, err
}
if isApplied {
data, err := app.cutImportBlock(data, to, id)
if err != nil {
return nil, err
}
return data, nil
}
} else {
data, err := app.cutImportBlock(data, to, id)
if err != nil {
return nil, err
}
return data, nil
}
return data, nil
}
func (app *App) movedImportIsApplied(state *tfstate.TFState, to string) (bool, error) {
toAttrs, err := state.Lookup(to)
if err != nil {
return false, err
}
if toAttrs.String() != "null" {
return true, nil
}
return false, nil
}
func (app *App) cutImportBlock(data []byte, to string, id string) ([]byte, error) {
var 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 == ':' || 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() == "import" && isAtLineStart(data, lastPos, s.Position.Offset) {
found, data, err := app.readImportBlock(&s, data, to, id, s.Offset)
if err != nil {
return nil, err
}
if found {
return data, nil
}
}
lastPos = s.Position.Offset
}
return nil, nil
}
func (app *App) readImportBlock(s *scanner.Scanner, data []byte, to string, id string, lastPos int) (bool, []byte, error) {
var spos, epos int
var importBlock ImportBlock
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 importBlock.To == to && importBlock.Id == id {
data = bytes.Join([][]byte{data[:spos], data[epos:]}, []byte(""))
return true, data, nil
}
return false, nil, nil
case "to":
current = "to"
case "id":
current = "id"
case "=":
//case "\"":
// Ignore
default:
switch current {
case "to":
importBlock.To = s.TokenText()
case "id":
id = s.TokenText()
if id[0] == '"' && id[len(id)-1] == '"' {
id = id[1 : len(id)-1]
}
importBlock.Id = id
default:
return false, nil, fmt.Errorf("unexpected token: " + s.TokenText())
}
}
}
return false, nil, nil
}