diff --git a/.circleci/config.yml b/.circleci/config.yml index 15a67064..a863c4ca 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -9,76 +9,43 @@ jobs: working_directory: /go/src/github.com/warrensbox/terraform-switcher - steps: - - checkout - - run: go get -v -t -d ./... - - run: go vet -tests=false ./... - - run: go test -v ./... - - run: mkdir -p build - - run: go build -v -o build/tfswitch - - - persist_to_workspace: - root: build - paths: tfswitch - - test-ubuntu: - docker: - - image: "circleci/ubuntu-server" - - steps: - - - run: ls - - - attach_workspace: - at: build - - test-alpine: - docker: - - image: "mhart/alpine-node" - - steps: - - run: ls - - develop-master: - docker: - - image: warrensbox/go-rb-py-aws - steps: - checkout - run: - command: | - echo "Placeholder" - # cd docs; bundle install --path vendor/bundler; bundle exec jekyll build -c _config.yml; cd .. - # git config user.email "warren.veerasingam@gmail.com" - # git config user.name "warrensbox" - # git add . - # git commit -m "Release Version" - # git push origin ${RELEASE_VERSION} - + command: | + set +e + go get -v -t -d ./... + go vet -tests=false ./... + go test -v ./... + mkdir -p build + go build -v -o build/tfswitch release: docker: - - image: warrensbox/go-rb-py-aws + - image: circleci/golang:1.11 + + working_directory: /go/src/github.com/warrensbox/terraform-switcher steps: - checkout - run: command: | - set +e + set +e source version export RELEASE_VERSION; RELEASE_VERSION=$RELEASE_VERSION.${CIRCLE_BUILD_NUM} export RELEASE_VERSION; echo $RELEASE_VERSION - - go get -v -t -d ./... + mkdir -p /home/circleci/bin + curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + export PATH="/home/warrensbox/bin:${PATH}" + rm -rf dist git config user.email "warren.veerasingam@gmail.com" git config user.name "warrensbox" git tag -a ${RELEASE_VERSION} -m "Release Version" git push origin ${RELEASE_VERSION} - curl -L https://git.io/goreleaser | VERSION=v0.76.1 bash + curl -L https://git.io/goreleaser | VERSION=v0.99.0 bash -s -- --rm-dist - cd docs; bundle install --path vendor/bundler; bundle exec jekyll build -c _config.yml; cd .. workflows: version: 2 @@ -86,24 +53,9 @@ workflows: build_and_test: jobs: - build - - test-ubuntu: - requires: - - build - - test-alpine: - requires: - - build - - develop-master: - requires: - - test-ubuntu - - test-alpine - filters: - branches: - only: - - master - - release - release: requires: - - develop-master + - build filters: branches: only: diff --git a/Makefile b/Makefile index 1f165c71..7f803816 100644 --- a/Makefile +++ b/Makefile @@ -31,3 +31,8 @@ test: $(EXE) .PHONEY: dep dep: dep ensure + +.PHONY: docs +docs: + cd docs; bundle install --path vendor/bundler; bundle exec jekyll build -c _config.yml; cd .. + diff --git a/README.md b/README.md index 3d3c26dd..2f5d02a5 100644 --- a/README.md +++ b/README.md @@ -89,12 +89,12 @@ version = "0.11.3" 2. For example, `echo "0.10.5" >> .tfswitchrc` for version 0.10.5 of terraform 3. Run the command `tfswitch` in the same directory as your `.tfswitchrc` -*instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it* +*Instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it* **Automatically switch with bash** Add the following to the end of your `~/.bashrc` file: -(Use either `.tfswitchrc` or `.tfswitch.toml`) +(Use either `.tfswitchrc` or `.tfswitch.toml` or `.terraform-version`) ``` cdtfswitch(){ @@ -138,6 +138,20 @@ cd(){ } ``` +### Jenkins setup +drawing + +``` +#!/bin/bash + +echo "Installing tfswitch locally" +wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh +chmod 755 install.sh +./install.sh -b bin-directory + +./bin-directory/tfswitch +``` + ## Additional Info See how to *upgrade*, *uninstall*, *troubleshoot* here: [More info](https://warrensbox.github.io/terraform-switcher/additional) diff --git a/docs/_site/additional.html b/docs/_site/additional.html index 7ad31181..8f502959 100644 --- a/docs/_site/additional.html +++ b/docs/_site/additional.html @@ -23,7 +23,7 @@ {"description":"Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform","@type":"WebPage","url":"/additional.html","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"/assets/img/logo.png"}},"headline":"tfswitch","@context":"http://schema.org"} - + diff --git a/docs/_site/index.html b/docs/_site/index.html index 2213cf3f..93cd3025 100644 --- a/docs/_site/index.html +++ b/docs/_site/index.html @@ -23,7 +23,7 @@ {"name":"tfswitch","description":"Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform","@type":"WebSite","url":"/","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"/assets/img/logo.png"}},"headline":"Terraform Switcher","@context":"http://schema.org"} - + @@ -162,10 +162,12 @@

