github.com/skabbes/up@v0.2.1/install.sh (about) 1 #!/bin/sh 2 set -e 3 # Code generated by godownloader. DO NOT EDIT. 4 # 5 6 usage() { 7 this=$1 8 cat <<EOF 9 $this: download go binaries for apex/up 10 11 Usage: $this [-b] bindir [version] 12 -b sets bindir or installation directory, default "/usr/local/bin" 13 [version] is a version number from 14 https://github.com/apex/up/releases 15 If version is missing, then an attempt to find the latest will be found. 16 17 Generated by godownloader 18 https://github.com/goreleaser/godownloader 19 20 EOF 21 exit 2 22 } 23 24 parse_args() { 25 #BINDIR is /usr/local/bin unless set be ENV 26 # over-ridden by flag below 27 28 BINDIR=${BINDIR:-/usr/local/bin} 29 while getopts "b:h?" arg; do 30 case "$arg" in 31 b) BINDIR="$OPTARG" ;; 32 h | \?) usage "$0" ;; 33 esac 34 done 35 shift $((OPTIND - 1)) 36 VERSION=$1 37 } 38 # this function wraps all the destructive operations 39 # if a curl|bash cuts off the end of the script due to 40 # network, either nothing will happen or will syntax error 41 # out preventing half-done work 42 execute() { 43 TMPDIR=$(mktmpdir) 44 echo "$PREFIX: downloading ${TARBALL_URL}" 45 http_download "${TMPDIR}/${TARBALL}" "${TARBALL_URL}" 46 47 echo "$PREFIX: verifying checksums" 48 http_download "${TMPDIR}/${CHECKSUM}" "${CHECKSUM_URL}" 49 hash_sha256_verify "${TMPDIR}/${TARBALL}" "${TMPDIR}/${CHECKSUM}" 50 51 (cd "${TMPDIR}" && untar "${TARBALL}") 52 install -d "${BINDIR}" 53 install "${TMPDIR}/${BINARY}" "${BINDIR}/" 54 echo "$PREFIX: installed as ${BINDIR}/${BINARY}" 55 } 56 is_supported_platform() { 57 platform=$1 58 found=1 59 case "$platform" in 60 darwin/amd64) found=0 ;; 61 darwin/386) found=0 ;; 62 linux/amd64) found=0 ;; 63 linux/386) found=0 ;; 64 windows/amd64) found=0 ;; 65 windows/386) found=0 ;; 66 freebsd/amd64) found=0 ;; 67 freebsd/386) found=0 ;; 68 netbsd/amd64) found=0 ;; 69 netbsd/386) found=0 ;; 70 openbsd/amd64) found=0 ;; 71 openbsd/386) found=0 ;; 72 esac 73 return $found 74 } 75 check_platform() { 76 if is_supported_platform "$PLATFORM"; then 77 # optional logging goes here 78 true 79 else 80 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" 81 exit 1 82 fi 83 } 84 adjust_version() { 85 if [ -z "${VERSION}" ]; then 86 echo "$PREFIX: checking GitHub for latest version" 87 VERSION=$(github_last_release "$OWNER/$REPO") 88 fi 89 # if version starts with 'v', remove it 90 VERSION=${VERSION#v} 91 } 92 adjust_format() { 93 # change format (tar.gz or zip) based on ARCH 94 true 95 } 96 adjust_os() { 97 # adjust archive name based on OS 98 true 99 } 100 adjust_arch() { 101 # adjust archive name based on ARCH 102 true 103 } 104 105 cat /dev/null <<EOF 106 ------------------------------------------------------------------------ 107 https://github.com/client9/shlib - portable posix shell functions 108 Public domain - http://unlicense.org 109 https://github.com/client9/shlib/blob/master/LICENSE.md 110 but credit (and pull requests) appreciated. 111 ------------------------------------------------------------------------ 112 EOF 113 is_command() { 114 command -v "$1" >/dev/null 115 } 116 uname_os() { 117 os=$(uname -s | tr '[:upper:]' '[:lower:]') 118 echo "$os" 119 } 120 uname_arch() { 121 arch=$(uname -m) 122 case $arch in 123 x86_64) arch="amd64" ;; 124 x86) arch="386" ;; 125 i686) arch="386" ;; 126 i386) arch="386" ;; 127 aarch64) arch="arm64" ;; 128 armv5*) arch="arm5" ;; 129 armv6*) arch="arm6" ;; 130 armv7*) arch="arm7" ;; 131 esac 132 echo ${arch} 133 } 134 uname_os_check() { 135 os=$(uname_os) 136 case "$os" in 137 darwin) return 0 ;; 138 dragonfly) return 0 ;; 139 freebsd) return 0 ;; 140 linux) return 0 ;; 141 android) return 0 ;; 142 nacl) return 0 ;; 143 netbsd) return 0 ;; 144 openbsd) return 0 ;; 145 plan9) return 0 ;; 146 solaris) return 0 ;; 147 windows) return 0 ;; 148 esac 149 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" 150 return 1 151 } 152 uname_arch_check() { 153 arch=$(uname_arch) 154 case "$arch" in 155 386) return 0 ;; 156 amd64) return 0 ;; 157 arm64) return 0 ;; 158 armv5) return 0 ;; 159 armv6) return 0 ;; 160 armv7) return 0 ;; 161 ppc64) return 0 ;; 162 ppc64le) return 0 ;; 163 mips) return 0 ;; 164 mipsle) return 0 ;; 165 mips64) return 0 ;; 166 mips64le) return 0 ;; 167 s390x) return 0 ;; 168 amd64p32) return 0 ;; 169 esac 170 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" 171 return 1 172 } 173 untar() { 174 tarball=$1 175 case "${tarball}" in 176 *.tar.gz | *.tgz) tar -xzf "${tarball}" ;; 177 *.tar) tar -xf "${tarball}" ;; 178 *.zip) unzip "${tarball}" ;; 179 *) 180 echo "Unknown archive format for ${tarball}" 181 return 1 182 ;; 183 esac 184 } 185 mktmpdir() { 186 test -z "$TMPDIR" && TMPDIR="$(mktemp -d)" 187 mkdir -p "${TMPDIR}" 188 echo "${TMPDIR}" 189 } 190 http_download() { 191 local_file=$1 192 source_url=$2 193 header=$3 194 headerflag='' 195 destflag='' 196 if is_command curl; then 197 cmd='curl --fail -sSL' 198 destflag='-o' 199 headerflag='-H' 200 elif is_command wget; then 201 cmd='wget -q' 202 destflag='-O' 203 headerflag='--header' 204 else 205 echo "http_download: unable to find wget or curl" 206 return 1 207 fi 208 if [ -z "$header" ]; then 209 $cmd $destflag "$local_file" "$source_url" 210 else 211 $cmd $headerflag "$header" $destflag "$local_file" "$source_url" 212 fi 213 } 214 github_api() { 215 local_file=$1 216 source_url=$2 217 header="" 218 case "$source_url" in 219 https://api.github.com*) 220 test -z "$GITHUB_TOKEN" || header="Authorization: token $GITHUB_TOKEN" 221 ;; 222 esac 223 http_download "$local_file" "$source_url" "$header" 224 } 225 github_last_release() { 226 owner_repo=$1 227 giturl="https://api.github.com/repos/${owner_repo}/releases/latest" 228 html=$(github_api - "$giturl") 229 version=$(echo "$html" | grep -m 1 "\"tag_name\":" | cut -f4 -d'"') 230 test -z "$version" && return 1 231 echo "$version" 232 } 233 hash_sha256() { 234 TARGET=${1:-/dev/stdin} 235 if is_command gsha256sum; then 236 hash=$(gsha256sum "$TARGET") || return 1 237 echo "$hash" | cut -d ' ' -f 1 238 elif is_command sha256sum; then 239 hash=$(sha256sum "$TARGET") || return 1 240 echo "$hash" | cut -d ' ' -f 1 241 elif is_command shasum; then 242 hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1 243 echo "$hash" | cut -d ' ' -f 1 244 elif is_command openssl; then 245 hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1 246 echo "$hash" | cut -d ' ' -f a 247 else 248 echo "hash_sha256: unable to find command to compute sha-256 hash" 249 return 1 250 fi 251 } 252 hash_sha256_verify() { 253 TARGET=$1 254 checksums=$2 255 if [ -z "$checksums" ]; then 256 echo "hash_sha256_verify: checksum file not specified in arg2" 257 return 1 258 fi 259 BASENAME=${TARGET##*/} 260 want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1) 261 if [ -z "$want" ]; then 262 echo "hash_sha256_verify: unable to find checksum for '${TARGET}' in '${checksums}'" 263 return 1 264 fi 265 got=$(hash_sha256 "$TARGET") 266 if [ "$want" != "$got" ]; then 267 echo "hash_sha256_verify: checksum for '$TARGET' did not verify ${want} vs $got" 268 return 1 269 fi 270 } 271 cat /dev/null <<EOF 272 ------------------------------------------------------------------------ 273 End of functions from https://github.com/client9/shlib 274 ------------------------------------------------------------------------ 275 EOF 276 277 OWNER=apex 278 REPO=up 279 BINARY=up 280 FORMAT=tar.gz 281 OS=$(uname_os) 282 ARCH=$(uname_arch) 283 PREFIX="$OWNER/$REPO" 284 PLATFORM="${OS}/${ARCH}" 285 GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download 286 287 uname_os_check "$OS" 288 uname_arch_check "$ARCH" 289 290 parse_args "$@" 291 292 check_platform 293 294 adjust_version 295 296 adjust_format 297 298 adjust_os 299 300 adjust_arch 301 302 echo "$PREFIX: found version ${VERSION} for ${OS}/${ARCH}" 303 304 NAME=${BINARY}_${VERSION}_${OS}_${ARCH} 305 TARBALL=${NAME}.${FORMAT} 306 TARBALL_URL=${GITHUB_DOWNLOAD}/v${VERSION}/${TARBALL} 307 CHECKSUM=${BINARY}_checksums.txt 308 CHECKSUM_URL=${GITHUB_DOWNLOAD}/v${VERSION}/${CHECKSUM} 309 310 # Adjust binary name if windows 311 if [ "$OS" = "windows" ]; then 312 BINARY="${BINARY}.exe" 313 fi 314 315 execute