|
1 |
| -## 999_footer.sh |
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +## |
| 4 | +## Borrowed from rustup (https://sh.rustup.rs) |
| 5 | +## |
| 6 | +## This is just a little script that can be downloaded from the internet to |
| 7 | +## install dfx. It just does platform detection, downloads the installer |
| 8 | +## and runs it. |
| 9 | +## |
| 10 | +## You are NOT AUTHORIZED to remove any license agreements or prompts from the following script. |
| 11 | +## |
| 12 | +set -u |
| 13 | + |
| 14 | +# Functions useful for dealing with the manifest (which is JSON). |
| 15 | + |
| 16 | +# Get the version of a tag from the manifest JSON file. |
| 17 | +# Arguments: |
| 18 | +# $1 - The tag to get. |
| 19 | +# STDIN - The manifest file. |
| 20 | +# Returns: |
| 21 | +# 0 if the tag was found, 1 if it wasn't. |
| 22 | +# Prints out the version number. |
| 23 | +get_tag_from_manifest_json() { |
| 24 | + # Find the tag in the file. Then get the last digits. |
| 25 | + # The first grep returns `"tag_name": "1.2.3` (without the last quote). |
| 26 | + cat \ |
| 27 | + | tr -d '\n' \ |
| 28 | + | grep -o "\"$1\":[[:space:]]*\"[a-zA-Z0-9.]*" \ |
| 29 | + | grep -o "[0-9.]*$" |
| 30 | +} |
| 31 | + |
| 32 | +# A newline separated list of boolean flags. See the read_flags function to see how it's parsed. |
| 33 | +DFX_BOOL_FLAGS="" |
| 34 | + |
| 35 | +# Make a BOOLEAN flag and its description. |
| 36 | +# |
| 37 | +# Arguments: |
| 38 | +# $1 - The long name of the boolean. This will be used on the command line. The name of the |
| 39 | +# environment variable will be `flag_NAME` where NAME is this argument, capitalized. |
| 40 | +# The value of this argument is empty string if not specified, and "1" if it is. |
| 41 | +# $2 - A description of the flag. This is not currently used but will be when we have enough |
| 42 | +# flags to implement help. |
| 43 | +define_flag_BOOL() { |
| 44 | + local VARNAME |
| 45 | + VARNAME="flag_$(echo "$1" | tr /a-z/ /A-Z)" |
| 46 | + eval "$VARNAME=\${$VARNAME:-}" |
| 47 | + DFX_BOOL_FLAGS="${DFX_BOOL_FLAGS}--${1} $VARNAME $2" |
| 48 | +} |
| 49 | + |
| 50 | +# Get the flag name of a line in the flag description. |
| 51 | +get_flag_name() { |
| 52 | + echo "$1" |
| 53 | +} |
| 54 | + |
| 55 | +# Get the variable name of a line in the flag description. |
| 56 | +get_var_name() { |
| 57 | + echo "$2" |
| 58 | +} |
| 59 | + |
| 60 | +# Read all the command line flags and set the flag_XXXX environment variables. |
| 61 | +# |
| 62 | +# Arguments: |
| 63 | +# $* - Flags to parse. |
| 64 | +# Side Effects: |
| 65 | +# Environment variables are set according to flags defined and flags. |
| 66 | +read_flags() { |
| 67 | + # Set values from command line. |
| 68 | + # shellcheck disable=SC2199 |
| 69 | + # https://github.com/koalaman/shellcheck/wiki/SC2199 |
| 70 | + while [ -n "$*" ]; do |
| 71 | + local ARG="$1" |
| 72 | + shift |
| 73 | + |
| 74 | + OLD_IFS="$IFS" |
| 75 | + IFS=$'\n' |
| 76 | + for line in ${DFX_BOOL_FLAGS}; do |
| 77 | + [ "$line" ] || break |
| 78 | + |
| 79 | + IFS="$OLD_IFS" |
| 80 | + FLAG="$(get_flag_name "$line")" |
| 81 | + VARNAME="$(get_var_name "$line")" |
| 82 | + |
| 83 | + if [ "$ARG" == "$FLAG" ]; then |
| 84 | + eval "$VARNAME=1" |
| 85 | + fi |
| 86 | + done |
| 87 | + done |
| 88 | +} |
| 89 | + |
| 90 | +log() { |
| 91 | + if "$_ansi_escapes_are_valid"; then |
| 92 | + printf "\33[1minfo:\33[0m %s\n" "$1" 1>&2 |
| 93 | + else |
| 94 | + printf '%s\n' "$1" 1>&2 |
| 95 | + fi |
| 96 | +} |
| 97 | + |
| 98 | +say() { |
| 99 | + printf 'dfinity-sdk: %s\n' "$1" |
| 100 | +} |
| 101 | + |
| 102 | +warn() { |
| 103 | + if $_ansi_escapes_are_valid; then |
| 104 | + printf "\33[1mwarn:\33[0m %s\n" "$1" 1>&2 |
| 105 | + else |
| 106 | + printf '%s\n' "$1" 1>&2 |
| 107 | + fi |
| 108 | +} |
| 109 | + |
| 110 | +err() { |
| 111 | + say "$1" >&2 |
| 112 | + exit 1 |
| 113 | +} |
| 114 | + |
| 115 | +need_cmd() { |
| 116 | + if ! check_cmd "$1"; then |
| 117 | + err "need '$1' (command not found)" |
| 118 | + fi |
| 119 | +} |
| 120 | + |
| 121 | +check_cmd() { |
| 122 | + command -v "$1" >/dev/null 2>&1 |
| 123 | +} |
| 124 | + |
| 125 | +assert_nz() { |
| 126 | + if [ -z "$1" ]; then err "assert_nz $2"; fi |
| 127 | +} |
| 128 | + |
| 129 | +# Run a command that should never fail. If the command fails execution |
| 130 | +# will immediately terminate with an error showing the failing |
| 131 | +# command. |
| 132 | +ensure() { |
| 133 | + if ! "$@"; then err "command failed: $*"; fi |
| 134 | +} |
| 135 | + |
| 136 | +# This is just for indicating that commands' results are being |
| 137 | +# intentionally ignored. Usually, because it's being executed |
| 138 | +# as part of error handling. |
| 139 | +ignore() { |
| 140 | + "$@" |
| 141 | +} |
| 142 | + |
| 143 | +define_flag_BOOL "insecure" "Allows downloading from insecure URLs, either using HTTP or TLS 1.2 or less." |
| 144 | + |
| 145 | +check_help_for() { |
| 146 | + local _cmd |
| 147 | + local _arg |
| 148 | + local _ok |
| 149 | + _cmd="$1" |
| 150 | + _ok="y" |
| 151 | + shift |
| 152 | + |
| 153 | + # If we're running on OS-X, older than 10.13, then we always |
| 154 | + # fail to find these options to force fallback |
| 155 | + if check_cmd sw_vers; then |
| 156 | + case "$(sw_vers -productVersion)" in |
| 157 | + 10.15*) ;; # Catalina |
| 158 | + 11.*) ;; # Big Sur |
| 159 | + 12.*) ;; # Monterey |
| 160 | + 13.*) ;; # Ventura |
| 161 | + *) |
| 162 | + warn "Detected OS X platform older than 10.15 (Catalina)" |
| 163 | + _ok="n" |
| 164 | + ;; |
| 165 | + esac |
| 166 | + fi |
| 167 | + |
| 168 | + for _arg in "$@"; do |
| 169 | + if ! "$_cmd" --help all | grep -q -- "$_arg"; then |
| 170 | + _ok="n" |
| 171 | + fi |
| 172 | + done |
| 173 | + |
| 174 | + test "$_ok" = "y" |
| 175 | +} |
| 176 | + |
| 177 | +# Check for an error message in the output of a command. |
| 178 | +# Arguments: |
| 179 | +# $1 - The error message to look for. |
| 180 | +# $2... - The command and arguments to run. |
| 181 | +# Returns: |
| 182 | +# Whether false if the error message was not found, or true if it wasn't (so the feature is |
| 183 | +# supported. |
| 184 | +# TODO: move this logic to execute once during install.sh run. |
| 185 | +check_support_for() { |
| 186 | + local err="$1" |
| 187 | + shift |
| 188 | + local cmd="$*" |
| 189 | + |
| 190 | + # Run the command, grep for the error message, if it is found returns false, if it |
| 191 | + # is not found, returns true. |
| 192 | + ! ($cmd 2>&1 | grep "$err" >/dev/null) |
| 193 | +} |
| 194 | + |
| 195 | +# This wraps curl or wget. Try curl first, if not installed, use wget instead. |
| 196 | +# Arguments: |
| 197 | +# $1 - URL to download. |
| 198 | +# $2 - Path to output the download. Use - to output to stdout. |
| 199 | +downloader() { |
| 200 | + local _dld |
| 201 | + if check_cmd curl; then |
| 202 | + _dld=curl |
| 203 | + elif check_cmd wget; then |
| 204 | + _dld=wget |
| 205 | + else |
| 206 | + _dld='curl or wget' # to be used in error message of need_cmd |
| 207 | + fi |
| 208 | + |
| 209 | + if [ "$1" = --check ]; then |
| 210 | + need_cmd "$_dld" |
| 211 | + elif [ "$_dld" = curl ]; then |
| 212 | + if check_help_for curl --proto --tlsv1.2; then |
| 213 | + curl --proto '=https' --tlsv1.2 --show-error --fail --connect-timeout 10 --retry 5 --location "$1" --output "$2" |
| 214 | + elif ! [ "$flag_INSECURE" ]; then |
| 215 | + warn "Not forcing TLS v1.2, this is potentially less secure" |
| 216 | + curl --show-error --fail --connect-timeout 10 --retry 5 --location "$1" --output "$2" |
| 217 | + else |
| 218 | + err "TLS 1.2 is not supported on this platform. To force using it, use the --insecure flag." |
| 219 | + fi |
| 220 | + elif [ "$_dld" = wget ]; then |
| 221 | + if check_help_for wget --https-only --secure-protocol; then |
| 222 | + wget --https-only --secure-protocol=TLSv1_2 --timeout 10 --tries 5 --waitretry 5 "$1" -O "$2" |
| 223 | + elif ! [ "$flag_INSECURE" ]; then |
| 224 | + warn "Not forcing TLS v1.2, this is potentially less secure" |
| 225 | + wget --timeout 10 --tries 5 --waitretry 5 "$1" -O "$2" |
| 226 | + else |
| 227 | + err "TLS 1.2 is not supported on this platform. To force using it, use the --insecure flag." |
| 228 | + fi |
| 229 | + else |
| 230 | + err "Unknown downloader" # should not reach here |
| 231 | + fi |
| 232 | +} |
2 | 233 |
|
3 | 234 | # If DFX_RELEASE_ROOT is unset or empty, default it.
|
4 | 235 | SDK_WEBSITE="https://sdk.dfinity.org"
|
|
0 commit comments