...
1#!/bin/sh
2set -e
3# Code generated by godownloader on 2018-06-05T12:04:55Z. DO NOT EDIT.
4#
5
6usage() {
7 this=$1
8 cat <<EOF
9$this: download go binaries for golangci/golangci-lint
10
11Usage: $this [-b] bindir [-d] [tag]
12 -b sets bindir or installation directory, Defaults to ./bin
13 -d turns on debug logging
14 [tag] is a tag from
15 https://github.com/golangci/golangci-lint/releases
16 If tag is missing, then the latest will be used.
17
18 Generated by godownloader
19 https://github.com/goreleaser/godownloader
20
21EOF
22 exit 2
23}
24
25parse_args() {
26 #BINDIR is ./bin unless set be ENV
27 # over-ridden by flag below
28
29 BINDIR=${BINDIR:-./bin}
30 while getopts "b:dh?" arg; do
31 case "$arg" in
32 b) BINDIR="$OPTARG" ;;
33 d) log_set_priority 10 ;;
34 h | \?) usage "$0" ;;
35 esac
36 done
37 shift $((OPTIND - 1))
38 TAG=$1
39}
40# this function wraps all the destructive operations
41# if a curl|bash cuts off the end of the script due to
42# network, either nothing will happen or will syntax error
43# out preventing half-done work
44execute() {
45 tmpdir=$(mktmpdir)
46 log_debug "downloading files into ${tmpdir}"
47 http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
48 http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}"
49 hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
50 srcdir="${tmpdir}/${NAME}"
51 rm -rf "${srcdir}"
52 (cd "${tmpdir}" && untar "${TARBALL}")
53 install -d "${BINDIR}"
54 for binexe in "golangci-lint" ; do
55 if [ "$OS" = "windows" ]; then
56 binexe="${binexe}.exe"
57 fi
58 install "${srcdir}/${binexe}" "${BINDIR}/"
59 log_info "installed ${BINDIR}/${binexe}"
60 done
61}
62is_supported_platform() {
63 platform=$1
64 found=1
65 case "$platform" in
66 darwin/amd64) found=0 ;;
67 darwin/386) found=0 ;;
68 windows/amd64) found=0 ;;
69 windows/386) found=0 ;;
70 linux/amd64) found=0 ;;
71 linux/386) found=0 ;;
72 esac
73 return $found
74}
75check_platform() {
76 if is_supported_platform "$PLATFORM"; then
77 # optional logging goes here
78 true
79 else
80 log_crit "platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
81 exit 1
82 fi
83}
84tag_to_version() {
85 if [ -z "${TAG}" ]; then
86 log_info "checking GitHub for latest tag"
87 else
88 log_info "checking GitHub for tag '${TAG}'"
89 fi
90 REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
91 if test -z "$REALTAG"; then
92 log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
93 exit 1
94 fi
95 # if version starts with 'v', remove it
96 TAG="$REALTAG"
97 VERSION=${TAG#v}
98}
99adjust_format() {
100 # change format (tar.gz or zip) based on ARCH
101 case ${ARCH} in
102 windows) FORMAT=zip ;;
103 esac
104 true
105}
106adjust_os() {
107 # adjust archive name based on OS
108 true
109}
110adjust_arch() {
111 # adjust archive name based on ARCH
112 true
113}
114
115cat /dev/null <<EOF
116------------------------------------------------------------------------
117https://github.com/client9/shlib - portable posix shell functions
118Public domain - http://unlicense.org
119https://github.com/client9/shlib/blob/master/LICENSE.md
120but credit (and pull requests) appreciated.
121------------------------------------------------------------------------
122EOF
123is_command() {
124 command -v "$1" >/dev/null
125}
126echoerr() {
127 echo "$@" 1>&2
128}
129log_prefix() {
130 echo "$0"
131}
132_logp=6
133log_set_priority() {
134 _logp="$1"
135}
136log_priority() {
137 if test -z "$1"; then
138 echo "$_logp"
139 return
140 fi
141 [ "$1" -le "$_logp" ]
142}
143log_tag() {
144 case $1 in
145 0) echo "emerg" ;;
146 1) echo "alert" ;;
147 2) echo "crit" ;;
148 3) echo "err" ;;
149 4) echo "warning" ;;
150 5) echo "notice" ;;
151 6) echo "info" ;;
152 7) echo "debug" ;;
153 *) echo "$1" ;;
154 esac
155}
156log_debug() {
157 log_priority 7 || return 0
158 echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
159}
160log_info() {
161 log_priority 6 || return 0
162 echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
163}
164log_err() {
165 log_priority 3 || return 0
166 echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
167}
168log_crit() {
169 log_priority 2 || return 0
170 echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
171}
172uname_os() {
173 os=$(uname -s | tr '[:upper:]' '[:lower:]')
174 case "$os" in
175 msys_nt) os="windows" ;;
176 esac
177 echo "$os"
178}
179uname_arch() {
180 arch=$(uname -m)
181 case $arch in
182 x86_64) arch="amd64" ;;
183 x86) arch="386" ;;
184 i686) arch="386" ;;
185 i386) arch="386" ;;
186 aarch64) arch="arm64" ;;
187 armv5*) arch="armv5" ;;
188 armv6*) arch="armv6" ;;
189 armv7*) arch="armv7" ;;
190 esac
191 echo ${arch}
192}
193uname_os_check() {
194 os=$(uname_os)
195 case "$os" in
196 darwin) return 0 ;;
197 dragonfly) return 0 ;;
198 freebsd) return 0 ;;
199 linux) return 0 ;;
200 android) return 0 ;;
201 nacl) return 0 ;;
202 netbsd) return 0 ;;
203 openbsd) return 0 ;;
204 plan9) return 0 ;;
205 solaris) return 0 ;;
206 windows) return 0 ;;
207 esac
208 log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
209 return 1
210}
211uname_arch_check() {
212 arch=$(uname_arch)
213 case "$arch" in
214 386) return 0 ;;
215 amd64) return 0 ;;
216 arm64) return 0 ;;
217 armv5) return 0 ;;
218 armv6) return 0 ;;
219 armv7) return 0 ;;
220 ppc64) return 0 ;;
221 ppc64le) return 0 ;;
222 mips) return 0 ;;
223 mipsle) return 0 ;;
224 mips64) return 0 ;;
225 mips64le) return 0 ;;
226 s390x) return 0 ;;
227 amd64p32) return 0 ;;
228 esac
229 log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib"
230 return 1
231}
232untar() {
233 tarball=$1
234 case "${tarball}" in
235 *.tar.gz | *.tgz) tar -xzf "${tarball}" ;;
236 *.tar) tar -xf "${tarball}" ;;
237 *.zip) unzip "${tarball}" ;;
238 *)
239 log_err "untar unknown archive format for ${tarball}"
240 return 1
241 ;;
242 esac
243}
244mktmpdir() {
245 test -z "$TMPDIR" && TMPDIR="$(mktemp -d)"
246 mkdir -p "${TMPDIR}"
247 echo "${TMPDIR}"
248}
249http_download_curl() {
250 local_file=$1
251 source_url=$2
252 header=$3
253 if [ -z "$header" ]; then
254 code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
255 else
256 code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
257 fi
258 if [ "$code" != "200" ]; then
259 log_debug "http_download_curl received HTTP status $code"
260 return 1
261 fi
262 return 0
263}
264http_download_wget() {
265 local_file=$1
266 source_url=$2
267 header=$3
268 if [ -z "$header" ]; then
269 wget -q -O "$local_file" "$source_url"
270 else
271 wget -q --header "$header" -O "$local_file" "$source_url"
272 fi
273}
274http_download() {
275 log_debug "http_download $2"
276 if is_command curl; then
277 http_download_curl "$@"
278 return
279 elif is_command wget; then
280 http_download_wget "$@"
281 return
282 fi
283 log_crit "http_download unable to find wget or curl"
284 return 1
285}
286http_copy() {
287 tmp=$(mktemp)
288 http_download "${tmp}" "$1" "$2" || return 1
289 body=$(cat "$tmp")
290 rm -f "${tmp}"
291 echo "$body"
292}
293github_release() {
294 owner_repo=$1
295 version=$2
296 test -z "$version" && version="latest"
297 giturl="https://github.com/${owner_repo}/releases/${version}"
298 json=$(http_copy "$giturl" "Accept:application/json")
299 test -z "$json" && return 1
300 version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
301 test -z "$version" && return 1
302 echo "$version"
303}
304hash_sha256() {
305 TARGET=${1:-/dev/stdin}
306 if is_command gsha256sum; then
307 hash=$(gsha256sum "$TARGET") || return 1
308 echo "$hash" | cut -d ' ' -f 1
309 elif is_command sha256sum; then
310 hash=$(sha256sum "$TARGET") || return 1
311 echo "$hash" | cut -d ' ' -f 1
312 elif is_command shasum; then
313 hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
314 echo "$hash" | cut -d ' ' -f 1
315 elif is_command openssl; then
316 hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
317 echo "$hash" | cut -d ' ' -f a
318 else
319 log_crit "hash_sha256 unable to find command to compute sha-256 hash"
320 return 1
321 fi
322}
323hash_sha256_verify() {
324 TARGET=$1
325 checksums=$2
326 if [ -z "$checksums" ]; then
327 log_err "hash_sha256_verify checksum file not specified in arg2"
328 return 1
329 fi
330 BASENAME=${TARGET##*/}
331 want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
332 if [ -z "$want" ]; then
333 log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
334 return 1
335 fi
336 got=$(hash_sha256 "$TARGET")
337 if [ "$want" != "$got" ]; then
338 log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
339 return 1
340 fi
341}
342cat /dev/null <<EOF
343------------------------------------------------------------------------
344End of functions from https://github.com/client9/shlib
345------------------------------------------------------------------------
346EOF
347
348PROJECT_NAME="golangci-lint"
349OWNER=golangci
350REPO="golangci-lint"
351BINARY=golangci-lint
352FORMAT=tar.gz
353OS=$(uname_os)
354ARCH=$(uname_arch)
355PREFIX="$OWNER/$REPO"
356
357# use in logging routines
358log_prefix() {
359 echo "$PREFIX"
360}
361PLATFORM="${OS}/${ARCH}"
362GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
363
364uname_os_check "$OS"
365uname_arch_check "$ARCH"
366
367parse_args "$@"
368
369check_platform
370
371tag_to_version
372
373adjust_format
374
375adjust_os
376
377adjust_arch
378
379log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
380
381NAME=${BINARY}-${VERSION}-${OS}-${ARCH}
382TARBALL=${NAME}.${FORMAT}
383TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
384CHECKSUM=${PROJECT_NAME}-${VERSION}-checksums.txt
385CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
386
387
388execute
View as plain text