You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

install.sh 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env sh
  2. set -eu
  3. if [ -n "${GITHUB_ACTIONS-}" ]; then
  4. set -x
  5. fi
  6. # Check pipefail support in a subshell, ignore if unsupported
  7. # shellcheck disable=SC3040
  8. (set -o pipefail 2> /dev/null) && set -o pipefail
  9. help() {
  10. cat <<'EOF'
  11. Install a binary released on GitHub
  12. USAGE:
  13. install.sh [options]
  14. FLAGS:
  15. -h, --help Display this message
  16. -f, --force Force overwriting an existing binary
  17. OPTIONS:
  18. --repo REPO Github Repository to install the binary from [default: dora-rs]
  19. --bin BIN Name of the binary to install [default: dora]
  20. --tag TAG Tag (version) of the bin to install, defaults to latest release
  21. --to LOCATION Where to install the binary [default: ~/.dora/bin]
  22. --target TARGET
  23. EOF
  24. }
  25. say() {
  26. echo "install: $*" >&2
  27. }
  28. err() {
  29. if [ -n "${td-}" ]; then
  30. rm -rf "$td"
  31. fi
  32. say "error: $*"
  33. exit 1
  34. }
  35. need() {
  36. if ! command -v "$1" > /dev/null 2>&1; then
  37. err "need $1 (command not found)"
  38. fi
  39. }
  40. download() {
  41. url="$1"
  42. output="$2"
  43. if command -v curl > /dev/null; then
  44. curl --proto =https --tlsv1.2 -sSfL "$url" "-o$output"
  45. else
  46. wget --https-only --secure-protocol=TLSv1_2 --quiet "$url" "-O$output"
  47. fi
  48. }
  49. force=false
  50. while test $# -gt 0; do
  51. case $1 in
  52. --help | -h)
  53. help
  54. exit 0
  55. ;;
  56. --repo)
  57. repo=$2
  58. shift
  59. ;;
  60. --bin)
  61. bin=$2
  62. shift
  63. ;;
  64. --tag)
  65. tag=$2
  66. shift
  67. ;;
  68. --target)
  69. target=$2
  70. shift
  71. ;;
  72. --to)
  73. dest=$2
  74. shift
  75. ;;
  76. *)
  77. say "error: unrecognized argument '$1'. Usage:"
  78. help
  79. exit 1
  80. ;;
  81. esac
  82. shift
  83. done
  84. if [ -z "${repo-}" ]; then
  85. repo="dora-rs"
  86. fi
  87. if [ -z "${bin-}" ]; then
  88. bin="dora"
  89. fi
  90. url=https://github.com/$repo/$bin
  91. releases=$url/releases
  92. command -v curl > /dev/null 2>&1 ||
  93. command -v wget > /dev/null 2>&1 ||
  94. err "need wget or curl (command not found)"
  95. need mkdir
  96. need mktemp
  97. if [ -z "${tag-}" ]; then
  98. need grep
  99. need cut
  100. fi
  101. if [ -z "${target-}" ]; then
  102. need cut
  103. fi
  104. if [ -z "${dest-}" ]; then
  105. dest="$HOME/.dora/bin"
  106. fi
  107. if [ -z "${tag-}" ]; then
  108. tag=$(
  109. download https://api.github.com/repos/$repo/$bin/releases/latest - |
  110. grep tag_name |
  111. cut -d'"' -f4
  112. )
  113. fi
  114. if [ -z "${target-}" ]; then
  115. # bash compiled with MINGW (e.g. git-bash, used in github windows runners),
  116. # unhelpfully includes a version suffix in `uname -s` output, so handle that.
  117. # e.g. MINGW64_NT-10-0.19044
  118. kernel=$(uname -s | cut -d- -f1)
  119. uname_target="$(uname -m)-$kernel"
  120. case $uname_target in
  121. aarch64-Linux) target=aarch64-unknown-linux-musl;;
  122. arm64-Darwin) target=aarch64-apple-darwin;;
  123. armv7l-Linux) target=armv7-unknown-linux-musleabihf;;
  124. x86_64-Darwin) target=x86_64-apple-darwin;;
  125. x86_64-Linux) target=x86_64-unknown-linux-gnu;;
  126. *)
  127. # shellcheck disable=SC2016
  128. err 'Could not determine target from output of `uname -m`-`uname -s`, please use `--target`:' "$uname_target"
  129. ;;
  130. esac
  131. fi
  132. case $target in
  133. *) extension=zip; need unzip;;
  134. esac
  135. archive="$releases/download/$tag/$bin-$tag-$target.$extension"
  136. say "Repository: $url"
  137. say "Bin: $bin"
  138. say "Tag: $tag"
  139. say "Target: $target"
  140. say "Destination: $dest"
  141. say "Archive: $archive"
  142. td=$(mktemp -d || mktemp -d -t tmp)
  143. if [ "$extension" = "zip" ]; then
  144. download "$archive" "$td/$bin.zip"
  145. unzip -d "$td" "$td/$bin.zip"
  146. else
  147. download "$archive" - | tar -C "$td" -xz
  148. fi
  149. echo "Placing dora-rs cli in $dest"
  150. if [ -e "$dest/$bin" ] && [ "$force" = false ]; then
  151. echo " Replacing \`$dest/$bin\` with downloaded version"
  152. cp "$td/$bin" "$dest/$bin"
  153. chmod 755 "$dest/$bin"
  154. else
  155. mkdir -p "$dest"
  156. cp "$td/$bin" "$dest/$bin"
  157. chmod 755 "$dest/$bin"
  158. echo ""
  159. fi
  160. if [ "$SHELL" = "/bin/bash" ]; then
  161. if ! grep -q "$dest" ~/.bashrc; then
  162. echo "Adding $dest to PATH in ~/.bashrc"
  163. echo "export PATH=\$PATH:$dest" >> ~/.bashrc
  164. echo "Path added to ~/.bashrc."
  165. echo "Please reload with:"
  166. echo " source ~/.bashrc"
  167. else
  168. echo "$dest is already in the PATH in ~/.bashrc"
  169. fi
  170. elif [ "$SHELL" = "/bin/zsh" ]; then
  171. if ! grep -q "$dest" ~/.zshrc; then
  172. echo "Adding $dest to PATH in ~/.zshrc"
  173. echo "export PATH=\$PATH:$dest" >> ~/.zshrc
  174. echo "Path added to ~/.zshrc."
  175. echo "Please reload with:"
  176. echo " source ~/.zshrc"
  177. else
  178. echo "$dest is already in the PATH in ~/.zshrc"
  179. fi
  180. else
  181. echo "Unsupported shell: $SHELL"
  182. echo "Please add the following to your shell's configuration file manually:"
  183. echo " export PATH=\$PATH:$dest"
  184. fi
  185. rm -rf "$td"