Skip to content

Instantly share code, notes, and snippets.

@myitcv
Last active September 10, 2018 16:53
Show Gist options
  • Save myitcv/09dcc1fdb23997ce0a4aff40e22ef5ce to your computer and use it in GitHub Desktop.
Save myitcv/09dcc1fdb23997ce0a4aff40e22ef5ce to your computer and use it in GitHub Desktop.
"Global" module-aware install script for Go 1.11

Usage

./install.sh <pkg list>

Issues

  • Specific to git (fixable)
  • Does not support specification of a major import path (fixable)

Questions

  • Does not use any sort of "cache" for the initial go get -d (which is in effect an import-aware repo clone); should it? Or should this always hit the network (or GOPROXY if set)?
  • Whilst the import path would (when implemented) give us a major version if one were required, this script gives us the latest version within a major version - is this an desirable or an issue?
  • Do we want to keep track of the installed versions of a command somewhere? Should this be query-able?
#!/usr/bin/env bash
set -ue
set -o pipefail
pgb=$(go env GOBIN)
pgp=$(go env GOPATH)
pgpb=$(go env GOPATH | sed -e 's/:/\n/' | head -n 1)
tbin=$pgb
if [ "$tbin" == "" ]
then
tbin="$pgp/bin"
fi
tgp=$(mktemp -d)
trap "rm -rf $tgp" EXIT
export GOPATH=$tgp
cd $tgp
export GO111MODULE=on
GO111MODULE=off go get -d "$@"
for i in "$@"
do
pushd $(GO111MODULE=off go list -f "{{.Dir}}" $i) > /dev/null
if [ "$(go env GOMOD)" == "" ]
then
pushd $(git rev-parse --show-toplevel) > /dev/null
go mod init
popd > /dev/null
fi
GOBIN="$tbin" GOPATH="$pgp" go install $i
echo "Installed $i to $(GOBIN="$tbin" GOPATH="$pgp" go list -f '{{.Target}}')"
popd > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment