-
Notifications
You must be signed in to change notification settings - Fork 31
/
block_remove.go
44 lines (35 loc) · 1.02 KB
/
block_remove.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
package editor
import (
"io"
"github.com/hashicorp/hcl/v2/hclwrite"
)
// RemoveBlock reads HCL from io.Reader, and removes a matched block,
// and writes the updated HCL to io.Writer.
// Note that a filename is used only for an error message.
// If an error occurs, Nothing is written to the output stream.
func RemoveBlock(r io.Reader, w io.Writer, filename string, address string) error {
e := &Editor{
source: &parser{filename: filename},
filters: []Filter{
&blockRemove{address: address},
},
sink: &verticalFormater{},
}
return e.Apply(r, w)
}
// blockRemove is a filter implementation for block.
type blockRemove struct {
address string
}
// Filter reads HCL and removes only matched blocks at a given address.
func (f *blockRemove) Filter(inFile *hclwrite.File) (*hclwrite.File, error) {
typeName, labels, err := parseAddress(f.address)
if err != nil {
return nil, err
}
matched := findBlocks(inFile.Body(), typeName, labels)
for _, b := range matched {
inFile.Body().RemoveBlock(b)
}
return inFile, nil
}