Ensure goenv and go play together well
This PR seeks to address several issues surrounding the go and goenv plugins. The nature of goenv allows for a situation where the initial shell does not point to a working go binary, and instead at the included shim script. The result is that one must run reload after moving to a project directory with a version file, however in doing so, PATH is updated, requiring they exit the shell or risk lookup collisions and unexpected behavior. This is solved by using preexec to check the version before changing directories and restarting the shell if the version has changed. The exec pattern is copied from _bash-it-restart, but is edited to support this specific use case. Additionally, tests have been uploaded and these are now being linted.
This commit is contained in:
41
plugins/available/go.plugin.bash
Executable file → Normal file
41
plugins/available/go.plugin.bash
Executable file → Normal file
@@ -1,19 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# shellcheck shell=bash
|
||||
cite about-plugin
|
||||
about-plugin 'go environment variables & path configuration'
|
||||
|
||||
command -v go &>/dev/null || return
|
||||
# Load after basher and goenv
|
||||
# BASH_IT_LOAD_PRIORITY: 270
|
||||
|
||||
function _go_pathmunge_wrap() {
|
||||
IFS=':' local -a 'a=($1)'
|
||||
local i=${#a[@]}
|
||||
while [ $i -gt 0 ] ; do
|
||||
i=$(( i - 1 ))
|
||||
pathmunge "${a[i]}/bin"
|
||||
done
|
||||
}
|
||||
# Test `go version` because goenv creates shim scripts that will be found in PATH
|
||||
# but do not always resolve to a working install.
|
||||
{ _command_exists go && go version &> /dev/null; } || return 0
|
||||
|
||||
export GOROOT="${GOROOT:-$(go env GOROOT)}"
|
||||
export GOPATH="${GOPATH:-$(go env GOPATH)}"
|
||||
_go_pathmunge_wrap "${GOPATH}:${GOROOT}"
|
||||
|
||||
# $GOPATH/bin is the default location for binaries. Because GOPATH accepts a list of paths and each
|
||||
# might be managed differently, we add each path's /bin folder to PATH using pathmunge,
|
||||
# while preserving ordering.
|
||||
# e.g. GOPATH=foo:bar -> PATH=foo/bin:bar/bin
|
||||
_bash-it-gopath-pathmunge() {
|
||||
_about 'Ensures paths in GOPATH are added to PATH using pathmunge, with /bin appended'
|
||||
_group 'go'
|
||||
if [[ -z $GOPATH ]]; then
|
||||
echo 'GOPATH empty' >&2
|
||||
return 1
|
||||
fi
|
||||
local paths i
|
||||
IFS=: read -r -a paths <<< "$GOPATH"
|
||||
i=${#paths[@]}
|
||||
while [[ $i -gt 0 ]]; do
|
||||
i=$((i - 1))
|
||||
if [[ -n "${paths[i]}" ]]; then
|
||||
pathmunge "${paths[i]}/bin"
|
||||
fi
|
||||
done
|
||||
}
|
||||
_bash-it-gopath-pathmunge
|
||||
|
||||
Reference in New Issue
Block a user