|
1 |
| -#!/bin/bash |
| 1 | +#!/bin/sh |
| 2 | +# TODO(everyone): Keep this script simple and easily auditable. |
2 | 3 |
|
3 |
| -set -e |
4 |
| - |
5 |
| -main() { |
6 |
| - # ANSI escape code variables |
7 |
| - BOLD=$(tput bold) |
8 |
| - NORMAL=$(tput sgr0) |
9 |
| - |
10 |
| - if ! command -v tar >/dev/null 2>&1; then |
11 |
| - echo "Error: 'tar' is required." 1>&2 |
12 |
| - exit 1 |
13 |
| - fi |
| 4 | +# Function to check if a command exists |
| 5 | +command_exists() { |
| 6 | + type "$1" &> /dev/null |
| 7 | +} |
14 | 8 |
|
15 |
| - if ! command -v jq >/dev/null 2>&1; then |
16 |
| - echo "Error: 'jq' is required." 1>&2 |
17 |
| - exit 1 |
18 |
| - fi |
| 9 | +set -e |
19 | 10 |
|
20 |
| - OS=$(uname -s) |
21 |
| - if [ "$OS" = "Windows_NT" ]; then |
22 |
| - echo "Error: Windows is not supported yet." 1>&2 |
| 11 | +if ! command -v tar >/dev/null; then |
| 12 | + echo "Error: 'tar' is required to install Tailpipe." 1>&2 |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +if ! command -v gzip >/dev/null; then |
| 17 | + echo "Error: 'gzip' is required to install Tailpipe." 1>&2 |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +if ! command -v install >/dev/null; then |
| 22 | + echo "Error: 'install' is required to install Tailpipe." 1>&2 |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +if [ "$OS" = "Windows_NT" ]; then |
| 27 | + echo "Error: Windows is not supported yet." 1>&2 |
| 28 | + exit 1 |
| 29 | +else |
| 30 | + case $(uname -sm) in |
| 31 | + "Darwin x86_64") target="darwin.amd64.tar.gz" ;; |
| 32 | + "Darwin arm64") target="darwin.arm64.tar.gz" ;; |
| 33 | + "Linux x86_64") target="linux.amd64.tar.gz" ;; |
| 34 | + "Linux aarch64") target="linux.arm64.tar.gz" ;; |
| 35 | + *) echo "Error: '$(uname -sm)' is not supported yet." 1>&2;exit 1 ;; |
| 36 | + esac |
| 37 | +fi |
| 38 | + |
| 39 | +if [ $# -eq 0 ]; then |
| 40 | + tailpipe_uri="https://github.com/turbot/tailpipe/releases/latest/download/tailpipe.${target}" |
| 41 | +else |
| 42 | + tailpipe_uri="https://github.com/turbot/tailpipe/releases/download/${1}/tailpipe.${target}" |
| 43 | +fi |
| 44 | + |
| 45 | +bin_dir="/usr/local/bin" |
| 46 | +exe="$bin_dir/tailpipe" |
| 47 | + |
| 48 | +test -z "$tmp_dir" && tmp_dir="$(mktemp -d)" |
| 49 | +mkdir -p "${tmp_dir}" |
| 50 | +tmp_dir="${tmp_dir%/}" |
| 51 | + |
| 52 | +echo "Created temporary directory at $tmp_dir. Changing to $tmp_dir" |
| 53 | +cd "$tmp_dir" |
| 54 | + |
| 55 | +# set a trap for a clean exit - even in failures |
| 56 | +trap 'rm -rf $tmp_dir' EXIT |
| 57 | + |
| 58 | +case $(uname -s) in |
| 59 | + "Darwin") zip_location="$tmp_dir/tailpipe.tar.gz" ;; |
| 60 | + "Linux") zip_location="$tmp_dir/tailpipe.tar.gz" ;; |
| 61 | + *) echo "Error: tailpipe is not supported on '$(uname -s)' yet." 1>&2;exit 1 ;; |
| 62 | +esac |
| 63 | + |
| 64 | +echo "Downloading from $tailpipe_uri" |
| 65 | +if command -v wget >/dev/null; then |
| 66 | + # because --show-progress was introduced in 1.16. |
| 67 | + wget --help | grep -q '\--showprogress' && _FORCE_PROGRESS_BAR="--no-verbose --show-progress" || _FORCE_PROGRESS_BAR="" |
| 68 | + # prefer an IPv4 connection, since github.com does not handle IPv6 connections properly. |
| 69 | + if ! wget --prefer-family=IPv4 --progress=bar:force:noscroll $_FORCE_PROGRESS_BAR -O "$zip_location" "$tailpipe_uri"; then |
| 70 | + echo "Could not find version $1" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +elif command -v curl >/dev/null; then |
| 74 | + # curl uses HappyEyeball for connections, therefore, no preference is required |
| 75 | + if ! curl --fail --location --progress-bar --output "$zip_location" "$tailpipe_uri"; then |
| 76 | + echo "Could not find version $1" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | +else |
| 80 | + echo "Unable to find wget or curl. Cannot download." |
23 | 81 | exit 1
|
24 |
| - else |
25 |
| - UNAME_SM=$(uname -sm) |
26 |
| - case "$UNAME_SM" in |
27 |
| - "Darwin x86_64") target="darwin.amd64.tar.gz" ;; |
28 |
| - "Darwin arm64") target="darwin.arm64.tar.gz" ;; |
29 |
| - "Linux x86_64") target="linux.amd64.tar.gz" ;; |
30 |
| - "Linux aarch64") target="linux.arm64.tar.gz" ;; |
31 |
| - *) echo "Error: '$UNAME_SM' is not supported yet." 1>&2; exit 1 ;; |
32 |
| - esac |
33 |
| - fi |
34 |
| - |
35 |
| - # Check if version is provided as an argument |
36 |
| - if [ $# -eq 0 ] || [ -z "$1" ]; then |
37 |
| - printf "Enter the version (latest): " |
38 |
| - read version |
39 |
| - version=${version:-latest} |
40 |
| - else |
41 |
| - version=$1 |
42 |
| - fi |
43 |
| - |
44 |
| - # Check if location is provided as an argument |
45 |
| - if [ $# -lt 2 ] || [ -z "$2" ]; then |
46 |
| - printf "Enter location (/usr/local/bin): " |
47 |
| - read location |
48 |
| - location=${location:-/usr/local/bin} |
49 |
| - else |
50 |
| - location=$2 |
51 |
| - fi |
52 |
| - |
53 |
| - bin_dir=$location |
54 |
| - exe="$bin_dir/tailpipe" |
55 |
| - |
56 |
| - tmp_dir=$(mktemp -d) |
57 |
| - mkdir -p "${tmp_dir}" |
58 |
| - tmp_dir="${tmp_dir%/}" |
59 |
| - |
60 |
| - echo "Created temporary directory at $tmp_dir." |
61 |
| - cd "$tmp_dir" || exit |
62 |
| - |
63 |
| - # set a trap for a clean exit - even in failures |
64 |
| - trap 'rm -rf $tmp_dir' EXIT |
65 |
| - |
66 |
| - case $(uname -s) in |
67 |
| - "Darwin" | "Linux") zip_location="$tmp_dir/tailpipe.${target}" ;; |
68 |
| - *) echo "Error: tailpipe is not supported on '$(uname -s)' yet." 1>&2; exit 1 ;; |
69 |
| - esac |
70 |
| - |
71 |
| - # Generate the URI for the binary |
72 |
| - if [ "$version" = "latest" ]; then |
73 |
| - uri="https://api.github.com/repos/turbot/tailpipe/releases/latest" |
74 |
| - asset_name="tailpipe.${target}" |
75 |
| - else |
76 |
| - uri="https://api.github.com/repos/turbot/tailpipe/releases/tags/${version}" |
77 |
| - asset_name="tailpipe.${target}" |
78 |
| - fi |
79 |
| - |
80 |
| - # Read the GitHub Personal Access Token |
81 |
| - GITHUB_TOKEN=${GITHUB_TOKEN:-} |
82 |
| - |
83 |
| - if [ -z "$GITHUB_TOKEN" ]; then |
84 |
| - echo "" |
85 |
| - echo "Error: GITHUB_TOKEN is not set. Please set your GitHub Personal Access Token as an environment variable." 1>&2 |
86 |
| - exit 1 |
87 |
| - fi |
88 |
| - AUTH="Authorization: token $GITHUB_TOKEN" |
89 |
| - |
90 |
| - response=$(curl -sH "$AUTH" $uri) |
91 |
| - id=$(echo "$response" | jq --arg asset_name "$asset_name" '.assets[] | select(.name == $asset_name) | .id' | tr -d '"') |
92 |
| - GH_ASSET="$uri/releases/assets/$id" |
93 |
| - |
94 |
| - echo "" |
95 |
| - echo "Downloading ${BOLD}${asset_name}${NORMAL}..." |
96 |
| - curl -#SL -H "$AUTH" -H "Accept: application/octet-stream" \ |
97 |
| - "https://api.github.com/repos/turbot/tailpipe/releases/assets/$id" \ |
98 |
| - -o "$zip_location" -L --create-dirs |
99 |
| - |
100 |
| - file "$zip_location" |
| 82 | +fi |
101 | 83 |
|
102 |
| - echo "Deflating downloaded archive" |
103 |
| - tar -xvf "$zip_location" -C "$tmp_dir" |
| 84 | +echo "Deflating downloaded archive" |
| 85 | +tar -xf "$zip_location" -C "$tmp_dir" |
104 | 86 |
|
105 |
| - echo "Installing" |
106 |
| - install -d "$bin_dir" |
107 |
| - install "$tmp_dir/tailpipe" "$bin_dir" |
| 87 | +echo "Installing" |
| 88 | +install -d "$bin_dir" |
| 89 | +install "$tmp_dir/tailpipe" "$bin_dir" |
108 | 90 |
|
109 |
| - echo "Applying necessary permissions" |
110 |
| - chmod +x $exe |
| 91 | +echo "Applying necessary permissions" |
| 92 | +chmod +x $exe |
111 | 93 |
|
112 |
| - echo "Removing downloaded archive" |
113 |
| - rm "$zip_location" |
| 94 | +echo "Removing downloaded archive" |
| 95 | +rm "$zip_location" |
114 | 96 |
|
115 |
| - echo "tailpipe was installed successfully to $bin_dir" |
116 |
| - |
117 |
| - if ! command -v $bin_dir/tailpipe >/dev/null 2>&1; then |
118 |
| - echo "tailpipe was installed, but could not be executed. Are you sure '$bin_dir/tailpipe' has the necessary permissions?" |
119 |
| - exit 1 |
120 |
| - fi |
121 |
| -} |
| 97 | +echo "Tailpipe was installed successfully to $exe" |
122 | 98 |
|
123 |
| -# Call the main function to run the script |
124 |
| -main "$@" |
| 99 | +if ! command -v $bin_dir/tailpipe >/dev/null; then |
| 100 | + echo "Tailpipe was installed, but could not be executed. Are you sure '$bin_dir/tailpipe' has the necessary permissions?" |
| 101 | + exit 1 |
| 102 | +fi |
0 commit comments