Use .tfswitchrc file

  • Run the command tfswitch in the same directory as your .tfswitchrc.
  • +

    Instead of a .tfswitchrc file, a .terraform-version file may be used for compatibility with tfenv and other tools which use it

    +

    Automatically switch with bash

    Add the following to the end of your ~/.bashrc file: -(Use either .tfswitchrc or .tfswitch.toml)

    +(Use either .tfswitchrc or .tfswitch.toml or .terraform-version)

    cdtfswitch(){
       builtin cd "$@";
    @@ -207,6 +209,19 @@ 

    Use .tfswitchrc file

    }
    +

    Jenkins setup

    +

    drawing

    + +
    #!/bin/bash 
    +
    +echo "Installing tfswitch locally"
    +wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh 
    +chmod 755 install.sh
    +./install.sh -b bin-directory
    +
    +./bin-directory/tfswitch
    +
    +

    Issues

    diff --git a/docs/_site/index.md b/docs/_site/index.md index d97d1e8f..2450c92f 100644 --- a/docs/_site/index.md +++ b/docs/_site/index.md @@ -82,11 +82,12 @@ version = "0.11.3" 2. For example, `echo "0.10.5" >> .tfswitchrc` for version 0.10.5 of terraform. 3. Run the command `tfswitch` in the same directory as your `.tfswitchrc`. +*Instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it* **Automatically switch with bash** Add the following to the end of your `~/.bashrc` file: -(Use either `.tfswitchrc` or `.tfswitch.toml`) +(Use either `.tfswitchrc` or `.tfswitch.toml` or `.terraform-version`) ``` cdtfswitch(){ @@ -130,6 +131,20 @@ cd(){ } ``` +### Jenkins setup +drawing + +``` +#!/bin/bash + +echo "Installing tfswitch locally" +wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh +chmod 755 install.sh +./install.sh -b bin-directory + +./bin-directory/tfswitch +``` +
    ## Issues diff --git a/docs/index.md b/docs/index.md index d97d1e8f..2450c92f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -82,11 +82,12 @@ version = "0.11.3" 2. For example, `echo "0.10.5" >> .tfswitchrc` for version 0.10.5 of terraform. 3. Run the command `tfswitch` in the same directory as your `.tfswitchrc`. +*Instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it* **Automatically switch with bash** Add the following to the end of your `~/.bashrc` file: -(Use either `.tfswitchrc` or `.tfswitch.toml`) +(Use either `.tfswitchrc` or `.tfswitch.toml` or `.terraform-version`) ``` cdtfswitch(){ @@ -130,6 +131,20 @@ cd(){ } ``` +### Jenkins setup +drawing + +``` +#!/bin/bash + +echo "Installing tfswitch locally" +wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh +chmod 755 install.sh +./install.sh -b bin-directory + +./bin-directory/tfswitch +``` +
    ## Issues diff --git a/lib/install.go b/lib/install.go index 6d416a43..c3794d29 100644 --- a/lib/install.go +++ b/lib/install.go @@ -12,27 +12,21 @@ const ( hashiURL = "https://releases.hashicorp.com/terraform/" installFile = "terraform" installVersion = "terraform_" - //binLocation = "/usr/local/bin/terraform" - installPath = "/.terraform.versions/" - recentFile = "RECENT" + installPath = "/.terraform.versions/" + recentFile = "RECENT" ) var ( installLocation = "/tmp" - //installedBinPath = "/tmp" ) -func init() { - /* get current user */ - usr, errCurr := user.Current() - if errCurr != nil { - log.Fatal(errCurr) - } - - /* set installation location */ - installLocation = usr.HomeDir + installPath +// initialize : removes existing symlink to terraform binary +func initialize() { - /* set default binary path for terraform */ + /* Step 1 */ + /* initilize default binary path for terraform */ + /* assumes that terraform is installed here */ + /* we will find the terraform path instalation later and replace this variable with the correct installed bin path */ installedBinPath := "/usr/local/bin/terraform" /* find terraform binary location if terraform is already installed*/ @@ -45,16 +39,33 @@ func init() { installedBinPath = path } - /* remove current symlink if exist*/ + /* check if current symlink to terraform binary exist */ symlinkExist := CheckSymlink(installedBinPath) + /* remove current symlink if exist*/ if symlinkExist { RemoveSymlink(installedBinPath) } +} + +// getInstallLocation : get location where the terraform binary will be installed, +// will create a directory in the home location if it does not exist +func getInstallLocation() string { + /* get current user */ + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + + /* set installation location */ + installLocation = usr.HomeDir + installPath + /* Create local installation directory if it does not exist */ CreateDirIfNotExist(installLocation) + return installLocation + } //Install : Install the provided version in the argument @@ -65,6 +76,18 @@ func Install(tfversion string, binPath string) { os.Exit(1) } + pathDir := Path(binPath) //get path directory from binary path + binDirExist := CheckDirExist(pathDir) //check bin path exist + + if !binDirExist { + fmt.Printf("Error - Binary path does not exist: %s\n", pathDir) + fmt.Printf("Create binary path: %s for terraform installation\n", pathDir) + os.Exit(1) + } + + initialize() //initialize path + installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file + goarch := runtime.GOARCH goos := runtime.GOOS @@ -130,6 +153,8 @@ func Install(tfversion string, binPath string) { // AddRecent : add to recent file func AddRecent(requestedVersion string) { + installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file + fileExist := CheckFileExist(installLocation + recentFile) if fileExist { lines, errRead := ReadLines(installLocation + recentFile) @@ -170,6 +195,8 @@ func AddRecent(requestedVersion string) { // GetRecentVersions : get recent version from file func GetRecentVersions() ([]string, error) { + installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file + fileExist := CheckFileExist(installLocation + recentFile) if fileExist { @@ -205,5 +232,8 @@ func GetRecentVersions() ([]string, error) { //CreateRecentFile : create a recent file func CreateRecentFile(requestedVersion string) { + + installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file + WriteLines([]string{requestedVersion}, installLocation+recentFile) } diff --git a/lib/symlink.go b/lib/symlink.go index f0df72c4..ba7f8291 100644 --- a/lib/symlink.go +++ b/lib/symlink.go @@ -36,6 +36,7 @@ func RemoveSymlink(symlinkPath string) { os.Exit(1) } else { errRemove := os.Remove(symlinkPath) + if errRemove != nil { log.Fatalf(` Unable to remove symlink. diff --git a/main.go b/main.go index c1498d8f..445b0f56 100644 --- a/main.go +++ b/main.go @@ -107,14 +107,7 @@ func main() { tfversion = version.(string) } - pathDir := lib.Path(binPath) //get path directory from binary path - binDirExist := lib.CheckDirExist(pathDir) //check bin path exist - - if !binDirExist { - fmt.Printf("Binary path does not exist: %s\n", pathDir) - fmt.Printf("Create binary path: %s for terraform installation\n", pathDir) - os.Exit(1) - } else if *listAllFlag { //show all terraform version including betas and RCs + if *listAllFlag { //show all terraform version including betas and RCs listAll := true //set list all true - all versions including beta and rc will be displayed installOption(listAll, &binPath) } else if tfversion == "" { // if no version is provided, show a dropdown of available release versions @@ -130,7 +123,7 @@ func main() { } } else if _, err := os.Stat(rcfile); err == nil && len(args) == 0 { //if there is a .tfswitchrc file, and no commmand line arguments - fmt.Printf("Reading required terraform version %s ", rcFilename) + fmt.Printf("Reading required terraform version %s \n", rcFilename) fileContents, err := ioutil.ReadFile(rcfile) if err != nil { @@ -147,7 +140,7 @@ func main() { os.Exit(1) } } else if _, err := os.Stat(tfvfile); err == nil && len(args) == 0 { //if there is a .terraform-version file, and no command line arguments - fmt.Printf("Reading required terraform version %s ", tfvFilename) + fmt.Printf("Reading required terraform version %s \n", tfvFilename) fileContents, err := ioutil.ReadFile(tfvfile) if err != nil {