diff --git a/README.md b/README.md index e79ee487..1f2c5b32 100644 --- a/README.md +++ b/README.md @@ -13,23 +13,28 @@ The installation is minimal and easy. Once installed, simply select the version you require from the dropdown and start using Terraform. ## Documentation + Click [here](https://tfswitch.warrensbox.com) for our extended documentation. ## NOTE + Going forward we will change the version identifier of `tfswitch` to align with the common go package versioning. Please be advised to change any automated implementation you might have that is relying on the `tfswitch` version string. **Old version string:** `0.1.2412` **New version string:** `v1.0.0` Note the `v` that is preceding all version numbers. ## Installation -`tfswitch` is available as a binary and on various package managers (eg. Homebrew). + +`tfswitch` is available as a binary and on various package managers (eg. Homebrew). ## Windows + Download and extract the Windows version of `tfswitch` that is compatible with your system. We are building binaries for 386, amd64, arm6 and arm7 CPU structure. See the [release page](https://github.com/warrensbox/terraform-switcher/releases/latest) for your download. ## Homebrew + For macOS or various Linux distributions, Homebrew offers the simplest installation process. If you do not have homebrew installed, click here. ```ruby @@ -37,6 +42,7 @@ brew install warrensbox/tap/tfswitch ``` ## Linux + Installation for Linux operating systems. ```sh @@ -59,12 +65,12 @@ Alternatively, you can install the binary from the source %v", version) @@ -137,7 +137,7 @@ func versionExist(val interface{}, array interface{}) (exists bool) { s := reflect.ValueOf(array) for i := 0; i < s.Len(); i++ { - if reflect.DeepEqual(val, s.Index(i).Interface()) == true { + if reflect.DeepEqual(val, s.Index(i).Interface()) { exists = true return exists } @@ -156,7 +156,7 @@ func removeDuplicateVersions(elements []string) []string { for _, val := range elements { versionOnly := strings.Trim(val, " *recent") - if encountered[versionOnly] == true { + if encountered[versionOnly] { // Do not add duplicate. } else { // Record this element as an encountered element. diff --git a/lib/param_parsing/parameters.go b/lib/param_parsing/parameters.go index e211f922..9c534eac 100644 --- a/lib/param_parsing/parameters.go +++ b/lib/param_parsing/parameters.go @@ -11,6 +11,7 @@ type Params struct { CustomBinaryPath string DefaultVersion string HelpFlag bool + InstallPath string LatestFlag bool LatestPre string LatestStable string @@ -34,6 +35,7 @@ func GetParameters() Params { getopt.StringVarLong(¶ms.CustomBinaryPath, "bin", 'b', "Custom binary path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username/bin/terraform")) getopt.StringVarLong(¶ms.DefaultVersion, "default", 'd', "Default to this version in case no other versions could be detected. Ex: tfswitch --default 1.2.4") getopt.BoolVarLong(¶ms.HelpFlag, "help", 'h', "Displays help message") + getopt.StringVarLong(¶ms.InstallPath, "install", 'i', "Custom install path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username")) getopt.BoolVarLong(¶ms.LatestFlag, "latest", 'u', "Get latest stable version") getopt.StringVarLong(¶ms.LatestPre, "latest-pre", 'p', "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)") getopt.StringVarLong(¶ms.LatestStable, "latest-stable", 's', "Latest implicit version based on a constraint. Ex: tfswitch --latest-stable 0.13.0 downloads 0.13.7 and 0.13 downloads 0.15.5 (latest)") @@ -83,6 +85,7 @@ func initParams(params Params) Params { params.CustomBinaryPath = lib.ConvertExecutableExt(lib.GetDefaultBin()) params.DefaultVersion = lib.DefaultLatest params.HelpFlag = false + params.InstallPath = lib.GetHomeDirectory() params.LatestFlag = false params.LatestPre = lib.DefaultLatest params.LatestStable = lib.DefaultLatest diff --git a/lib/symlink.go b/lib/symlink.go index 91bd9052..88d7b99e 100644 --- a/lib/symlink.go +++ b/lib/symlink.go @@ -91,7 +91,6 @@ func CheckSymlink(symlinkPath string) bool { // ChangeSymlink : move symlink to existing binary func ChangeSymlink(binVersionPath string, binPath string) { - //installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file binPath = installableBinLocation(binPath) /* remove current symlink if exist*/ diff --git a/main.go b/main.go index 3d69004c..fd004d23 100644 --- a/main.go +++ b/main.go @@ -19,9 +19,10 @@ package main import ( "fmt" + "os" + lib "github.com/warrensbox/terraform-switcher/lib" "github.com/warrensbox/terraform-switcher/lib/param_parsing" - "os" ) var parameters = param_parsing.GetParameters() @@ -43,32 +44,32 @@ func main() { os.Exit(0) case parameters.ListAllFlag: /* show all terraform version including betas and RCs*/ - lib.InstallOption(true, parameters.CustomBinaryPath, parameters.MirrorURL) + lib.InstallOption(true, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) case parameters.LatestPre != "": /* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ - lib.InstallLatestImplicitVersion(parameters.LatestPre, parameters.CustomBinaryPath, parameters.MirrorURL, true) + lib.InstallLatestImplicitVersion(parameters.LatestPre, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, true) case parameters.ShowLatestPre != "": /* show latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */ lib.ShowLatestImplicitVersion(parameters.ShowLatestPre, parameters.MirrorURL, true) case parameters.LatestStable != "": /* latest implicit version. Ex: tfswitch --latest-stable 0.13 downloads 0.13.5 (latest) */ - lib.InstallLatestImplicitVersion(parameters.LatestStable, parameters.CustomBinaryPath, parameters.MirrorURL, false) + lib.InstallLatestImplicitVersion(parameters.LatestStable, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL, false) case parameters.ShowLatestStable != "": /* show latest implicit stable version. Ex: tfswitch --show-latest-stable 0.13 downloads 0.13.5 (latest) */ lib.ShowLatestImplicitVersion(parameters.ShowLatestStable, parameters.MirrorURL, false) case parameters.LatestFlag: /* latest stable version */ - lib.InstallLatestVersion(parameters.CustomBinaryPath, parameters.MirrorURL) + lib.InstallLatestVersion(parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) case parameters.ShowLatestFlag: /* show latest stable version */ lib.ShowLatestVersion(parameters.MirrorURL) case parameters.Version != "": - lib.InstallVersion(parameters.Version, parameters.CustomBinaryPath, parameters.MirrorURL) + lib.InstallVersion(parameters.Version, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) case parameters.DefaultVersion != "": /* if default version is provided - Pick this instead of going for prompt */ - lib.InstallVersion(parameters.DefaultVersion, parameters.CustomBinaryPath, parameters.MirrorURL) + lib.InstallVersion(parameters.DefaultVersion, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) default: // Set list all false - only official release will be displayed - lib.InstallOption(false, parameters.CustomBinaryPath, parameters.MirrorURL) + lib.InstallOption(false, parameters.CustomBinaryPath, parameters.InstallPath, parameters.MirrorURL) } } diff --git a/www/docs/usage/commandline.md b/www/docs/usage/commandline.md index 664a9619..dc6de05f 100644 --- a/www/docs/usage/commandline.md +++ b/www/docs/usage/commandline.md @@ -17,47 +17,74 @@ The most recently selected versions are presented at the top of the dropdown. ## See all versions including beta, alpha and release candidates(rc) drawing -1. Display all versions including beta, alpha and release candidates(rc). +1. Display all versions including beta, alpha and release candidates(rc). 2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions. 3. Hit **Enter** to select the desired version. ## Use environment variables -You can also set the `TF_VERSION` environment variable to your desired terraform version. -For example: + +You can also set the `TF_VERSION` environment variable to your desired terraform version. +For example: + ```bash export TF_VERSION=0.14.4 tfswitch #will automatically switch to terraform version 0.14.4 ``` + ## Install latest version only + 1. Install the latest stable version only. 2. Run `tfswitch -u` or `tfswitch --latest`. 3. Hit **Enter** to install. ## Install latest implicit version for stable releases + 1. Install the latest implicit stable version. 2. Ex: `tfswitch -s 0.13` or `tfswitch --latest-stable 0.13` downloads 0.13.6 (latest) version. 3. Hit **Enter** to install. ## Install latest implicit version for beta, alpha and release candidates(rc) + 1. Install the latest implicit pre-release version. 2. Ex: `tfswitch -p 0.13` or `tfswitch --latest-pre 0.13` downloads 0.13.0-rc1 (latest) version. 3. Hit **Enter** to install. ## Show latest version only + 1. Just show what the latest version is. 2. Run `tfswitch -U` or `tfswitch --show-latest` 3. Hit **Enter** to show. ## Show latest implicit version for stable releases + 1. Show the latest implicit stable version. 2. Ex: `tfswitch -S 0.13` or `tfswitch --show-latest-stable 0.13` shows 0.13.6 (latest) version. 3. Hit **Enter** to show. ## Show latest implicit version for beta, alpha and release candidates(rc) + 1. Show the latest implicit pre-release version. 2. Ex: `tfswitch -P 0.13` or `tfswitch --show-latest-pre 0.13` shows 0.13.0-rc1 (latest) version. 3. Hit **Enter** to show. -## Use custom mirror -To install from a remote mirror other than the default(https://releases.hashicorp.com/terraform). Use the `-m` or `--mirror` parameter. -Ex: `tfswitch --mirror https://example.jfrog.io/artifactory/hashicorp` +## Use custom mirror + +To install from a remote mirror other than the default(https://releases.hashicorp.com/terraform). Use the `-m` or `--mirror` parameter. + +```bash +tfswitch --mirror https://example.jfrog.io/artifactory/hashicorp` +``` + +## Install to non-default location + +By default `tfswitch` will download the Terraform binary to the user home directory under this path: `/Users/warrenveerasingam/.terraform.versions` + +If you want to install the binaries outside of the home directory then you can provide the `-i` or `--install` to install Terraform binaries to a non-standard path. Useful if you want to install versions of Terraform that can be shared with multiple users. + +The Terraform binaries will then be placed in the directory `.terraform.versions` under the custom install path e.g. `/opt/terraform/.terraform.versions` + +```bash +tfswitch -i /opt/terraform +``` + +**NOTE** - The directory passed in `-i`/`--install` must be created before running `tfswtich`