Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
close #43 close #44 close #45 close #48
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Mar 9, 2018
1 parent 399c451 commit 8a05b84
Show file tree
Hide file tree
Showing 12 changed files with 857 additions and 379 deletions.
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"strings"
"text/template"
"time"

"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
Expand All @@ -17,8 +18,13 @@ import (

// given a template, and a config, generate shell script
func makeShell(tplsrc string, cfg *config.Project) (string, error) {
funcMap := template.FuncMap{
"timestamp": func() string {
return time.Now().UTC().Format(time.RFC3339)
},
}
var out bytes.Buffer
t, err := template.New("shell").Parse(tplsrc)
t, err := template.New("shell").Funcs(funcMap).Parse(tplsrc)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions makeshellfn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ cat \
untar.sh \
mktmpdir.sh \
http_download.sh \
github_api.sh \
github_last_release.sh \
github_release.sh \
hash_sha256.sh \
license_end.sh \
| grep -v '^#' | grep -v ' #' | tr -s '\n'
Expand Down
170 changes: 118 additions & 52 deletions samples/godownloader-event-gateway.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#!/bin/sh
set -e
# Code generated by godownloader. DO NOT EDIT.
# Code generated by godownloader on 2018-03-09T08:19:10Z. DO NOT EDIT.
#

usage() {
this=$1
cat <<EOF
$this: download go binaries for serverless/event-gateway
Usage: $this [-b] bindir [tag]
Usage: $this [-b] bindir [-d] [tag]
-b sets bindir or installation directory, Defaults to ./bin
-d turns on debug logging
[tag] is a tag from
https://github.com/serverless/event-gateway/releases
If tag is missing, then an attempt to find the latest will be found.
Consider setting GITHUB_TOKEN to avoid triggering GitHub rate limits.
See the following for more details:
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
If tag is missing, then the latest will be used.
Generated by godownloader
https://github.com/goreleaser/godownloader
Expand All @@ -30,9 +27,10 @@ parse_args() {
# over-ridden by flag below

BINDIR=${BINDIR:-./bin}
while getopts "b:h?" arg; do
while getopts "b:dh?" arg; do
case "$arg" in
b) BINDIR="$OPTARG" ;;
d) log_set_priority 10 ;;
h | \?) usage "$0" ;;
esac
done
Expand All @@ -52,7 +50,7 @@ execute() {
(cd "${TMPDIR}" && untar "${TARBALL}")
install -d "${BINDIR}"
install "${TMPDIR}/${BINARY}" "${BINDIR}/"
echo "$PREFIX: installed as ${BINDIR}/${BINARY}"
log_info "installed as ${BINDIR}/${BINARY}"
}
is_supported_platform() {
platform=$1
Expand All @@ -72,16 +70,23 @@ check_platform() {
# optional logging goes here
true
else
echo "${PREFIX}: platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
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"
exit 1
fi
}
tag_to_version() {
if [ -z "${TAG}" ]; then
echo "$PREFIX: checking GitHub for latest tag"
TAG=$(github_last_release "$OWNER/$REPO")
log_info "checking GitHub for latest tag"
else
log_info "checking GitHub for tag '${TAG}'"
fi
REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
if test -z "$REALTAG"; then
log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
exit 1
fi
# if version starts with 'v', remove it
TAG="$REALTAG"
VERSION=${TAG#v}
}
adjust_format() {
Expand All @@ -108,6 +113,52 @@ EOF
is_command() {
command -v "$1" >/dev/null
}
echoerr() {
echo "$@" 1>&2
}
log_prefix() {
echo "$0"
}
_logp=6
log_set_priority() {
_logp="$1"
}
log_priority() {
if test -z "$1"; then
echo "$_logp"
return
fi
[ "$1" -le "$_logp" ]
}
log_tag() {
case $1 in
0) echo "emerg" ;;
1) echo "alert" ;;
2) echo "crit" ;;
3) echo "err" ;;
4) echo "warning" ;;
5) echo "notice" ;;
6) echo "info" ;;
7) echo "debug" ;;
*) echo "$1" ;;
esac
}
log_debug() {
log_priority 7 || return 0
echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
}
log_info() {
log_priority 6 || return 0
echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
}
log_err() {
log_priority 3 || return 0
echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
}
log_crit() {
log_priority 2 || return 0
echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
}
uname_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
Expand Down Expand Up @@ -144,7 +195,7 @@ uname_os_check() {
solaris) return 0 ;;
windows) return 0 ;;
esac
echo "$0: uname_os_check: internal error '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
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"
return 1
}
uname_arch_check() {
Expand All @@ -165,7 +216,7 @@ uname_arch_check() {
s390x) return 0 ;;
amd64p32) return 0 ;;
esac
echo "$0: uname_arch_check: internal error '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib"
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"
return 1
}
untar() {
Expand All @@ -175,7 +226,7 @@ untar() {
*.tar) tar -xf "${tarball}" ;;
*.zip) unzip "${tarball}" ;;
*)
echo "Unknown archive format for ${tarball}"
log_err "untar unknown archive format for ${tarball}"
return 1
;;
esac
Expand All @@ -185,49 +236,60 @@ mktmpdir() {
mkdir -p "${TMPDIR}"
echo "${TMPDIR}"
}
http_download() {
http_download_curl() {
local_file=$1
source_url=$2
header=$3
headerflag=''
destflag=''
if is_command curl; then
cmd='curl --fail -sSL -N'
destflag='-o'
headerflag='-H'
elif is_command wget; then
cmd='wget -q'
destflag='-O'
headerflag='--header'
if [ -z "$header" ]; then
code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
else
echo "http_download: unable to find wget or curl"
code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
fi
if [ "$code" != "200" ]; then
log_debug "http_download_curl received HTTP status $code"
return 1
fi
echo "$PREFIX: downloading ${source_url}"
return 0
}
http_download_wget() {
local_file=$1
source_url=$2
header=$3
if [ -z "$header" ]; then
$cmd $destflag "$local_file" "$source_url"
wget -q -O "$local_file" "$source_url"
else
$cmd $headerflag "$header" $destflag "$local_file" "$source_url"
wget -q --header "$header" -O "$local_file" "$source_url"
fi
}
github_api() {
local_file=$1
source_url=$2
header=""
case "$source_url" in
https://api.github.com*)
test -z "$GITHUB_TOKEN" || header="Authorization: token $GITHUB_TOKEN"
;;
esac
http_download "$local_file" "$source_url" "$header"
http_download() {
log_debug "http_download $2"
if is_command curl; then
http_download_curl "$@"
return
elif is_command wget; then
http_download_wget "$@"
return
fi
log_crit "http_download unable to find wget or curl"
return 1
}
http_copy() {
tmp=$(mktemp)
http_download "${tmp}" "$1" "$2" || return 1
body=$(cat "$tmp")
rm -f "${tmp}"
echo "$body"
}
github_last_release() {
github_release() {
owner_repo=$1
giturl="https://api.github.com/repos/${owner_repo}/releases/latest"
html=$(github_api - "$giturl")
tag=$(echo "$html" | grep -m 1 "\"tag_name\":" | cut -f4 -d'"')
test -z "$tag" && return 1
echo "$tag"
version=$2
test -z "$version" && version="latest"
giturl="https://github.com/${owner_repo}/releases/${version}"
json=$(http_copy "$giturl" "Accept:application/json")
test -z "$json" && return 1
version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
test -z "$version" && return 1
echo "$version"
}
hash_sha256() {
TARGET=${1:-/dev/stdin}
Expand All @@ -244,27 +306,26 @@ hash_sha256() {
hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
echo "$hash" | cut -d ' ' -f a
else
echo "hash_sha256: unable to find command to compute sha-256 hash"
log_crit "hash_sha256 unable to find command to compute sha-256 hash"
return 1
fi
}
hash_sha256_verify() {
TARGET=$1
checksums=$2
echo "$PREFIX: verifying checksums"
if [ -z "$checksums" ]; then
echo "hash_sha256_verify: checksum file not specified in arg2"
log_err "hash_sha256_verify checksum file not specified in arg2"
return 1
fi
BASENAME=${TARGET##*/}
want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
if [ -z "$want" ]; then
echo "hash_sha256_verify: unable to find checksum for '${TARGET}' in '${checksums}'"
log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
return 1
fi
got=$(hash_sha256 "$TARGET")
if [ "$want" != "$got" ]; then
echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got"
log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
return 1
fi
}
Expand All @@ -281,6 +342,11 @@ FORMAT=zip
OS=$(uname_os)
ARCH=$(uname_arch)
PREFIX="$OWNER/$REPO"

# use in logging routines
log_prefix() {
echo "$PREFIX"
}
PLATFORM="${OS}/${ARCH}"
GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download

Expand All @@ -299,7 +365,7 @@ adjust_os

adjust_arch

echo "$PREFIX: found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"

NAME=${BINARY}_${VERSION}_${OS}_${ARCH}
TARBALL=${NAME}.${FORMAT}
Expand Down
Loading

0 comments on commit 8a05b84

Please sign in to comment.