|
1 | 1 | #!/bin/bash
|
2 |
| -{ |
3 |
| - set -e |
4 |
| - SUDO='' |
5 |
| - if [ "$(id -u)" != "0" ]; then |
6 |
| - SUDO='sudo' |
7 |
| - echo "This script requires superuser access." |
8 |
| - echo "You will be prompted for your password by sudo." |
9 |
| - # clear any previous sudo permission |
10 |
| - sudo -k |
11 |
| - fi |
12 |
| - |
13 |
| - # run inside sudo |
14 |
| - $SUDO bash << SCRIPT |
15 |
| - set -e |
16 |
| -
|
17 |
| - OS="" |
18 |
| - ARCH="" |
19 |
| - OS_ARCH="" |
20 |
| -
|
21 |
| - echoerr() { echo "\$@" 1>&2; } |
22 |
| -
|
23 |
| - unsupported_arch() { |
24 |
| - local os=$1 |
25 |
| - local arch=$2 |
26 |
| - echoerr "The Graph CLI does not support $os / $arch at this time." |
27 |
| - exit 1 |
28 |
| - } |
29 | 2 |
|
30 |
| - set_os_arch() { |
31 |
| - if [ "\$(uname)" == "Darwin" ]; then |
32 |
| - OS=darwin |
33 |
| - elif [ "\$(expr substr \$(uname -s) 1 5)" == "Linux" ]; then |
34 |
| - OS=linux |
35 |
| - else |
36 |
| - OS=win32 |
37 |
| - fi |
| 3 | +if [ -n "$1" ] |
| 4 | +then |
| 5 | + INSTALL_DIR=${1%%/} |
| 6 | + BIN_DIR= |
| 7 | +else |
| 8 | + INSTALL_DIR=/usr/local/lib |
| 9 | + BIN_DIR=/usr/local/bin |
| 10 | +fi |
| 11 | + |
| 12 | +if [ ! -w "$INSTALL_DIR" ] |
| 13 | +then |
| 14 | + cat <<EOF |
| 15 | +usage: install.sh [DIR] |
| 16 | +
|
| 17 | +Install graph-cli to DIR. DIR defaults to /usr/local/lib. |
| 18 | +
|
| 19 | +The directory $INSTALL_DIR is not writable by the current user. |
| 20 | +
|
| 21 | +Try running this script as root for a system-wide install which will also |
| 22 | +put 'graph' into /usr/local/bin or pass an installation directory that is |
| 23 | +writable by the current user as DIR |
| 24 | +EOF |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +set -e |
| 29 | + |
| 30 | +OS="" |
| 31 | +ARCH="" |
| 32 | + |
| 33 | +echoerr() { echo "$@" 1>&2; } |
38 | 34 |
|
39 |
| - ARCH="\$(uname -m)" |
40 |
| - if [ "\$ARCH" == "x86_64" ]; then |
41 |
| - ARCH=x64 |
42 |
| - elif [ "\$ARCH" == "amd64" ]; then |
43 |
| - ARCH=x64 |
44 |
| - elif [ "\$ARCH" == "arm64" ]; then |
45 |
| - if [ "\$OS" == "darwin" ]; then |
| 35 | +unsupported_arch() { |
| 36 | + local os=$1 |
| 37 | + local arch=$2 |
| 38 | + echoerr "The Graph CLI does not support $os / $arch at this time." |
| 39 | + exit 1 |
| 40 | +} |
| 41 | + |
| 42 | +set_os_arch() { |
| 43 | + if [ "$(uname)" == "Darwin" ]; then |
| 44 | + OS=darwin |
| 45 | + elif [ "$(uname -s)" == "Linux" ]; then |
| 46 | + OS=linux |
| 47 | + else |
| 48 | + OS=win32 |
| 49 | + fi |
| 50 | + |
| 51 | + ARCH="$(uname -m)" |
| 52 | + if [ "$ARCH" == "x86_64" ]; then |
| 53 | + ARCH=x64 |
| 54 | + elif [ "$ARCH" == "amd64" ]; then |
| 55 | + ARCH=x64 |
| 56 | + elif [ "$ARCH" == "arm64" ]; then |
| 57 | + if [ "$OS" == "darwin" ]; then |
46 | 58 | ARCH=arm64
|
47 |
| - else |
48 |
| - ARCH=arm |
49 |
| - fi |
50 |
| - elif [[ "\$ARCH" == aarch* ]]; then |
51 |
| - ARCH=arm |
52 | 59 | else
|
53 |
| - unsupported_arch $OS $ARCH |
| 60 | + ARCH=arm |
54 | 61 | fi
|
55 |
| - } |
| 62 | + elif [[ "$ARCH" == aarch* ]]; then |
| 63 | + ARCH=arm |
| 64 | + else |
| 65 | + unsupported_arch $OS $ARCH |
| 66 | + fi |
| 67 | +} |
56 | 68 |
|
57 |
| - download() { |
58 |
| - DOWNLOAD_DIR=$(mktemp -d) |
| 69 | +download() { |
| 70 | + DOWNLOAD_DIR=$(mktemp -d) |
59 | 71 |
|
60 |
| - TARGET="\$OS-\$ARCH" |
61 |
| - URL="https://github.com/graphprotocol/graph-tooling/releases/latest/download/graph-\$TARGET.tar.gz" |
62 |
| - echo "Downloading \$URL" |
| 72 | + TARGET="$OS-$ARCH" |
| 73 | + URL="https://github.com/graphprotocol/graph-tooling/releases/latest/download/graph-$TARGET.tar.gz" |
| 74 | + echo "Downloading $URL" |
63 | 75 |
|
64 |
| - if ! curl --progress-bar --fail -L "\$URL" -o "\$DOWNLOAD_DIR/graph.tar.gz"; then |
65 |
| - echo "Download failed." |
66 |
| - exit 1 |
67 |
| - fi |
| 76 | + if ! curl --progress-bar --fail -L "$URL" -o "$DOWNLOAD_DIR/graph.tar.gz"; then |
| 77 | + echo "Download failed." |
| 78 | + exit 1 |
| 79 | + fi |
68 | 80 |
|
69 |
| - echo "Downloaded to \$DOWNLOAD_DIR" |
| 81 | + echo "Downloaded to $DOWNLOAD_DIR" |
70 | 82 |
|
71 |
| - rm -rf "/usr/local/lib/graph" |
72 |
| - tar xzf "\$DOWNLOAD_DIR/graph.tar.gz" -C /usr/local/lib |
73 |
| - rm -rf "\$DOWNLOAD_DIR" |
74 |
| - echo "Unpacked to /usr/local/lib/graph" |
| 83 | + rm -rf "${INSTALL_DIR}/graph" |
| 84 | + tar xzf "$DOWNLOAD_DIR/graph.tar.gz" -C "$INSTALL_DIR" |
| 85 | + rm -rf "$DOWNLOAD_DIR" |
| 86 | + echo "Unpacked to $INSTALL_DIR" |
75 | 87 |
|
76 |
| - echo "Installing to /usr/local/bin/graph" |
77 |
| - rm -f /usr/local/bin/graph |
78 |
| - ln -s /usr/local/lib/graph/bin/graph /usr/local/bin/graph |
79 |
| - } |
| 88 | + if [ -n "$BIN_DIR" ] |
| 89 | + then |
| 90 | + echo "Installing to ${BIN_DIR}/graph" |
| 91 | + rm -f "$BIN_DIR/graph" |
| 92 | + ln -s "$INSTALL_DIR/graph/bin/graph" "$BIN_DIR/graph" |
| 93 | + LOCATION="$BIN_DIR/graph" |
| 94 | + else |
| 95 | + LOCATION="$INSTALL_DIR/graph/bin/graph" |
| 96 | + fi |
| 97 | +} |
80 | 98 |
|
81 |
| - set_os_arch |
82 |
| - download |
| 99 | +set_os_arch |
| 100 | +download |
83 | 101 |
|
84 |
| -SCRIPT |
85 |
| - LOCATION=$(command -v graph) |
86 |
| - echo "The Graph CLI installed to $LOCATION" |
87 |
| - graph --version |
88 |
| -} |
| 102 | +echo "The Graph CLI installed to $LOCATION" |
| 103 | +$LOCATION --version |
0 commit comments