-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathinstall.sh
executable file
·119 lines (104 loc) · 3.27 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/sh
# Exit as soon as any command fails
set -e
REPO="onflow/flow-cli"
ASSETS_URL="https://github.com/$REPO/releases/download/"
# The version to download, set by get_version (defaults to args[1])
VERSION="$1"
# The architecture string, set by get_architecture
ARCH=""
# Optional environment variable for Github API token
# If GITHUB_TOKEN is set, use it in the curl requests to avoid rate limiting
github_token_header=""
if [ -n "$GITHUB_TOKEN" ]; then
github_token_header="Authorization: Bearer $GITHUB_TOKEN"
fi
# Get the architecture (CPU, OS) of the current system as a string.
# Only MacOS/x86_64/ARM64 and Linux/x86_64/ARM64 architectures are supported.
get_architecture() {
_ostype="$(uname -s)"
_cputype="$(uname -m)"
_targetpath=""
if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ]; then
if sysctl hw.optional.x86_64 | grep -q ': 1'; then
_cputype=x86_64
fi
fi
case "$_ostype" in
Linux)
_ostype=linux
_targetpath=$HOME/.local/bin
;;
Darwin)
_ostype=darwin
_targetpath=/usr/local/bin
;;
*)
echo "unrecognized OS type: $_ostype"
return 1
;;
esac
case "$_cputype" in
x86_64 | x86-64 | x64 | amd64)
_cputype=amd64
;;
arm64 | aarch64)
_cputype=arm64
;;
*)
echo "unknown CPU type: $_cputype"
return 1
;;
esac
_arch="${_ostype}-${_cputype}"
ARCH="${_arch}"
TARGET_PATH="${_targetpath}"
}
# Get the latest version from remote if none specified in args.
get_version() {
if [ -z "$VERSION" ]
then
VERSION=""
if [ -n "$github_token_header" ]
then
VERSION=$(curl -H "$github_token_header" -s "https://api.github.com/repos/$REPO/releases/latest" | grep -E 'tag_name' | cut -d '"' -f 4)
else
VERSION=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep -E 'tag_name' | cut -d '"' -f 4)
fi
if [ -z "$VERSION" ] && [ -n "$github_token_header" ]
then
echo "Failed to get latest version from Github API, is your GITHUB_TOKEN valid? Trying without authentication..."
github_token_header=""
get_version
fi
fi
}
# Determine the system architecure, download the appropriate binary, and
# install it in `/usr/local/bin` on macOS and `~/.local/bin` on Linux
# with executable permission.
main() {
get_architecture || exit 1
get_version || exit 1
echo "Downloading version $VERSION ..."
tmpfile=$(mktemp 2>/dev/null || mktemp -t flow)
url="$ASSETS_URL$VERSION/flow-cli-$VERSION-$ARCH.tar.gz"
if [ -n "$github_token_header" ]
then
curl -H "$github_token_header" -L --progress-bar "$url" -o $tmpfile
else
curl -L --progress-bar "$url" -o $tmpfile
fi
# Ensure we don't receive a not found error as response.
if grep -q "Not Found" $tmpfile
then
echo "Version $VERSION could not be found"
exit 1
fi
[ -d $TARGET_PATH ] || mkdir -p $TARGET_PATH
tar -xf $tmpfile -C $TARGET_PATH
mv $TARGET_PATH/flow-cli $TARGET_PATH/flow
chmod +x $TARGET_PATH/flow
echo "Successfully installed the Flow CLI to $TARGET_PATH."
echo "Make sure $TARGET_PATH is in your \$PATH environment variable."
}
main