This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
forked from buzzfeed/sso
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpm
executable file
·130 lines (110 loc) · 3.79 KB
/
gpm
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
set -eu
# GPM commit when fetched from https://github.com/pote/gpm
GPM_VERSION="v1.4.0"
GPM_COMMIT="8218c4175bd4a1496c964e62e7af9b505e91e318"
## Functions/
usage() {
cat << EOF
SYNOPSIS
gpm leverages the power of the go get command and the underlying version
control systems used by it to set your Go dependencies to desired versions,
thus allowing easily reproducible builds in your Go projects.
A Godeps file in the root of your Go application is expected containing
the import paths of your packages and a specific tag or commit hash
from its version control system, an example Godeps file looks like this:
$ cat Godeps
# This is a comment
github.com/nu7hatch/gotrail v0.0.2
github.com/replicon/fast-archiver v1.02 #This is another comment!
github.com/nu7hatch/gotrail 2eb79d1f03ab24bacbc32b15b75769880629a865
gpm has a companion tool, called [gvp](https://github.com/pote/gvp) which
provides vendoring functionalities, it alters your GOPATH so every project
has its own isolated dependency directory, its usage is recommended.
USAGE
$ gpm # Same as 'gpm get'.
$ gpm get # Parses the Godeps file, gets dependencies and sets them
# to the appropriate version but does not install them.
$ gpm install # Parses the Godeps file, installs dependencies, sets
# them to the appropriate version and precompiles/installs them.
$ gpm version # Outputs version information
$ gpm help # Prints this message
EOF
}
is_in_use() {
[[ -e "$1/.git/index.lock" || -e "$1/.hg/store/lock" || -e "$1/.bzr/checkout/lock" ]]
}
# Exit with an error message to stderr. An optional second argument sets the
# exit status (defaults to `1`).
abort() {
echo "$1" >&2;
exit ${2:-1};
}
# Download dependencies and set their current versions to those specified in
# the Godeps file (the path to which should be passed as the first argument).
get_dependencies() {
(command -v go >/dev/null) ||
abort ">> Go is currently not installed or in your PATH"
[ -z "${GOPATH:-}" ] && abort ">> GOPATH is not set"
[[ -r "$1" ]] || abort ">> $1 file does not exist."
local deps=$(sed 's/#.*//;/^\s*$/d' < "$1") || echo ""
while read package _; do
(
echo ">> Getting package "$package"" >&3
go get -u -d "$package"
) &
done < <(echo "$deps")
wait
while read package version; do
(
local pkg_path=$(echo "$package" | awk -F/ '{print $1"/"$2"/"$3}')
local install_path="${GOPATH%%:*}/src/${pkg_path%%/...}"
echo ">> Setting $package to version $version" >&3
cd "$install_path"
is_in_use $install_path && wait
[ -d .bzr ] && bzr revert -q -r "$version"
[ -d .git ] && git checkout -q "$version"
[ -d .hg ] && hg update -q "$version"
[ -d .svn ] && svn update -r "$version"
) &
done < <(echo "$deps")
wait
echo "$deps"
}
# Reads dependencies from input and installs the packages.
install_dependencies() {
while read package _; do
echo ">> Building package "$package"" >&3
go install "$package"
done
echo ">> All Done" >&3
}
## /Functions
## Command Line Parsing
case "${1:-"get"}" in
"version")
echo ">> gpm $GPM_VERSION commit:$GPM_COMMIT"
;;
"install")
(get_dependencies "${2:-"Godeps"}" | install_dependencies) 3>&1
;;
"get")
get_dependencies "${2:-"Godeps"}" 3>&1 >/dev/null
;;
"help")
usage
;;
*)
## Support for Plugins: if command is unknown search for a gpm-command executable.
if command -v "gpm-$1" > /dev/null
then
plugin=$1 &&
shift &&
gpm-$plugin $@ &&
exit
else
echo ">> No command 'gpm $1'" >&2
usage && exit 1
fi
;;
esac