-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall
More file actions
186 lines (162 loc) · 4.79 KB
/
install
File metadata and controls
186 lines (162 loc) · 4.79 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
CORE_CLI_DOWNLOAD_ROOT=https://github.com/core-coin/core-cli/releases/download
#!/bin/sh
# Copyright 2016 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
# This is just a little script that can be downloaded from the internet to
# install core-cli. It just does platform detection, downloads the installer
# and runs it.
{ # this ensures the entire script is downloaded #
GH_LATEST_RELEASE="https://api.github.com/repos/core-coin/core-cli/releases/latest"
set -e
main() {
downloader --check
need_cmd uname
need_cmd mktemp
need_cmd chmod
need_cmd mkdir
need_cmd rm
need_cmd sed
need_cmd grep
_ostype="$(uname -s)"
_cputype="$(uname -m)"
case "$_ostype" in
Linux)
_ostype=linux
;;
Darwin)
_ostype=darwin
;;
MINGW*|MSYS*|CYGWIN*|Windows_NT)
_ostype=windows
;;
*)
err "machine architecture is currently unsupported"
;;
esac
TARGET="${_ostype}-${_cputype}"
core_cli_dir="$HOME/.core-cli"
ensure mkdir -p "$core_cli_dir"
# Check for CORE_CLI_RELEASE environment variable override. Otherwise fetch
# the latest release tag from github
release_file="$core_cli_dir/release"
printf 'looking for latest release\n' 1>&2
ensure downloader "$GH_LATEST_RELEASE" "$release_file"
release=$(\
grep -m 1 \"tag_name\": "$release_file" \
| sed -ne 's/^ *"tag_name": "\([^"]*\)",$/\1/p' \
)
if [ -z "$release" ]; then
err 'Unable to figure latest release'
fi
case "$TARGET" in
linux-x86_64)
gh_binary_name="core-cli-linux-x86_64"
;;
linux-aarch64)
gh_binary_name="core-cli-linux-arm64"
;;
darwin-x86_64)
gh_binary_name="core-cli-darwin-x86_64"
;;
darwin-arm64)
gh_binary_name="core-cli-darwin-arm64"
;;
windows-x86_64)
gh_binary_name="core-cli-windows-x86_64.exe"
;;
*)
err "Unsupported target: $TARGET"
;;
esac
binary_name="core-cli"
if [ "$_ostype" = "windows" ]; then
binary_name="core-cli.exe"
fi
download_url="$CORE_CLI_DOWNLOAD_ROOT/$release/$gh_binary_name"
core_cli_install_init="$core_cli_dir/$binary_name"
printf 'downloading %s core-cli\n' "$release" 1>&2
ensure downloader "$download_url" "$core_cli_install_init"
ensure chmod u+x "$core_cli_install_init"
# Add the .core-cli directory to the PATH
case "$_ostype" in
linux|darwin)
profile_files=("$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zshrc")
profile_file_found=false
for profile_file in "${profile_files[@]}"; do
if [ -f "$profile_file" ]; then
profile_file_found=true
if ! grep -q "$core_cli_dir" "$profile_file"; then
echo "export PATH=\$PATH:$core_cli_dir" >> "$profile_file"
export PATH=$PATH:$core_cli_dir
fi
fi
done
if [ "$profile_file_found" = false ]; then
touch "$HOME/.profile"
echo "export PATH=\$PATH:$core_cli_dir" >> "$HOME/.profile"
export PATH=$PATH:$core_cli_dir
fi
;;
windows)
if ! echo "$PATH" | grep -q "$core_cli_dir"; then
setx PATH "$PATH;$core_cli_dir"
fi
;;
esac
reset && exec $SHELL
}
err() {
printf 'core-cli-install-init: %s\n' "$1" >&2
exit 1
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then
err "command failed: $*"
fi
}
# This is just for indicating that commands' results are being
# intentionally ignored. Usually, because it's being executed
# as part of error handling.
ignore() {
"$@"
}
# This wraps curl or wget. Try curl first, if not installed,
# use wget instead.
downloader() {
if check_cmd curl; then
program=curl
elif check_cmd wget; then
program=wget
else
program='curl or wget' # to be used in error message of need_cmd
fi
if [ "$1" = --check ]; then
need_cmd "$program"
elif [ "$program" = curl ]; then
curl -sSfL "$1" -o "$2"
elif [ "$program" = wget ]; then
wget "$1" -O "$2"
else
err "Unknown downloader" # should not reach here
fi
}
main "$@"
} # this ensures the entire script is downloaded #