├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ └── main.yml ├── .gitignore ├── .shellspec ├── LICENSE ├── Makefile ├── README.md ├── bin ├── download ├── exec-env ├── help.deps ├── help.links ├── help.overview ├── install ├── latest-stable ├── list-all ├── list-bin-paths ├── list-legacy-filenames ├── parse-legacy-file └── uninstall ├── lib └── helpers.sh ├── renovate.json ├── set-env.bash ├── set-env.fish ├── set-env.nu ├── set-env.zsh ├── spec └── spec_helper.sh └── test-fixtures ├── .go-version ├── create-dummy-installs.sh └── go.mod /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @asdf-community/asdf-golang 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | # Triggers the workflow on push or pull request events but only for the master branch 4 | push: 5 | 6 | pull_request: 7 | 8 | # Allows you to run this workflow manually from the Actions tab 9 | workflow_dispatch: 10 | 11 | jobs: 12 | plugin_test: 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | os: 17 | - macos-latest 18 | - ubuntu-latest 19 | 20 | runs-on: ${{ matrix.os }} 21 | 22 | steps: 23 | - name: Checkout code 24 | uses: actions/checkout@v4 25 | 26 | - name: 🏗️ Create dummy install directories 27 | run: | 28 | ./test-fixtures/create-dummy-installs.sh 29 | 30 | - name: Test parsing 'go.mod' 31 | run: | 32 | bin/parse-legacy-file test-fixtures/go.mod | grep "^1.19.3" 33 | - name: Test parsing '.go-version' 34 | run: | 35 | bin/parse-legacy-file test-fixtures/.go-version | grep "^1.17.13" 36 | - name: Test plugin works to get latest version 37 | uses: asdf-vm/actions/plugin-test@v3 38 | with: 39 | command: go version 40 | 41 | - name: Checkout code 42 | uses: actions/checkout@v4 43 | 44 | - name: Run ShellSpec 45 | run: | 46 | curl -fsSL https://git.io/shellspec | sh -s 0.28.1 -p ./tools -y 47 | ./tools/lib/shellspec/bin/shellspec 48 | 49 | lint: 50 | runs-on: ubuntu-latest 51 | 52 | steps: 53 | - name: Checkout code 54 | uses: actions/checkout@v4 55 | 56 | - name: Run ShellCheck 57 | run: shellcheck bin/* 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /asdf/ 2 | -------------------------------------------------------------------------------- /.shellspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | 3 | ## Default kcov (coverage) options 4 | # --kcov-options "--include-path=. --path-strip-level=1" 5 | # --kcov-options "--include-pattern=.sh" 6 | # --kcov-options "--exclude-pattern=/.shellspec,/spec/,/coverage/,/report/" 7 | 8 | ## Example: Include script "myprog" with no extension 9 | # --kcov-options "--include-pattern=.sh,myprog" 10 | 11 | ## Example: Only specified files/directories 12 | # --kcov-options "--include-pattern=myprog,/lib/" 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Kenny Parnell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SRCFILES = $(shell git ls-files "bin/**" "lib/**" "spec/**" "test-fixtures/*.sh") 2 | SHFMT_BASE_FLAGS = -s -i 2 -ci 3 | 4 | format: 5 | shfmt -w $(SHFMT_BASE_FLAGS) $(SRCFILES) 6 | .PHONY: format 7 | 8 | format-check: 9 | shfmt -d $(SHFMT_BASE_FLAGS) $(SRCFILES) 10 | .PHONY: format-check 11 | 12 | lint: 13 | shellcheck -x $(SRCFILES) 14 | .PHONY: lint 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # asdf-golang 2 | 3 | [![CI](https://github.com/asdf-community/asdf-golang/actions/workflows/main.yml/badge.svg)](https://github.com/asdf-community/asdf-golang/actions/workflows/main.yml) 4 | 5 | golang plugin for [asdf version manager](https://github.com/asdf-vm/asdf) 6 | 7 | ## Requirements 8 | 9 | ### MacOS 10 | 11 | * [GNU Core Utils](http://www.gnu.org/software/coreutils/coreutils.html) - `brew install coreutils` 12 | 13 | ### Linux (Debian) 14 | 15 | * [GNU Core Utils](http://www.gnu.org/software/coreutils/coreutils.html) - `apt install coreutils` 16 | * [curl](https://curl.haxx.se) - `apt install curl` 17 | 18 | ## Install 19 | 20 | ```bash 21 | asdf plugin add golang https://github.com/asdf-community/asdf-golang.git 22 | ``` 23 | 24 | ## Use 25 | 26 | To ensure the Golang environment variables are correctly set when using the `asdf` Go plugin (`asdf-golang`), you should source the appropriate `set-env` script for your shell. This is particularly important if you've customized the `asdf` data directory using the `ASDF_DATA_DIR` environment variable. Below are instructions for various shells: 27 | 28 | - **Zsh (`.zshrc`):** 29 | 30 | ```bash 31 | . ${ASDF_DATA_DIR:-$HOME/.asdf}/plugins/golang/set-env.zsh 32 | ``` 33 | 34 | - **Bash (`.bashrc`):** 35 | 36 | ```bash 37 | . ${ASDF_DATA_DIR:-$HOME/.asdf}/plugins/golang/set-env.bash 38 | ``` 39 | 40 | - **Fish (`config.fish`):** 41 | 42 | ```fish 43 | source (echo $ASDF_DATA_DIR | if test -z $it; echo $HOME/.asdf; else echo $it; end)/plugins/golang/set-env.fish 44 | ``` 45 | 46 | - **Nushell (`env.nu`):** 47 | 48 | ```nu 49 | source (if ($env.ASDF_DATA_DIR | empty?) { echo $nu.env.HOME/.asdf } { echo $env.ASDF_DATA_DIR })/plugins/golang/set-env.nu 50 | ``` 51 | 52 | ## When using `go get` or `go install` 53 | 54 | After using `go get` or `go install` to install a package you need to run `asdf reshim golang` to get any new shims. 55 | 56 | ### Default `go get` packages 57 | 58 | asdf-golang can automatically install a default set of packages with `go get -u $PACKAGE` right after installing a new Go version. 59 | To enable this feature, provide a \$HOME/.default-golang-pkgs file that lists one package per line, for example: 60 | 61 | ```bash 62 | // allows comments 63 | github.com/Dreamacro/clash 64 | github.com/jesseduffield/lazygit 65 | ``` 66 | 67 | You can specify a non-default location of this file by setting a `ASDF_GOLANG_DEFAULT_PACKAGES_FILE` variable. 68 | 69 | ## Version selection 70 | 71 | When using `.tool-versions` or `.go-version`, the exact version specified in the 72 | file will be selected. 73 | 74 | When using `go.mod`, the highest compatible version that is currently installed 75 | will be selected. As per the [Go modules 76 | reference](https://golang.org/ref/mod#go-mod-file-go), that is the highest minor 77 | version with a matching major version. For example, a `go 1.14` directive in a 78 | `go.mod` file will result in the highest installed `1.minor.patch` being 79 | selected, not necessarily `1.14.patch`. 80 | 81 | **Note**: Users can explicitly exclude or include `go.mod` and `go.work` by 82 | setting `ASDF_GOLANG_MOD_VERSION_ENABLED`. Currently it defaults to `true`, but that 83 | may change in the future, so it should be explicitly set. 84 | 85 | ## Architecture Override 86 | 87 | The `ASDF_GOLANG_OVERWRITE_ARCH` variable can be used to override the architecture 88 | that is used for determining which Go build to download. The primary use case is when attempting 89 | to install an older version of Go for use on an Apple M1 computer as Go was not being built for ARM at the time. 90 | 91 | #### Without ASDF_GOLANG_OVERWRITE_ARCH 92 | 93 | ``` 94 | > asdf install golang 1.15.8 95 | Platform 'darwin' supported! 96 | URL: https://dl.google.com/go/go1.15.8.darwin-arm64.tar.gz returned status 404 97 | ``` 98 | 99 | #### With ASDF_GOLANG_OVERWRITE_ARCH 100 | 101 | ``` 102 | > ASDF_GOLANG_OVERWRITE_ARCH=amd64 asdf install golang 1.15.8 103 | Platform 'darwin' supported! 104 | % Total % Received % Xferd Average Speed Time Time Time Current 105 | Dload Upload Total Spent Left Speed 106 | 100 116M 100 116M 0 0 98.6M 0 0:00:01 0:00:01 --:--:-- 98.6M 107 | verifying checksum 108 | /Users//.asdf/downloads/golang/1.15.8/archive.tar.gz: OK 109 | checksum verified 110 | ``` 111 | 112 | ## Skipping Checksums 113 | 114 | By default we try to verify the checksum of each install but ocassionally [that's not possible](https://github.com/asdf-community/asdf-golang/issues/91). 115 | If you need to skip the checksum for some reason just set `ASDF_GOLANG_SKIP_CHECKSUM`. 116 | 117 | ## Contributing 118 | 119 | Feel free to create an issue or pull request if you find a bug. 120 | 121 | ## Issues 122 | 123 | * Assumes Linux, FreeBSD, or Mac 124 | * Assumes x86_64, i386, i686, armv6l, armv7l, arm64 and ppc64le 125 | 126 | ## License 127 | 128 | MIT License 129 | -------------------------------------------------------------------------------- /bin/download: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.." 6 | 7 | # shellcheck source=/dev/null 8 | source "$PLUGIN_DIR/lib/helpers.sh" 9 | 10 | check_shasum() { 11 | local archive_file_name=$1 12 | local authentic_checksum_file=$2 13 | local authentic_checksum="" 14 | 15 | authentic_checksum=$(<"$authentic_checksum_file") 16 | 17 | if command -v sha256sum >/dev/null 2>&1; then 18 | sha256sum \ 19 | -c <(echo "$authentic_checksum $archive_file_name") 20 | elif command -v shasum >/dev/null 2>&1; then 21 | shasum \ 22 | -a 256 \ 23 | -c <(echo "$authentic_checksum $archive_file_name") 24 | else 25 | fail "sha256sum or shasum is not available for use" 26 | fi 27 | } 28 | 29 | download_golang() { 30 | local version=$1 31 | local download_path=$2 32 | local platform="" 33 | local arch="" 34 | 35 | platform=$(get_platform) 36 | arch=$(get_arch) 37 | download_url="https://dl.google.com/go/go${version}.${platform}-${arch}.tar.gz" 38 | 39 | http_code=$(curl -I -w '%{http_code}' -s -o /dev/null "$download_url") 40 | if [ "$http_code" -eq 404 ] || [ "$http_code" -eq 403 ]; then 41 | fail "URL: ${download_url} returned status ${http_code}" 42 | fi 43 | 44 | curl "$download_url" -o "${download_path}/archive.tar.gz" 45 | 46 | if [ "unset" = "${ASDF_GOLANG_SKIP_CHECKSUM:-unset}" ]; then 47 | curl "${download_url}.sha256" -o "${download_path}/archive.tar.gz.sha256" 48 | 49 | echo 'verifying checksum' 50 | if ! check_shasum "${download_path}/archive.tar.gz" "${download_path}/archive.tar.gz.sha256"; then 51 | fail "Authenticity of package archive can not be assured. Exiting." 52 | else 53 | msg "checksum verified" 54 | fi 55 | else 56 | err "checksum skipped" 57 | fi 58 | } 59 | 60 | download_golang "$ASDF_INSTALL_VERSION" "$ASDF_DOWNLOAD_PATH" 61 | -------------------------------------------------------------------------------- /bin/exec-env: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ "${ASDF_INSTALL_VERSION}" != 'system' ]; then 4 | if [[ "unset" == "${GOROOT:-unset}" ]]; then 5 | export GOROOT=$ASDF_INSTALL_PATH/go 6 | fi 7 | 8 | if [[ "unset" == "${GOPATH:-unset}" ]]; then 9 | export GOPATH=$ASDF_INSTALL_PATH/packages 10 | fi 11 | 12 | if [[ "unset" == "${GOBIN:-unset}" ]]; then 13 | export GOBIN=$ASDF_INSTALL_PATH/bin 14 | fi 15 | fi 16 | -------------------------------------------------------------------------------- /bin/help.deps: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.." 6 | 7 | # shellcheck source=/dev/null 8 | source "$PLUGIN_DIR/lib/helpers.sh" 9 | 10 | if [ "$(get_platform silent)" = "darwin" ]; then 11 | echo "coreutils" 12 | else 13 | echo "coreutils" 14 | echo "curl" 15 | fi 16 | -------------------------------------------------------------------------------- /bin/help.links: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.." 6 | 7 | # shellcheck source=/dev/null 8 | source "$PLUGIN_DIR/lib/helpers.sh" 9 | 10 | cat <<-EOM 11 | Plugin: https://github.com/asdf-community/asdf-golang 12 | Go: https://golang.org/ 13 | GNU Core Utils: http://www.gnu.org/software/coreutils/coreutils.html 14 | curl: https://curl.haxx.se 15 | EOM 16 | -------------------------------------------------------------------------------- /bin/help.overview: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.." 6 | 7 | # shellcheck source=/dev/null 8 | source "$PLUGIN_DIR/lib/helpers.sh" 9 | 10 | cat <<-EOM 11 | Manage Go installs with ASDF 12 | 13 | - https://github.com/asdf-community/asdf-golang/ 14 | - https://golang.org/ 15 | EOM 16 | -------------------------------------------------------------------------------- /bin/install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.." 6 | 7 | # shellcheck source=/dev/null 8 | source "$PLUGIN_DIR/lib/helpers.sh" 9 | 10 | install_golang() { 11 | local version="$1" 12 | local download_path="$2" 13 | local install_path="$3" 14 | created_tmp="0" 15 | 16 | if [ -z "$download_path" ]; then 17 | download_path=$(mktemp -dt asdf-golang.XXXX) 18 | created_tmp="1" 19 | ASDF_INSTALL_VERSION="$version" ASDF_DOWNLOAD_PATH="$download_path" "$PLUGIN_DIR/bin/download" 20 | fi 21 | 22 | tar -C "$install_path" -xzf "${download_path}/archive.tar.gz" 23 | 24 | if [ "1" = "$created_tmp" ]; then 25 | rm -r "$download_path" 26 | fi 27 | } 28 | 29 | install_default_go_pkgs() { 30 | local go_path="$1/go/bin" 31 | local default_go_pkgs="${ASDF_GOLANG_DEFAULT_PACKAGES_FILE:-${HOME}/.default-golang-pkgs}" 32 | IFS=. read -r go_major_version go_minor_version <<<"${2}" 33 | 34 | if [ ! -f "$default_go_pkgs" ]; then return; fi 35 | 36 | while read -r line; do 37 | name=$(echo "$line" | 38 | sed 's|\(.*\) //.*$|\1|' | 39 | sed -E 's|^[[:space:]]*//.*||') # the first sed is for comments after package names, the second for full line comments 40 | 41 | # Skip empty lines 42 | if [ -z "$name" ]; then continue; fi 43 | 44 | echo -ne "\nInstalling \033[33m${name}\033[39m go pkg... " >&2 45 | 46 | # if using go > 1.16 then use go install as the preferred donwload path 47 | if [ "$go_major_version" -ge 2 ] || [ "${go_minor_version//[!0-9]*/}" -ge 16 ]; then 48 | if [[ $name != *"@"* ]]; then 49 | name="${name}@latest" 50 | fi 51 | 52 | GOROOT="$ASDF_INSTALL_PATH/go" \ 53 | GOPATH="$ASDF_INSTALL_PATH/packages" \ 54 | GOBIN="$ASDF_INSTALL_PATH/bin" \ 55 | PATH="$go_path:$PATH" \ 56 | go install "$name" >/dev/null && rc=$? || rc=$? 57 | else 58 | GOROOT="$ASDF_INSTALL_PATH/go" \ 59 | GOPATH="$ASDF_INSTALL_PATH/packages" \ 60 | PATH="$go_path:$PATH" \ 61 | GOBIN="$ASDF_INSTALL_PATH/bin" \ 62 | go get -u "$name" >/dev/null && rc=$? || rc=$? 63 | fi 64 | 65 | if [[ $rc -eq 0 ]]; then 66 | msg "SUCCESS" 67 | else 68 | err "FAIL" 69 | fi 70 | done <"$default_go_pkgs" 71 | } 72 | 73 | install_golang "$ASDF_INSTALL_VERSION" "${ASDF_DOWNLOAD_PATH:-}" "$ASDF_INSTALL_PATH" 74 | install_default_go_pkgs "$ASDF_INSTALL_PATH" "$ASDF_INSTALL_VERSION" 75 | -------------------------------------------------------------------------------- /bin/latest-stable: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")/.." 6 | 7 | # shellcheck source=/dev/null 8 | source "$PLUGIN_DIR/lib/helpers.sh" 9 | 10 | command eval "VERSIONS=($("$PLUGIN_DIR"/bin/list-all | tr ' ' '\n' | grep -ivE '(^Available versions:|-src|-dev|-latest|-stm|[-\.]rc|-alpha|-beta|[-\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)' | grep "^$1" | sed 's/^[[:space:]]\+//'))" 11 | 12 | platform=$(get_platform true) 13 | arch=$(get_arch) 14 | version="" 15 | 16 | for ((i = ${#VERSIONS[@]} - 1; i >= 0; i--)); do 17 | version="${VERSIONS[i]}" 18 | download_url="https://dl.google.com/go/go${version}.${platform}-${arch}.tar.gz" 19 | http_code=$(curl -I -w '%{http_code}' -s -o /dev/null "$download_url") 20 | if [ "$http_code" -ne 404 ] && [ "$http_code" -ne 403 ]; then 21 | break 22 | fi 23 | done 24 | 25 | printf "%s" "${version}" 26 | -------------------------------------------------------------------------------- /bin/list-all: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | git ls-remote --tags https://github.com/golang/go "go*" | 6 | awk -F/go '{ print $2 }' | 7 | uniq | 8 | sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | 9 | grep -v '^1\($\|\.0\|\.0\.[0-9]\|\.1\|\.1rc[0-9]\|\.1\.[0-9]\|.2\|\.2rc[0-9]\|\.2\.1\|.8.5rc5\)$' | 10 | tr '\n' ' ' 11 | -------------------------------------------------------------------------------- /bin/list-bin-paths: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echo -n "bin go/bin" 4 | -------------------------------------------------------------------------------- /bin/list-legacy-filenames: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | case "${ASDF_GOLANG_MOD_VERSION_ENABLED:-}" in 4 | true) 5 | printf ".go-version go.mod go.work\n" 6 | ;; 7 | false) 8 | printf ".go-version\n" 9 | ;; 10 | *) 11 | cat >&2 <<-EOF 12 | Notice: Behaving like ASDF_GOLANG_MOD_VERSION_ENABLED=true 13 | In the future this will have to be set to continue 14 | reading from the go.mod and go.work files 15 | EOF 16 | printf ".go-version go.mod go.work\n" 17 | ;; 18 | esac 19 | -------------------------------------------------------------------------------- /bin/parse-legacy-file: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | plugin_dir() { 4 | local current_script_path=${BASH_SOURCE[0]} 5 | cd "$(dirname "$(dirname "$current_script_path")")" || exit 6 | pwd 7 | } 8 | 9 | install_dir() { 10 | printf -v pdir "%s" "$(plugin_dir)" 11 | printf -v plugin_name "%s" "$(basename "$pdir")" 12 | printf "%s/installs/%s" "$(dirname "$(dirname "$pdir")")" "$plugin_name" 13 | } 14 | 15 | installed_versions() { 16 | local plugin_installs_path 17 | plugin_installs_path="$(install_dir)" 18 | 19 | if [ -d "$plugin_installs_path" ]; then 20 | for install in "${plugin_installs_path}"/*/; do 21 | [[ -e $install ]] || break 22 | basename "$install" | sed 's/^ref-/ref:/' 23 | done 24 | fi 25 | } 26 | 27 | get_legacy_version() { 28 | current_file="$1" 29 | basename=$(basename -- "$current_file") 30 | 31 | if [[ $basename =~ ^go.(mod|work)$ ]]; then 32 | GOLANG_VERSION=$( 33 | grep 'go\s*[0-9]' "$current_file" | 34 | sed -E \ 35 | -e 's/.*heroku goVersion //' \ 36 | -e 's/[[:space:]]//' \ 37 | -e 's/go([0-9]+).*/\1/' | 38 | head -1 39 | ) 40 | elif [ -e "$current_file" ]; then 41 | GOLANG_VERSION=$(cat "$current_file") 42 | else 43 | GOLANG_VERSION="" 44 | fi 45 | 46 | local installed 47 | installed=$(installed_versions | sed -e 's/ //g' | grep "^$GOLANG_VERSION" | sort -V | tail -1) 48 | 49 | if [ -z "$installed" ]; then 50 | installed=$("$(plugin_dir)/bin/latest-stable" "$GOLANG_VERSION") 51 | fi 52 | 53 | echo "$installed" 54 | } 55 | 56 | get_legacy_version "$1" 57 | -------------------------------------------------------------------------------- /bin/uninstall: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ -d "${ASDF_INSTALL_PATH}/packages" ]; then 4 | chmod -R u+wx "${ASDF_INSTALL_PATH}/packages" 5 | fi 6 | 7 | rm -rf "${ASDF_INSTALL_PATH}" 8 | -------------------------------------------------------------------------------- /lib/helpers.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | [ "${BASH_VERSINFO[0]}" -ge 3 ] && set -o pipefail 4 | 5 | get_platform() { 6 | local silent=${1:-} 7 | local platform="" 8 | 9 | platform="$(uname | tr '[:upper:]' '[:lower:]')" 10 | 11 | case "$platform" in 12 | linux | darwin | freebsd) 13 | [ -z "$silent" ] && msg "Platform '${platform}' supported!" 14 | ;; 15 | *) 16 | fail "Platform '${platform}' not supported!" 17 | ;; 18 | esac 19 | 20 | printf "%s" "$platform" 21 | } 22 | 23 | get_arch() { 24 | local arch="" 25 | local arch_check=${ASDF_GOLANG_OVERWRITE_ARCH:-"$(uname -m)"} 26 | case "${arch_check}" in 27 | x86_64 | amd64) arch="amd64" ;; 28 | i686 | i386 | 386) arch="386" ;; 29 | armv6l | armv7l) arch="armv6l" ;; 30 | aarch64 | arm64) arch="arm64" ;; 31 | ppc64le) arch="ppc64le" ;; 32 | loongarch64 | loong64) arch="loong64" ;; 33 | riscv64) arch="riscv64" ;; 34 | *) 35 | fail "Arch '${arch_check}' not supported!" 36 | ;; 37 | esac 38 | 39 | printf "%s" "$arch" 40 | } 41 | 42 | msg() { 43 | echo -e "\033[32m$1\033[39m" >&2 44 | } 45 | 46 | err() { 47 | echo -e "\033[31m$1\033[39m" >&2 48 | } 49 | 50 | fail() { 51 | err "$1" 52 | exit 1 53 | } 54 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": ["config:base", "schedule:daily"] 4 | } 5 | -------------------------------------------------------------------------------- /set-env.bash: -------------------------------------------------------------------------------- 1 | asdf_update_golang_env() { 2 | local go_bin_path 3 | go_bin_path="$(asdf which go 2>/dev/null)" 4 | if [[ -n "${go_bin_path}" ]]; then 5 | abs_go_bin_path="$(readlink -f "${go_bin_path}")" 6 | 7 | export GOROOT 8 | GOROOT="$(dirname "$(dirname "${abs_go_bin_path}")")" 9 | 10 | export GOPATH 11 | GOPATH="$(dirname "${GOROOT}")/packages" 12 | 13 | export GOBIN 14 | GOBIN="$(dirname "${GOROOT}")/bin" 15 | fi 16 | } 17 | asdf_update_golang_env 18 | -------------------------------------------------------------------------------- /set-env.fish: -------------------------------------------------------------------------------- 1 | function asdf_update_golang_env --on-event fish_prompt 2 | set --local go_bin_path (asdf which go 2>/dev/null) 3 | if test -n "$go_bin_path" 4 | set --local full_path (builtin realpath "$go_bin_path") 5 | 6 | set -gx GOROOT (dirname (dirname "$full_path")) 7 | set -gx GOPATH (dirname "$GOROOT")/packages 8 | set -gx GOBIN (dirname "$GOROOT")/bin 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /set-env.nu: -------------------------------------------------------------------------------- 1 | $env.GOROOT = (asdf which go | path split | drop 2 | path join) 2 | -------------------------------------------------------------------------------- /set-env.zsh: -------------------------------------------------------------------------------- 1 | asdf_update_golang_env() { 2 | local go_bin_path 3 | go_bin_path="$(asdf which go 2>/dev/null)" 4 | if [[ -n "${go_bin_path}" ]]; then 5 | export GOROOT 6 | GOROOT="$(dirname "$(dirname "${go_bin_path:A}")")" 7 | 8 | export GOPATH 9 | GOPATH="$(dirname "${GOROOT:A}")/packages" 10 | 11 | export GOBIN 12 | GOBIN="$(dirname "${GOROOT:A}")/bin" 13 | fi 14 | } 15 | 16 | autoload -U add-zsh-hook 17 | add-zsh-hook precmd asdf_update_golang_env 18 | -------------------------------------------------------------------------------- /spec/spec_helper.sh: -------------------------------------------------------------------------------- 1 | # shellcheck shell=sh 2 | 3 | # Defining variables and functions here will affect all specfiles. 4 | # Change shell options inside a function may cause different behavior, 5 | # so it is better to set them here. 6 | # set -eu 7 | 8 | # This callback function will be invoked only once before loading specfiles. 9 | spec_helper_precheck() { 10 | # Available functions: info, warn, error, abort, setenv, unsetenv 11 | # Available variables: VERSION, SHELL_TYPE, SHELL_VERSION 12 | : minimum_version "0.28.1" 13 | } 14 | 15 | # This callback function will be invoked after a specfile has been loaded. 16 | spec_helper_loaded() { 17 | : 18 | } 19 | 20 | # This callback function will be invoked after core modules has been loaded. 21 | spec_helper_configure() { 22 | # Available functions: import, before_each, after_each, before_all, after_all 23 | : import 'support/custom_matcher' 24 | } 25 | -------------------------------------------------------------------------------- /test-fixtures/.go-version: -------------------------------------------------------------------------------- 1 | 1.17.13 2 | -------------------------------------------------------------------------------- /test-fixtures/create-dummy-installs.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | declare -a fake_install_versions=( 4 | 1.16 5 | 1.17.1 6 | 1.17.7 7 | 1.17.13 8 | 1.18.1 9 | 1.18.2 10 | 1.18.6 11 | 1.19 12 | 1.19.3 13 | ) 14 | 15 | mkdir -p ../../installs/asdf-golang 16 | for version in "${fake_install_versions[@]}"; do 17 | mkdir "../../installs/asdf-golang/${version}" 18 | done 19 | -------------------------------------------------------------------------------- /test-fixtures/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/asdf-community/asdf-golang 2 | 3 | go 1.18 4 | --------------------------------------------------------------------------------