plugins/go: simplify _bash-it-gopath-pathmunge()

pull/2062/head
John D Pell 2021-09-23 22:01:04 -07:00
parent 3573275c5d
commit 2e143cf3ff
2 changed files with 14 additions and 17 deletions

View File

@ -11,28 +11,25 @@ if ! _binary_exists go || ! go version &> /dev/null; then
return 1
fi
export GOROOT="${GOROOT:-$(go env GOROOT)}"
export GOPATH="${GOPATH:-$(go env GOPATH)}"
: "${GOROOT:=$(go env GOROOT)}"
: "${GOPATH:=$(go env GOPATH)}"
export GOROOT GOPATH
# $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
function _bash-it-gopath-pathmunge() {
function _bash-it-component-plugin-callback-on-init-go() {
_about 'Ensures paths in GOPATH are added to PATH using pathmunge, with /bin appended'
_group 'go'
if [[ -z $GOPATH ]]; then
echo 'GOPATH empty' >&2
if [[ -z "${GOPATH:-}" ]]; then
_log_warning 'GOPATH empty'
return 1
fi
local paths i
local paths apath
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" || true # ignore failures
fi
for apath in "${paths[@]}"; do
pathmunge "${apath}/bin" || true
done
}
_bash-it-gopath-pathmunge
_bash-it-component-plugin-callback-on-init-go

View File

@ -21,10 +21,10 @@ function setup_go_path()
# We test `go version` in each test to account for users with goenv and no system go.
@test 'ensure _bash-it-gopath-pathmunge is defined' {
@test 'ensure _bash-it-component-plugin-callback-on-init-go is defined' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
load ../../plugins/available/go.plugin
run type -t _bash-it-gopath-pathmunge
run type -t _bash-it-component-plugin-callback-on-init-go
assert_line 'function'
}
@ -55,7 +55,7 @@ function setup_go_path()
setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/gopath2"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath2/bin:$BASH_IT/test/fixtures/go/gopath/bin"
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath/bin:$BASH_IT/test/fixtures/go/gopath2/bin"
}
@test 'plugins go: multiple entries in GOPATH, with space' {
@ -63,7 +63,7 @@ function setup_go_path()
setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin:$BASH_IT/test/fixtures/go/gopath/bin"
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath/bin:$BASH_IT/test/fixtures/go/go path/bin"
}
@test 'plugins go: multiple entries in GOPATH, with escaped space' {