diff --git a/install.sh b/install.sh old mode 100644 new mode 100755 diff --git a/lib/install.go b/lib/install.go index e71741c..fe041b0 100644 --- a/lib/install.go +++ b/lib/install.go @@ -140,7 +140,7 @@ func GetRecentVersions() ([]string, error) { return nil, nil } -//CreateRecentFile : create a recent file +// CreateRecentFile : create a recent file func CreateRecentFile(requestedVersion string) { installLocation = GetInstallLocation() @@ -168,7 +168,7 @@ func ValidVersionFormat(version string) bool { return semverRegex.MatchString(version) } -//Install : Install the provided version in the argument +// Install : Install the provided version in the argument func Install(tgversion string, usrBinPath string, mirrorURL string) string { /* Check to see if user has permission to the default bin location which is "/usr/local/bin/terragrunt" * If user does not have permission to default bin location, proceed to create $HOME/bin and install the tgswitch there @@ -183,6 +183,17 @@ func Install(tgversion string, usrBinPath string, mirrorURL string) string { goarch := runtime.GOARCH goos := runtime.GOOS + checkDarwinArm64Constraint, err := CheckDarwinArm64VersionConstraint(tgversion, goarch, goos) + + if err != nil { + log.Println(err) + } + + if checkDarwinArm64Constraint { + fmt.Printf("%s satisfies Darwin arm64 constraints for tg version < 0.28.12. Switching arch to amd64 \n", tgversion) + goarch = "amd64" + } + /* check if selected version already downloaded */ installFileVersionPath := ConvertExecutableExt(filepath.Join(installLocation, installVersion+tgversion)) fileExist := CheckFileExist(installLocation + installVersion + tgversion) @@ -225,7 +236,7 @@ func Install(tgversion string, usrBinPath string, mirrorURL string) string { /* rename unzipped file to terragrunt version name - terraform_x.x.x */ RenameFile(downloadedFile, installFileVersionPath) - err := os.Chmod(installFileVersionPath, 0755) + err = os.Chmod(installFileVersionPath, 0755) if err != nil { log.Println(err) } @@ -244,9 +255,9 @@ func Install(tgversion string, usrBinPath string, mirrorURL string) string { return "" } -//InstallableBinLocation : Checks if terragrunt is installable in the location provided by the user. -//If not, create $HOME/bin. Ask users to add $HOME/bin to $PATH -//Return $HOME/bin as install location +// InstallableBinLocation : Checks if terragrunt is installable in the location provided by the user. +// If not, create $HOME/bin. Ask users to add $HOME/bin to $PATH +// Return $HOME/bin as install location func InstallableBinLocation(userBinPath string) string { usr, errCurr := user.Current() @@ -294,7 +305,7 @@ func PrintCreateDirStmt(unableDir string, writable string) { fmt.Printf("RUN `export PATH=$PATH:%s` to append bin to $PATH\n", writable) } -//ConvertExecutableExt : convert excutable with local OS extension +// ConvertExecutableExt : convert excutable with local OS extension func ConvertExecutableExt(fpath string) string { switch runtime.GOOS { case "windows": diff --git a/lib/semver.go b/lib/semver.go index 4ef4982..120b730 100644 --- a/lib/semver.go +++ b/lib/semver.go @@ -53,3 +53,12 @@ func SemVerParser(tfconstraint *string, tflist []string) (string, error) { func PrintInvalidTFVersion() { fmt.Println("Version does not exist or invalid terraform version format.\n Format should be #.#.# or #.#.#-@# where # are numbers and @ are word characters.\n For example, 0.11.7 and 0.11.9-beta1 are valid versions") } + +// Function that check constraint for darwin M1. Terragrunt started release arm64 versions for linux and darwin OS from version 0.28.12 included. +// However, amd64 versions work on darwin arm64. To be tested on linux platforms. +func CheckDarwinArm64VersionConstraint(tgversion string, goarch string, goos string) (bool, error) { + version, err := semver.NewVersion(tgversion) + darwinM1constraint, err := semver.NewConstraint("< 0.28.12") + + return darwinM1constraint.Check(version) && goarch == "arm64" && goos == "darwin", err +} diff --git a/test-data/test_terragrunt-version_m1/.terragrunt-version b/test-data/test_terragrunt-version_m1/.terragrunt-version new file mode 100644 index 0000000..1655538 --- /dev/null +++ b/test-data/test_terragrunt-version_m1/.terragrunt-version @@ -0,0 +1 @@ +0.26.7 \ No newline at end of file diff --git a/test-data/test_terragrunt_hcl_m1/terragrunt.hcl b/test-data/test_terragrunt_hcl_m1/terragrunt.hcl new file mode 100644 index 0000000..2e76b00 --- /dev/null +++ b/test-data/test_terragrunt_hcl_m1/terragrunt.hcl @@ -0,0 +1,16 @@ +include { + path = "${find_in_parent_folders()}" +} + +terraform { + source = "..." + + extra_arguments "variables" { + commands = get_terraform_commands_that_need_vars() + } +} + inputs = merge( + jsondecode(file("${find_in_parent_folders("general.tfvars")}")) +) + +terragrunt_version_constraint=">= 0.28, < 0.28.1" \ No newline at end of file diff --git a/test-data/test_tgswitchrc_m1/.tgswitchrc b/test-data/test_tgswitchrc_m1/.tgswitchrc new file mode 100644 index 0000000..697f087 --- /dev/null +++ b/test-data/test_tgswitchrc_m1/.tgswitchrc @@ -0,0 +1 @@ +0.28.0 diff --git a/test-data/test_tgswitchtoml_m1/.tgswitch.toml b/test-data/test_tgswitchtoml_m1/.tgswitch.toml new file mode 100644 index 0000000..ce817ad --- /dev/null +++ b/test-data/test_tgswitchtoml_m1/.tgswitch.toml @@ -0,0 +1,2 @@ +bin = "/usr/local/bin/terragrunt" +version = "0.26.7" \ No newline at end of file diff --git a/test-tgswitch.sh b/test-tgswitch.sh index c1789bf..6ee9ec8 100755 --- a/test-tgswitch.sh +++ b/test-tgswitch.sh @@ -82,3 +82,12 @@ runtestdir "tgswitchrc" "test_tgswitchrc" "v0.33.0" runtestdir ".toml" "test_tgswitchtoml" "v0.34.0" runtestenv "env variable" "0.37.1" "v0.37.1" runtestarg "passing argument" "0.36.1" "v0.36.1" +# M1 darwin arm64 test versions < 0.28.12 +runtestdir "terragrunt version" "test_terragrunt-version_m1" "v0.26.7" +runtestdir "terragrunt hcl" "test_terragrunt_hcl_m1" "v0.28.0" +runtestdir "tgswitchrc" "test_tgswitchrc_m1" "v0.28.0" +runtestdir ".toml" "test_tgswitchtoml_m1" "v0.26.7" +runtestenv "env variable" "0.26.7" "v0.26.7" +runtestarg "passing argument" "0.26.7" "v0.26.7" +# Edge case +runtestarg "passing argument" "0.28.12" "v0.28.12" \ No newline at end of file