diff --git a/.circleci/config.yml b/.circleci/config.yml index d3302b9c..3f012825 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,18 +5,18 @@ 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 + working_directory: /home/circleci/go/src/github.com/warrensbox/terraform-switcher steps: - checkout - 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 @@ -28,12 +28,13 @@ 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: - - image: circleci/golang:1.16 + - 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 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. 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 ) 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: 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"