Skip to content

Commit

Permalink
Merge pull request #7 from takaishi/chore/index-import-block
Browse files Browse the repository at this point in the history
fix: failure parsing to string like module.foo["hoge"]
  • Loading branch information
takaishi authored Aug 29, 2024
2 parents fa9d066 + 81fb779 commit d198036
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
16 changes: 8 additions & 8 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ func (app *App) processImportBlock(block *hclsyntax.Block, state *tfstate.TFStat
to, _ := app.getValueFromAttribute(block.Body.Attributes["to"])
id, _ := app.getValueFromAttribute(block.Body.Attributes["id"])
fmt.Printf("to: %s, id: %s\n", to, id)
//isApplied, err := app.movedImportIsApplied(state, to)
//if err != nil {
// return data, err
//}
isApplied := true
isApplied, err := app.movedImportIsApplied(state, to)
if err != nil {
return data, err
}
if isApplied {
data, err := app.cutImportBlock(data, to, id)
if err != nil {
Expand All @@ -44,13 +43,13 @@ func (app *App) movedImportIsApplied(state *tfstate.TFState, to string) (bool, e
return false, nil
}

func (app *App) cutImportBlock(data []byte, to string, from string) ([]byte, error) {
func (app *App) cutImportBlock(data []byte, to string, id string) ([]byte, error) {
var s scanner.Scanner
var spos, epos int
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 All @@ -67,7 +66,7 @@ func (app *App) cutImportBlock(data []byte, to string, from string) ([]byte, err
case "}":
// Remove moved block that includes `}` and newline
epos = s.Offset + 2
if importBlock.To == to && importBlock.Id == from {
if importBlock.To == to && importBlock.Id == fmt.Sprintf("\"%s\"", id) {
data = bytes.Join([][]byte{data[:spos], data[epos:]}, []byte(""))
return data, nil
}
Expand All @@ -76,6 +75,7 @@ func (app *App) cutImportBlock(data []byte, to string, from string) ([]byte, err
case "id":
current = "id"
case "=":
//case "\"":
// Ignore
default:
switch current {
Expand Down
80 changes: 80 additions & 0 deletions import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package tfclean

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

func TestApp_cutImportBlock(t *testing.T) {
type fields struct {
hclParser *hclparse.Parser
CLI *CLI
}
type args struct {
data []byte
to string
id string
}
tests := []struct {
name string
fields fields
args args
want []byte
wantErr bool
}{
// TODO: Add test cases.
{
name: "",
fields: fields{},
args: args{
data: []byte(`
aaa
import {
id = "resource_id"
to = module.foo.hoge
}
bbb
`),
to: "module.foo.hoge",
id: "resource_id",
},
want: []byte("\naaa\nbbb\n"),
wantErr: false,
},
{
name: "",
fields: fields{},
args: args{
data: []byte(`
aaa
import {
id = "resource_id"
to = module.foo["hoge"]
}
bbb
`),
to: "module.foo[\"hoge\"]",
id: "resource_id",
},
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.cutImportBlock(tt.args.data, tt.args.to, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("cutImportBlock() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("cutImportBlock() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit d198036

Please sign in to comment.