diff --git a/plugins/available/go.plugin.bash b/plugins/available/go.plugin.bash index 73ee0e42..f9276d59 100644 --- a/plugins/available/go.plugin.bash +++ b/plugins/available/go.plugin.bash @@ -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 diff --git a/test/plugins/go.plugin.bats b/test/plugins/go.plugin.bats index 258e4254..1bdf902c 100644 --- a/test/plugins/go.plugin.bats +++ b/test/plugins/go.plugin.bats @@ -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' {