---
description: >
  Ensure the right version of Go is installed and set GOPATH to $HOME/go.
parameters:
  GOPROXY:
    description: >
      Set GOPROXY. By default this is set to "off" meaning you have to have all modules pre-downloaded.
    type: string
    default: "off"
  GOPRIVATE:
    description: Set GOPRIVATE, defaults to github.com/hashicorp/*
    type: string
    default: github.com/hashicorp/*
steps:
  - run:
      name: Setup Go
      command: |
        GO_VERSION=$(cat .go-version)
        [ -n "$GO_VERSION" ] || { echo "You must set GO_VERSION"; exit 1; }
        # Install Go
        cd ~
        curl -sSLO "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz"
        sudo rm -rf /usr/local/go
        sudo tar -C /usr/local -xzf "go${GO_VERSION}.linux-amd64.tar.gz"
        rm -f "go${GO_VERSION}.linux-amd64.tar.gz"
        GOPATH="/home/circleci/go"
        mkdir $GOPATH 2>/dev/null || { sudo mkdir $GOPATH && sudo chmod 777 $GOPATH; }
        mkdir $GOPATH/bin 2>/dev/null || { sudo mkdir $GOPATH/bin && sudo chmod 777 $GOPATH/bin; }
        echo "export GOPATH='$GOPATH'" >> "$BASH_ENV"
        echo "export PATH='$PATH:$GOPATH/bin:/usr/local/go/bin'" >> "$BASH_ENV"
        echo "export GOPROXY=<<parameters.GOPROXY>>" >> "$BASH_ENV"
        echo "export GOPRIVATE=<<parameters.GOPRIVATE>>" >> "$BASH_ENV"

        echo "$ go version"
        go version
