-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathcommon.bash
112 lines (92 loc) · 2.51 KB
/
common.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# File: common.bash
#
# Common bash routines.
#
# Script directory:
_sdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# debug "msg"
# Write a debug message to stderr.
debug()
{
if [ "$VERBOSE" == "true" ]; then
echo "DEBUG: $1" >&2
fi
}
# err "msg"
# Write and error message to stderr.
err()
{
echo "ERROR: $1" >&2
}
# get_go_version
# Read the project's Go version and return it in the GO_VERSION variable.
# On failure it will exit.
get_go_version() {
GO_VERSION=$(cat "${_sdir}/../.go-version")
if [ -z "$GO_VERSION" ]; then
err "Failed to detect the project's Go version"
exit 1
fi
}
# install_gvm
# Install gvm to /usr/local/bin.
# To read more about installing gvm in other platforms: https://github.com/andrewkroh/gvm#installation
install_gvm() {
# Install gvm
if [ ! -f "/usr/local/bin/gvm" ]; then
curl -sL -o ~/bin/gvm https://github.com/andrewkroh/gvm/releases/download/v0.3.0/gvm-linux-amd64
chmod +x /usr/local/bin/gvm
fi
GVM="/usr/local/bin/gvm"
debug "Gvm version $(${GVM} --version)"
}
# setup_go_root "version"
# This configures the Go version being used. It sets GOROOT and adds
# GOROOT/bin to the PATH. It uses gimme to download the Go version if
# it does not already exist in the ~/.gimme dir.
setup_go_root() {
local version=${1}
install_gvm
# Setup GOROOT and add go to the PATH.
eval "$(${GVM} ${version})"
debug "$(go version)"
}
# setup_go_path "gopath"
# This sets GOPATH and adds GOPATH/bin to the PATH.
setup_go_path() {
local gopath="${1}"
if [ -z "$gopath" ]; then return; fi
# Setup GOPATH.
export GOPATH="${gopath}"
# Add GOPATH to PATH.
export PATH="${GOPATH}/bin:${PATH}"
debug "GOPATH=${GOPATH}"
}
jenkins_setup() {
: "${HOME:?Need to set HOME to a non-empty value.}"
: "${WORKSPACE:?Need to set WORKSPACE to a non-empty value.}"
if [ -z ${GO_VERSION:-} ]; then
get_go_version
fi
# Setup Go.
export GOPATH=${WORKSPACE}
export PATH=${GOPATH}/bin:${PATH}
eval "$(gvm ${GO_VERSION})"
# Workaround for Python virtualenv path being too long.
export TEMP_PYTHON_ENV=$(mktemp -d)
export PYTHON_ENV="${TEMP_PYTHON_ENV}/python-env"
# Write cached magefile binaries to workspace to ensure
# each run starts from a clean slate.
export MAGEFILE_CACHE="${WORKSPACE}/.magefile"
}
docker_setup() {
OS="$(uname)"
case $OS in
'Darwin')
# Start the docker machine VM (ignore error if it's already running).
docker-machine start default || true
eval $(docker-machine env default)
;;
esac
}