Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bdd899
commit 1b6bbdf
Showing
5 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package editor | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
) | ||
|
||
// RemoveAttribute reads HCL from io.Reader, and remove a matched attribute, | ||
// 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 RemoveAttribute(r io.Reader, w io.Writer, filename string, address string) error { | ||
e := &Editor{ | ||
source: &parser{filename: filename}, | ||
filters: []Filter{ | ||
&attributeRemove{address: address}, | ||
}, | ||
sink: &formater{}, | ||
} | ||
|
||
return e.Apply(r, w) | ||
} | ||
|
||
// attributeRemove is a filter implementation for attribute. | ||
type attributeRemove struct { | ||
address string | ||
} | ||
|
||
// Filter reads HCL and remove a matched attribute at a given address. | ||
func (f *attributeRemove) Filter(inFile *hclwrite.File) (*hclwrite.File, error) { | ||
attr, body, err := findAttribute(inFile.Body(), f.address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if attr != nil { | ||
a := strings.Split(f.address, ".") | ||
attrName := a[len(a)-1] | ||
body.RemoveAttribute(attrName) | ||
} | ||
|
||
return inFile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package editor | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestAttributeRemove(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
src string | ||
address string | ||
ok bool | ||
want string | ||
}{ | ||
{ | ||
name: "simple top level attribute", | ||
src: ` | ||
a0 = v0 | ||
a1 = v1 | ||
`, | ||
address: "a0", | ||
ok: true, | ||
want: ` | ||
a1 = v1 | ||
`, | ||
}, | ||
{ | ||
name: "simple top level attribute (with comments)", | ||
src: ` | ||
// before attr | ||
a0 = "v0" // inline | ||
a1 = "v1" | ||
`, | ||
address: "a0", | ||
ok: true, | ||
want: ` | ||
a1 = "v1" | ||
`, // Unfortunately we can't keep the before attr comment. | ||
}, | ||
{ | ||
name: "attribute in block", | ||
src: ` | ||
a0 = v0 | ||
b1 "l1" { | ||
a1 = v1 | ||
a2 = v2 | ||
} | ||
`, | ||
address: "b1.l1.a1", | ||
ok: true, | ||
want: ` | ||
a0 = v0 | ||
b1 "l1" { | ||
a2 = v2 | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "top level attribute not found", | ||
src: ` | ||
a0 = v0 | ||
`, | ||
address: "a1", | ||
ok: true, | ||
want: ` | ||
a0 = v0 | ||
`, | ||
}, | ||
{ | ||
name: "attribute not found in block", | ||
src: ` | ||
a0 = v0 | ||
b1 "l1" { | ||
a1 = v1 | ||
} | ||
`, | ||
address: "b1.l1.a2", | ||
ok: true, | ||
want: ` | ||
a0 = v0 | ||
b1 "l1" { | ||
a1 = v1 | ||
} | ||
`, | ||
}, | ||
{ | ||
name: "block not found", | ||
src: ` | ||
a0 = v0 | ||
b1 "l1" { | ||
a1 = v1 | ||
} | ||
`, | ||
address: "b2.l1.a1", | ||
ok: true, | ||
want: ` | ||
a0 = v0 | ||
b1 "l1" { | ||
a1 = v1 | ||
} | ||
`, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
inStream := bytes.NewBufferString(tc.src) | ||
outStream := new(bytes.Buffer) | ||
err := RemoveAttribute(inStream, outStream, "test", tc.address) | ||
if tc.ok && err != nil { | ||
t.Fatalf("unexpected err = %s", err) | ||
} | ||
|
||
got := outStream.String() | ||
if !tc.ok && err == nil { | ||
t.Fatalf("expected to return an error, but no error, outStream: \n%s", got) | ||
} | ||
|
||
if got != tc.want { | ||
t.Fatalf("got:\n%s\nwant:\n%s", got, tc.want) | ||
} | ||
}) | ||
} | ||
} |