-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check terraform version for state command
- Loading branch information
Showing
8 changed files
with
139 additions
and
69 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 was deleted.
Oops, something went wrong.
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 wrapper | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
) | ||
|
||
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 && | ||
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") | ||
} | ||
} | ||
|
||
if checkArgsExists(args, "state") >= 0 && | ||
checkArgsExists(args, "mv") >= 0 && | ||
versionMoved.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 mv' command. Consider using Terraform configuration moved block instead") | ||
} | ||
} | ||
|
||
return args, nil | ||
} | ||
|
||
func checkArgsExists(args []string, cmd string) int { | ||
for i, arg := range args { | ||
if arg == cmd { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} |
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,80 @@ | ||
package wrapper | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
) | ||
|
||
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"} | ||
version, _ := semver.NewVersion("1.5.0") | ||
result, err := checkStateCommand(args, version) | ||
if err != nil || !slices.Equal(result, []string{"state", "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"} | ||
version, _ := semver.NewVersion("1.4.7") | ||
result, err := checkStateCommand(args, version) | ||
if err != nil || !slices.Equal(result, []string{"state", "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"} | ||
version, _ := semver.NewVersion("1.6.0") | ||
result, err := checkStateCommand(args, version) | ||
if err == nil { | ||
t.Errorf("Expected error, got: %v, %v", err, result) | ||
} | ||
}) | ||
|
||
t.Run("Valid state mv command with --force flag on 1.6.0", func(t *testing.T) { | ||
args := []string{"state", "mv", "--force"} | ||
version, _ := semver.NewVersion("1.6.0") | ||
result, err := checkStateCommand(args, version) | ||
if err != nil || !slices.Equal(result, []string{"state", "mv"}) { | ||
t.Errorf("Expected no error, got: %v, %v", err, result) | ||
} | ||
}) | ||
} | ||
|
||
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") | ||
if result != 0 { | ||
t.Errorf("Expected 0, got: %v", result) | ||
} | ||
result = checkArgsExists(args, "import") | ||
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) { | ||
args := []string{"state", "mv"} | ||
result := checkArgsExists(args, "state") | ||
if result != 0 { | ||
t.Errorf("Expected 0, got: %v", result) | ||
} | ||
result = checkArgsExists(args, "mv") | ||
if result != 1 { | ||
t.Errorf("Expected 1, got: %v", result) | ||
} | ||
result = checkArgsExists(args, "--force") | ||
if result != -1 { | ||
t.Errorf("Expected -1, got: %v", result) | ||
} | ||
}) | ||
} |
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