|
4 | 4 | # |
5 | 5 | # $ sh -c "$(curl -L https://github.com/intercloud/gobinsec/releases/latest/download/install)" |
6 | 6 | # |
7 | | -# or (if you don't have curl installed): |
8 | | -# |
9 | | -# $ sh -c "$(wget -O - https://github.com/intercloud/gobinsec/releases/latest/download/install)" |
10 | 7 |
|
11 | 8 | set -e |
12 | 9 |
|
13 | | -NAME="gobinsec" |
| 10 | +log() { |
| 11 | + echo "$@" |
| 12 | +} |
14 | 13 |
|
15 | | -# get OS and ARCH and build binary name |
16 | | -os=`uname | tr '[:upper:]' '[:lower:]'` |
17 | | -arch=`uname -m` |
| 14 | +logF() { |
| 15 | + echo >&2 "$@" |
| 16 | + exit 1 |
| 17 | +} |
| 18 | + |
| 19 | +repo="gobinsec" |
| 20 | +owner="intercloud" |
| 21 | +binary="${repo}" |
| 22 | + |
| 23 | +get_os() { |
| 24 | + echo $(uname -s | awk '{print tolower($0)}') |
| 25 | +} |
18 | 26 |
|
19 | | -if [ "$arch" = "i386" ]; then |
20 | | - arch="386" |
21 | | -elif [ "$arch" = "x86_64" ]; then |
22 | | - arch="amd64" |
| 27 | +get_arch() { |
| 28 | + a=$(uname -m) |
| 29 | + case ${a} in |
| 30 | + "x86_64" | "amd64") |
| 31 | + echo "x86_64" |
| 32 | + ;; |
| 33 | + "i386" | "i486" | "i586") |
| 34 | + echo "i386" |
| 35 | + ;; |
| 36 | + *) |
| 37 | + echo ${NIL} |
| 38 | + ;; |
| 39 | + esac |
| 40 | +} |
| 41 | + |
| 42 | +if [ -z "$githubUrl" ]; then |
| 43 | + githubUrl="https://github.com" |
23 | 44 | fi |
24 | 45 |
|
25 | | -echo "os: ${os}" |
26 | | -echo "arch: ${arch}" |
| 46 | +# parse flag |
| 47 | +for i in "$@"; do |
| 48 | + case $i in |
| 49 | + -v=* | --version=*) |
| 50 | + version="${i#*=}" |
| 51 | + shift |
| 52 | + ;; |
| 53 | + *) |
| 54 | + # unknown option |
| 55 | + ;; |
| 56 | + esac |
| 57 | +done |
| 58 | + |
| 59 | +if [ -z "$version" ]; then |
| 60 | + version="latest" |
| 61 | +fi |
27 | 62 |
|
28 | | -binary="${NAME}-${os}-${arch}" |
| 63 | +# get OS and ARCH and build binary name |
| 64 | +os=$(get_os) |
| 65 | +arch=$(get_arch) |
| 66 | + |
| 67 | +log "os: ${os}" |
| 68 | +log "arch: ${arch}" |
| 69 | +log "version: ${version:-latest}" |
| 70 | + |
| 71 | +fileName="${binary}_${os}_${arch}.tar.gz" |
29 | 72 |
|
30 | 73 | # set default installation directory |
31 | | -if [ -d "/opt/local/bin" ] |
32 | | -then |
33 | | - DEFAULT_DIR="/opt/local/bin" |
34 | | -elif [ -d "/opt/bin" ] |
35 | | -then |
36 | | - DEFAULT_DIR="/opt/bin" |
37 | | -elif [ -d "/usr/local/bin" ] |
38 | | -then |
39 | | - DEFAULT_DIR="/usr/local/bin" |
40 | | -elif [ -d "/usr/bin" ] |
41 | | -then |
42 | | - DEFAULT_DIR="/usr/bin" |
| 74 | +if [ -d "/opt/local/bin" ]; then |
| 75 | + executableFolder="/opt/local/bin" |
| 76 | +elif [ -d "/opt/bin" ]; then |
| 77 | + executableFolder="/opt/bin" |
| 78 | +elif [ -d "/usr/local/bin" ]; then |
| 79 | + executableFolder="/usr/local/bin" |
| 80 | +elif [ -d "/usr/bin" ]; then |
| 81 | + executableFolder="/usr/bin" |
43 | 82 | else |
44 | | - DEFAULT_DIR="/bin" |
| 83 | + executableFolder="/bin" |
45 | 84 | fi |
46 | 85 |
|
47 | | -# select command to download binary |
48 | | -if hash curl 2>/dev/null |
49 | | -then |
50 | | - command="curl -L -o" |
51 | | -elif hash wget 2>/dev/null |
52 | | -then |
53 | | - command="wget -O" |
54 | | -else |
55 | | - echo "You must install curl or wget to run this installation script" |
56 | | - exit 1 |
57 | | -fi |
| 86 | +# check command availability |
| 87 | +command -v curl >/dev/null 2>&1 || { logF "curl is required"; } |
| 88 | + |
| 89 | +# Set URI to download |
| 90 | +assetUri="${githubUrl}/${owner}/${repo}/releases/download/${version}/${fileName}" |
| 91 | +if [ "${version}" == "latest" ]; then |
58 | 92 |
|
59 | | -# download binary in /tmp/${NAME} and make it executable |
60 | | -${command} /tmp/${NAME} https://github.com/intercloud/${NAME}/releases/latest/download/${binary} |
61 | | -chmod +x /tmp/${NAME} |
| 93 | + asset=$( |
| 94 | + curl -sSf ${githubUrl}/${owner}/${repo}/releases | |
| 95 | + grep -o "${owner}/${repo}/releases/download/.*/${fileName}" | |
| 96 | + head -n 1 |
| 97 | + ) |
62 | 98 |
|
63 | | -# prompt for installation directory |
64 | | -read -p "Installation directory [${DEFAULT_DIR}]? " directory |
65 | | -if [ -z "$directory" ] |
66 | | -then |
67 | | - directory=${DEFAULT_DIR} |
| 99 | + assetUri="${githubUrl}/${asset}" |
68 | 100 | fi |
69 | 101 |
|
70 | | -# copy binary to installation directory |
71 | | -if [ -w "${directory}" ] |
72 | | -then |
73 | | - mv /tmp/${NAME} ${directory} |
| 102 | +downloadFolder="/tmp/${binary}" |
| 103 | +mkdir -p ${downloadFolder} |
| 104 | +downloadedFile="${downloadFolder}/${fileName}" |
| 105 | + |
| 106 | +log "[1/2] Download ${assetUri}" |
| 107 | +curl -sS --fail --location --output "${downloadedFile}" "${assetUri}" |
| 108 | + |
| 109 | +log "[2/2] Install ${binary} to '${executableFolder}'" |
| 110 | +if [ -w "${directory}" ]; then |
| 111 | + tar -xz -f ${downloadedFile} -C ${executableFolder} |
| 112 | + chmod +x ${executableFolder}/${binary} |
74 | 113 | else |
75 | | - sudo mv /tmp/${NAME} ${directory} |
76 | | - sudo chown root: ${directory}/${NAME} |
| 114 | + sudo tar -xz -f ${downloadedFile} -C ${executableFolder} |
| 115 | + sudo chmod +x ${executableFolder}/${binary} |
77 | 116 | fi |
78 | 117 |
|
79 | | -echo "${NAME} installed in '${directory}' directory" |
| 118 | +echo "${binary} installed in '${executableFolder}' directory" |
0 commit comments