Skip to content

A classic way to detect installation

xhd2015 edited this page Apr 23, 2024 · 1 revision

The following script installs xgo and detect if GOPATH/bin is added to PATH

#!/usr/bin/env bash
set -e

tmp=$(mktemp -d)
cd "$tmp"

function rm_tmp  {
    rm -rf "$tmp"
}
trap rm_tmp EXIT

go install github.com/xhd2015/xgo/cmd/xgo@latest

# NOTE: if GOPATH is set to '', go env GOPATH defaults to ~/go
if ! command -v xgo >/dev/null;then
    GOPATH=$(go env GOPATH)
    if [[ -z $GOPATH ]];then
        sourceTip=$'You may need to set GOPATH, and add $GOPATH/bin to your PATH variable and shell profile:\n  export PATH="$(go env GOPATH)"/bin:$PATH'
    else
        GOPATH0=$(echo "$GOPATH"|cut -d ':' -f1)
        if [[ $GOPATH0 = $GOPATH ]];then
            sourceTip=$'You may need to add $GOPATH/bin to your PATH variable and shell profile:\n  export PATH="$(go env GOPATH)"/bin:$PATH'
        else
            sourceTip=$'You may need to add $GOPATH/bin to your PATH variable and shell profile:\n  export PATH="$(go env GOPATH|cut -d: -f1)"/bin:$PATH'
        fi
    fi
fi

echo "Successfully installed, to get started, run:"
echo "  xgo help"
if [[ -n "$sourceTip" ]];then
    echo ""
    echo "NOTE: $sourceTip"
fi
echo ""