Skip to content

Commit 8c09a83

Browse files
Add install.sh script
1 parent d032909 commit 8c09a83

File tree

2 files changed

+389
-0
lines changed

2 files changed

+389
-0
lines changed

.goreleaser.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
---
2+
project_name: gosec
3+
4+
release:
5+
github:
6+
owner: securego
7+
name: gosec
8+
19
builds:
210
- main : ./cmd/gosec/
311
binary: gosec

install.sh

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

0 commit comments

Comments
 (0)