Skip to content

Commit

Permalink
import command
Browse files Browse the repository at this point in the history
  • Loading branch information
c4po committed Mar 20, 2024
1 parent ecdcd92 commit a149f29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
5 changes: 2 additions & 3 deletions internal/wrapper/checkargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ func checkStateCommand(args []string, version *semver.Version) ([]string, error)
versionImport, _ := semver.NewConstraint(">= 1.5.0")
versionMoved, _ := semver.NewConstraint(">= 1.1.0")
versionImport.Check(version)
if checkArgsExists(args, "state") >= 0 &&
checkArgsExists(args, "import") >= 0 &&
if checkArgsExists(args, "import") >= 0 &&
versionImport.Check(version) {
force_pos := checkArgsExists(args, "--force")
if force_pos > 0 {
return append(args[:force_pos], args[force_pos+1:]...), nil
} else {
return args, errors.New("--force flag is required for the 'state import' command. Consider using Terraform configuration import block instead")
return args, errors.New("--force flag is required for the 'import' command. Consider using Terraform configuration import block instead")
}
}

Expand Down
26 changes: 11 additions & 15 deletions internal/wrapper/checkargs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (

func TestCheckStateCommand(t *testing.T) {
t.Run("Valid state import command with --force flag on 1.5.0", func(t *testing.T) {
args := []string{"state", "import", "--force"}
args := []string{"import", "--force"}
version, _ := semver.NewVersion("1.5.0")
result, err := checkStateCommand(args, version)
if err != nil || !slices.Equal(result, []string{"state", "import"}) {
if err != nil || !slices.Equal(result, []string{"import"}) {
t.Errorf("Expected no error, got: %v, %v", err, result)
}
})

t.Run("Valid state import command without --force flag on 1.4.7", func(t *testing.T) {
args := []string{"state", "import"}
args := []string{"import"}
version, _ := semver.NewVersion("1.4.7")
result, err := checkStateCommand(args, version)
if err != nil || !slices.Equal(result, []string{"state", "import"}) {
if err != nil || !slices.Equal(result, []string{"import"}) {
t.Errorf("Expected no error, got: %v", err)
}
})

t.Run("Invalid state import command without --force flag on 1.5.0", func(t *testing.T) {
args := []string{"state", "import"}
args := []string{"import"}
version, _ := semver.NewVersion("1.6.0")
result, err := checkStateCommand(args, version)
_, err := checkStateCommand(args, version)
if err == nil {
t.Errorf("Expected error, got: %v, %v", err, result)
t.Errorf("Expected error, got: %v", err)
}
})

Expand All @@ -46,20 +46,16 @@ func TestCheckStateCommand(t *testing.T) {
}

func TestCheckArgsExists(t *testing.T) {
t.Run("Check 'state import --force' command", func(t *testing.T) {
args := []string{"state", "import", "--force"}
result := checkArgsExists(args, "state")
t.Run("Check 'import --force' command", func(t *testing.T) {
args := []string{"import", "--force"}
result := checkArgsExists(args, "import")
if result != 0 {
t.Errorf("Expected 0, got: %v", result)
}
result = checkArgsExists(args, "import")
result = checkArgsExists(args, "--force")
if result != 1 {
t.Errorf("Expected 1, got: %v", result)
}
result = checkArgsExists(args, "--force")
if result != 2 {
t.Errorf("Expected 2, got: %v", result)
}
})

t.Run("Check 'state moved' command", func(t *testing.T) {
Expand Down

0 comments on commit a149f29

Please sign in to comment.