Skip to content

Commit

Permalink
add customedit function (#93)
Browse files Browse the repository at this point in the history
* add customedit function

* move to v2

* explicitly import package as hcl

* Revert "explicitly import package as hcl"

This reverts commit a8051d6.
  • Loading branch information
drlau authored Jun 1, 2023
1 parent eb99b35 commit 255488b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Lint
uses: reviewdog/action-golangci-lint@v1
uses: reviewdog/action-golangci-lint@v2
8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,5 @@ jobs:
with:
go-version: ${{ matrix.go }}

- name: Cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test
run: go test -v -race ./...
6 changes: 6 additions & 0 deletions hcledit.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (h *HCLEditor) Delete(queryStr string, opts ...Option) error {
return w.Walk(h.writeFile.Body(), queries, 0, []string{})
}

// CustomEdit executes a custom function on the underlying file
func (h *HCLEditor) CustomEdit(fn func(*hclwrite.Body) error) error {
defer h.reload()
return fn(h.writeFile.Body())
}

// reload re-parse the HCL file. Some operation causes like `WithAfter` modifies Body token structure
// drastically (it re-construct it from scratch...) and, because of it, some operation will not work
// properly after it
Expand Down
69 changes: 69 additions & 0 deletions hcledit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"

"go.mercari.io/hcledit"
)
Expand Down Expand Up @@ -840,3 +842,70 @@ object1 = {
})
}
}

func TestCustomEdit(t *testing.T) {
cases := map[string]struct {
input string
function func(*hclwrite.Body) error
want string
}{
"Attribute": {
input: `
`,
function: func(b *hclwrite.Body) error {
block := b.AppendNewBlock("block", []string{"test"})
blockBody := block.Body()
blockBody.SetAttributeValue("attribute", cty.StringVal("A"))
return nil
},
want: `
block "test" {
attribute = "A"
}
`,
},
"Existing block with identical labels": {
input: `
block "test" {
existing = "A"
}
`,
function: func(b *hclwrite.Body) error {
block := b.AppendNewBlock("block", []string{"test"})
blockBody := block.Body()
blockBody.SetAttributeValue("attribute", cty.StringVal("A"))
return nil
},
want: `
block "test" {
existing = "A"
}
block "test" {
attribute = "A"
}
`,
},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
editor, err := hcledit.Read(strings.NewReader(tc.input), "")
if err != nil {
t.Fatal(err)
}
if err := editor.CustomEdit(tc.function); err != nil {
t.Fatal(err)
}

diff := cmp.Diff(tc.want, string(editor.Bytes()),
cmpopts.AcyclicTransformer("multiline", func(s string) []string {
return strings.Split(s, "\n")
}),
)
if diff != "" {
t.Errorf("CustomEdit() mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 255488b

Please sign in to comment.