From c5c07c114433f05e5f365eeab12ad61c2c81922b Mon Sep 17 00:00:00 2001
From: Elia Bracci <ebracci@sumologic.com>
Date: Tue, 20 Dec 2022 16:32:50 +0100
Subject: [PATCH 1/3] Fix bug on old terragrunt version on arm64 (M1 darwin)
 arch

---
 lib/install.go | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/lib/install.go b/lib/install.go
index e71741c..fc0681d 100644
--- a/lib/install.go
+++ b/lib/install.go
@@ -9,6 +9,8 @@ import (
 	"regexp"
 	"runtime"
 	"strings"
+
+	semver "github.com/hashicorp/go-version"
 )
 
 const (
@@ -140,7 +142,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 +170,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
@@ -182,6 +184,15 @@ func Install(tgversion string, usrBinPath string, mirrorURL string) string {
 
 	goarch := runtime.GOARCH
 	goos := runtime.GOOS
+	versionObj, err := semver.NewVersion(tgversion)
+
+	// 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.
+	darwinM1constraint, err := semver.NewConstraint("< 0.28.12")
+	if darwinM1constraint.Check(versionObj) && goarch == "arm64" && goos == "darwin" {
+		fmt.Printf("%s satisfies constraints %s", versionObj, darwinM1constraint)
+		goarch = "amd64"
+	}
 
 	/* check if selected version already downloaded */
 	installFileVersionPath := ConvertExecutableExt(filepath.Join(installLocation, installVersion+tgversion))
@@ -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":

From f919176bf50786cc475e9a1ebaa3f658593b2b3b Mon Sep 17 00:00:00 2001
From: Elia Bracci <106666672+EliaBracciSumo@users.noreply.github.com>
Date: Tue, 20 Dec 2022 17:07:37 +0100
Subject: [PATCH 2/3] Update install.sh

---
 install.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/install.sh b/install.sh
index c187a03..d2a0c11 100755
--- a/install.sh
+++ b/install.sh
@@ -337,7 +337,7 @@ End of functions from https://github.com/client9/shlib
 EOF
 
 PROJECT_NAME="tgswitch"
-OWNER=EliaBracciSumo
+OWNER=warrensbox
 REPO="tgswitch"
 BINARY=tgswitch
 FORMAT=tar.gz

From c615f339cf1f193358df2458a0ebe5fa80c2a81e Mon Sep 17 00:00:00 2001
From: Elia Bracci <ebracci@sumologic.com>
Date: Tue, 20 Dec 2022 22:23:45 +0100
Subject: [PATCH 3/3] Introduced tests cases for M1

---
 .../.terragrunt-version                          |  1 +
 test-data/test_terragrunt_hcl_m1/terragrunt.hcl  | 16 ++++++++++++++++
 test-data/test_tgswitchrc_m1/.tgswitchrc         |  1 +
 test-data/test_tgswitchtoml_m1/.tgswitch.toml    |  2 ++
 test-tgswitch.sh                                 | 11 +++++++++--
 5 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 test-data/test_terragrunt-version_m1/.terragrunt-version
 create mode 100644 test-data/test_terragrunt_hcl_m1/terragrunt.hcl
 create mode 100644 test-data/test_tgswitchrc_m1/.tgswitchrc
 create mode 100644 test-data/test_tgswitchtoml_m1/.tgswitch.toml

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 e7be098..6ee9ec8 100755
--- a/test-tgswitch.sh
+++ b/test-tgswitch.sh
@@ -82,5 +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
-runtestarg "passing argument" "0.28.0" "v0.28.0"
\ No newline at end of file
+# 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