Skip to content

Commit

Permalink
Merge pull request #5 from takaishi/fix/moved-block
Browse files Browse the repository at this point in the history
fix: failure parsing from/to string like `module.foo["hoge"]`
  • Loading branch information
takaishi authored Aug 29, 2024
2 parents d5b0f1d + bb69db5 commit d5d7eb1
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: test

on:
pull_request:
branches: [ main ]

jobs:
test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.21
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: make build
- run: make test
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

build:
.PHONY: build
build: test
go build -o dist/tfclean ./cmd/tfclean

.PHONY: install
install:
go install github.com/takaishi/tfclean/cmd/tfclean

.PHONY: test
test:
go test -race ./...
7 changes: 6 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ func (app *App) getValueFromAttribute(attr *hclsyntax.Attribute) (string, error)
switch traversal.(type) {
case hcl.TraverseRoot:
valueSlice = append(valueSlice, traversal.(hcl.TraverseRoot).Name)
valueSlice = append(valueSlice, ".")
case hcl.TraverseAttr:
valueSlice = append(valueSlice, traversal.(hcl.TraverseAttr).Name)
valueSlice = append(valueSlice, ".")
case hcl.TraverseIndex:
valueSlice = valueSlice[:len(valueSlice)-1]
valueSlice = append(valueSlice, fmt.Sprintf("[\"%s\"]", traversal.(hcl.TraverseIndex).Key.AsString()))
}
}
}
return strings.Join(valueSlice, "."), nil
return strings.Join(valueSlice, ""), nil
default:
return "", fmt.Errorf("unexpected type: %T", attr.Expr)
}
Expand Down
2 changes: 1 addition & 1 deletion moved.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (app *App) cutMovedBlock(data []byte, to string, from string) ([]byte, erro
s.Init(bytes.NewReader(data))
s.Mode = scanner.ScanIdents | scanner.ScanFloats
s.IsIdentRune = func(ch rune, i int) bool {
return ch == '-' || ch == '_' || ch == '.' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
return ch == '-' || ch == '_' || ch == '.' || ch == '"' || ch == '[' || ch == ']' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}

for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
Expand Down
79 changes: 79 additions & 0 deletions moved_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package tfclean

import (
"github.com/hashicorp/hcl/v2/hclparse"
"reflect"
"testing"
)

func TestApp_cutMovedBlock(t *testing.T) {
type fields struct {
hclParser *hclparse.Parser
CLI *CLI
}
type args struct {
data []byte
to string
from string
}
tests := []struct {
name string
fields fields
args args
want []byte
wantErr bool
}{
{
name: "",
fields: fields{},
args: args{
data: []byte(`
aaa
moved {
from = module.foo.hoge
to = module.foo.piyo
}
bbb
`),
from: "module.foo.hoge",
to: "module.foo.piyo",
},
want: []byte("\naaa\nbbb\n"),
wantErr: false,
},
{
name: "",
fields: fields{},
args: args{
data: []byte(`
aaa
moved {
from = module.foo["hoge"]
to = module.foo["piyo"]
}
bbb
`),
from: "module.foo[\"hoge\"]",
to: "module.foo[\"piyo\"]",
},
want: []byte("\naaa\nbbb\n"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &App{
hclParser: tt.fields.hclParser,
CLI: tt.fields.CLI,
}
got, err := app.cutMovedBlock(tt.args.data, tt.args.to, tt.args.from)
if (err != nil) != tt.wantErr {
t.Errorf("cutMovedBlock() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("cutMovedBlock() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d5d7eb1

Please sign in to comment.