install-misspell.sh 7.8 KB

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