From 35f482a3e285d7fc95420285f28f00cea31cadd9 Mon Sep 17 00:00:00 2001 From: Erik Weathers Date: Tue, 17 May 2022 01:27:22 -0700 Subject: [PATCH 1/8] MacOs -> macOS to be consistent with the standard casing (#230) --- .goreleaser.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 0877e37c..b7d900a5 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -73,7 +73,7 @@ brews: # Caveats for the user of your binary. # Default is empty. - caveats: "Type 'tfswitch' on your command line and choose the terraform version that you want from the dropdown. This command currently only works on MacOs and Linux" + caveats: "Type 'tfswitch' on your command line and choose the terraform version that you want from the dropdown. This command currently only works on macOS and Linux" # Your app's homepage. # Default is empty. From 64761d40ca465bc38e7e90ede90ef07f7618ab98 Mon Sep 17 00:00:00 2001 From: Isaac Wilson <10012479+Jukie@users.noreply.github.com> Date: Mon, 23 May 2022 16:19:29 -0600 Subject: [PATCH 2/8] Automate binary tests from test-data directory (#234) --- .circleci/config.yml | 1 + test-data/test_tfswitchtoml/.tfswitch.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d3302b9c..b385b507 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -28,6 +28,7 @@ jobs: go test -v ./... mkdir -p build go build -v -o build/tfswitch + find ./test-data/* -type d | xargs -n 1 ./build/tfswitch -c release: docker: diff --git a/test-data/test_tfswitchtoml/.tfswitch.toml b/test-data/test_tfswitchtoml/.tfswitch.toml index fa6f6c00..f234e9cc 100644 --- a/test-data/test_tfswitchtoml/.tfswitch.toml +++ b/test-data/test_tfswitchtoml/.tfswitch.toml @@ -1,2 +1,2 @@ -bin = "/Users/warrenveerasingam/bin/terraform" +bin = "/usr/local/bin/terraform" version = "0.11.3" From 1b3596280e6a45412390045b351437184703f4a5 Mon Sep 17 00:00:00 2001 From: Isaac Wilson <10012479+Jukie@users.noreply.github.com> Date: Mon, 23 May 2022 16:26:19 -0600 Subject: [PATCH 3/8] Upgrade to Go 1.18 (#232) * Upgrade to Go 1.18 * Fix image ref * Fix git endpoint * Fix CircleCI workflow * Update Makefile Co-authored-by: George L. Yermulnik * Fix makefile Co-authored-by: George L. Yermulnik --- .circleci/config.yml | 10 +++++----- Makefile | 2 +- go.mod | 34 +++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b385b507..857dc4a8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,7 +5,7 @@ version: 2.1 jobs: build: docker: - - image: circleci/golang:1.16 + - image: cimg/go:1.18 working_directory: /go/src/github.com/warrensbox/terraform-switcher @@ -14,9 +14,9 @@ jobs: - run: command: | set +e - echo "Building website" - sudo apt-get install python3 - sudo apt-get install python3-pip + echo "Building website" + sudo apt-get update + sudo apt-get install python3 python3-pip pip3 install mkdocs-material cd www mkdocs gh-deploy --force @@ -32,7 +32,7 @@ jobs: release: docker: - - image: circleci/golang:1.16 + - image: cimg/go:1.18 working_directory: /go/src/github.com/warrensbox/terraform-switcher diff --git a/Makefile b/Makefile index d754543b..63e54580 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ EXE := tfswitch PKG := github.com/warrensbox/terraform-switcher -VER := $(shell git ls-remote --tags git://github.com/warrensbox/terraform-switcher | awk '{print $$2}'| awk -F"/" '{print $$3}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n 2 | head -n1) +VER := $(shell git ls-remote --tags git@github.com:warrensbox/terraform-switcher.git | awk '{if ($$2 ~ "\\^\\{\\}$$") next; print vers[split($$2,vers,"\\/")]}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -1) PATH := build:$(PATH) GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) diff --git a/go.mod b/go.mod index db52cb0f..bfd0e76a 100644 --- a/go.mod +++ b/go.mod @@ -1,26 +1,42 @@ module github.com/warrensbox/terraform-switcher -go 1.13 +go 1.18 require ( - github.com/chzyer/logex v1.1.10 // indirect - github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3 // indirect - github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect github.com/hashicorp/go-version v1.4.0 github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 github.com/hashicorp/terraform-config-inspect v0.0.0-20211115214459-90acf1ca460f + github.com/manifoldco/promptui v0.2.2-0.20180308161052-c0c0d3afc6a0 + github.com/mitchellh/go-homedir v1.1.0 + github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30 + github.com/spf13/viper v1.4.0 + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c +) + +require ( + github.com/agext/levenshtein v1.2.2 // indirect + github.com/apparentlymart/go-textseg v1.0.0 // indirect + github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect + github.com/chzyer/logex v1.1.10 // indirect + github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3 // indirect + github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect + github.com/fsnotify/fsnotify v1.4.7 // indirect + github.com/google/go-cmp v0.3.1 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/hashicorp/hcl/v2 v2.0.0 // indirect github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect github.com/lunixbochs/vtclean v0.0.0-20170504063817-d14193dfc626 // indirect github.com/magiconair/properties v1.8.1 // indirect - github.com/manifoldco/promptui v0.2.2-0.20180308161052-c0c0d3afc6a0 github.com/mattn/go-colorable v0.0.9 // indirect github.com/mattn/go-isatty v0.0.3 // indirect - github.com/mitchellh/go-homedir v1.1.0 - github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30 + github.com/mitchellh/go-wordwrap v1.0.0 // indirect + github.com/mitchellh/mapstructure v1.1.2 // indirect github.com/pelletier/go-toml v1.4.0 // indirect github.com/spf13/afero v1.2.2 // indirect + github.com/spf13/cast v1.3.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/spf13/viper v1.4.0 + github.com/spf13/pflag v1.0.3 // indirect github.com/zclconf/go-cty v1.8.0 // indirect - golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c + golang.org/x/text v0.3.5 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect ) From 5e36f1237b9e2b9bcae17cd2aa384cd0ea8df904 Mon Sep 17 00:00:00 2001 From: Jukie <10012479+Jukie@users.noreply.github.com> Date: Mon, 16 May 2022 12:53:14 -0600 Subject: [PATCH 4/8] Upgrade to Go 1.18 --- .circleci/config.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 857dc4a8..0c6128e0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,7 +6,6 @@ jobs: build: docker: - image: cimg/go:1.18 - working_directory: /go/src/github.com/warrensbox/terraform-switcher steps: @@ -33,7 +32,6 @@ jobs: release: docker: - image: cimg/go:1.18 - working_directory: /go/src/github.com/warrensbox/terraform-switcher steps: From 08a85ed1dddbf1591b0e50f5119accd4854683b7 Mon Sep 17 00:00:00 2001 From: Jukie <10012479+Jukie@users.noreply.github.com> Date: Tue, 17 May 2022 08:57:34 -0600 Subject: [PATCH 5/8] Fix image ref --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0c6128e0..857dc4a8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -6,6 +6,7 @@ jobs: build: docker: - image: cimg/go:1.18 + working_directory: /go/src/github.com/warrensbox/terraform-switcher steps: @@ -32,6 +33,7 @@ jobs: release: docker: - image: cimg/go:1.18 + working_directory: /go/src/github.com/warrensbox/terraform-switcher steps: From 214a4bd6ac6d5c59708824aa59c72d2ec3531d17 Mon Sep 17 00:00:00 2001 From: Jukie <10012479+Jukie@users.noreply.github.com> Date: Mon, 23 May 2022 17:01:00 -0600 Subject: [PATCH 6/8] Fix working directory --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 857dc4a8..3f012825 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,7 +7,7 @@ jobs: docker: - image: cimg/go:1.18 - working_directory: /go/src/github.com/warrensbox/terraform-switcher + working_directory: /home/circleci/go/src/github.com/warrensbox/terraform-switcher steps: - checkout @@ -34,7 +34,7 @@ jobs: docker: - image: cimg/go:1.18 - working_directory: /go/src/github.com/warrensbox/terraform-switcher + working_directory: /home/circleci/go/src/github.com/warrensbox/terraform-switcher steps: - checkout From 936debfb9d809a00bfea2144bcc0c8bd66cceca9 Mon Sep 17 00:00:00 2001 From: Jukie <10012479+Jukie@users.noreply.github.com> Date: Mon, 23 May 2022 21:40:48 -0600 Subject: [PATCH 7/8] Swap logic for sourcing directory path --- main.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index a2be99e9..4951b54c 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,7 @@ const ( var version = "0.12.0\n" func main() { + dir := lib.GetCurrentDirectory() custBinPath := getopt.StringLong("bin", 'b', lib.ConvertExecutableExt(defaultBin), "Custom binary path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username/bin/terraform")) listAllFlag := getopt.BoolLong("list-all", 'l', "List all versions of terraform - including beta and rc") latestPre := getopt.StringLong("latest-pre", 'p', defaultLatest, "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)") @@ -59,8 +60,8 @@ func main() { showLatestStable := getopt.StringLong("show-latest-stable", 'S', defaultLatest, "Show latest implicit version. Ex: tfswitch --show-latest-stable 0.13 prints 0.13.7 (latest)") latestFlag := getopt.BoolLong("latest", 'u', "Get latest stable version") showLatestFlag := getopt.BoolLong("show-latest", 'U', "Show latest stable version") - mirrorURL := getopt.StringLong("mirror", 'm', defaultMirror, "Install from a remote other than the default. Default: https://releases.hashicorp.com/terraform") - chDirPath := getopt.StringLong("chdir", 'c', "", "Switch to a different working directory before executing the given command. Ex: tfswitch --chdir terraform_project will run tfswitch in the terraform_project directory") + mirrorURL := getopt.StringLong("mirror", 'm', defaultMirror, "Install from a remote API other than the default. Default: "+defaultMirror) + chDirPath := getopt.StringLong("chdir", 'c', dir, "Switch to a different working directory before executing the given command. Ex: tfswitch --chdir terraform_project will run tfswitch in the terraform_project directory") versionFlag := getopt.BoolLong("version", 'v', "Displays the version of tfswitch") helpFlag := getopt.BoolLong("help", 'h', "Displays help message") _ = versionFlag @@ -68,18 +69,13 @@ func main() { getopt.Parse() args := getopt.Args() - dir := lib.GetCurrentDirectory() homedir := lib.GetHomeDirectory() - if *chDirPath != "" { - dir = dir + "/" + *chDirPath - } - - TFVersionFile := filepath.Join(dir, tfvFilename) //settings for .terraform-version file in current directory (tfenv compatible) - RCFile := filepath.Join(dir, rcFilename) //settings for .tfswitchrc file in current directory (backward compatible purpose) - TOMLConfigFile := filepath.Join(dir, tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory) + TFVersionFile := filepath.Join(*chDirPath, tfvFilename) //settings for .terraform-version file in current directory (tfenv compatible) + RCFile := filepath.Join(*chDirPath, rcFilename) //settings for .tfswitchrc file in current directory (backward compatible purpose) + TOMLConfigFile := filepath.Join(*chDirPath, tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory) HomeTOMLConfigFile := filepath.Join(homedir, tomlFilename) //settings for .tfswitch.toml file in home directory (option to specify bin directory) - TGHACLFile := filepath.Join(dir, tgHclFilename) //settings for terragrunt.hcl file in current directory (option to specify bin directory) + TGHACLFile := filepath.Join(*chDirPath, tgHclFilename) //settings for terragrunt.hcl file in current directory (option to specify bin directory) switch { case *versionFlag: @@ -99,7 +95,7 @@ func main() { version := "" binPath := *custBinPath if fileExists(TOMLConfigFile) { //read from toml from current directory - version, binPath = getParamsTOML(binPath, dir) + version, binPath = getParamsTOML(binPath, *chDirPath) } else { // else read from toml from home directory version, binPath = getParamsTOML(binPath, homedir) } @@ -135,8 +131,8 @@ func main() { tfversion := retrieveFileContents(TFVersionFile) installVersion(tfversion, &binPath, mirrorURL) /* if versions.tf file found (IN ADDITION TO A TOML FILE) */ - case checkTFModuleFileExist(dir) && len(args) == 0: - installTFProvidedModule(dir, &binPath, mirrorURL) + case checkTFModuleFileExist(*chDirPath) && len(args) == 0: + installTFProvidedModule(*chDirPath, &binPath, mirrorURL) /* if Terraform Version environment variable is set */ case checkTFEnvExist() && len(args) == 0 && version == "": tfversion := os.Getenv("TF_VERSION") @@ -202,8 +198,8 @@ func main() { installVersion(tfversion, custBinPath, mirrorURL) /* if versions.tf file found */ - case checkTFModuleFileExist(dir) && len(args) == 0: - installTFProvidedModule(dir, custBinPath, mirrorURL) + case checkTFModuleFileExist(*chDirPath) && len(args) == 0: + installTFProvidedModule(*chDirPath, custBinPath, mirrorURL) /* if terragrunt.hcl file found */ case fileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0: From 404af78a3db202093fc5b2159179fc36c87c8f0c Mon Sep 17 00:00:00 2001 From: Jukie <10012479+Jukie@users.noreply.github.com> Date: Mon, 23 May 2022 21:05:34 -0600 Subject: [PATCH 8/8] Fix chDirPath flag to allow absolute paths