-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_grpcurl
executable file
·108 lines (91 loc) · 2.4 KB
/
get_grpcurl
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
#!/bin/sh
set -e
UNATTENDED="false"
if [ "$#" = "1" ] && [ "$1" = "-y" ]; then
UNATTENDED="true"
elif [ "$#" -gt 1 ]; then
echo "Usage: $0 [-y]" 1>&2
exit 1
fi
detect_os () {
RAW_OS=$(uname -s)
OS="linux"
case "$RAW_OS" in
"Linux") OS="linux";;
"Darwin") OS="osx";;
*) echo "Unsupported operating system \"${RAW_OS}\"" 1>&2; exit 1;;
esac
echo "${OS}"
}
detect_architecture () {
RAW_ARCH=$(uname -m)
ARCH="x86_64"
case "$RAW_ARCH" in
"x86_64") ARCH="x86_64";;
"i386") ARCH="x86_32";;
*) echo "Unsupported architecture \"${RAW_ARCH}\"" 1>&2; exit 1;;
esac
echo "${ARCH}"
}
detect_platform () {
OS=$(detect_os)
ARCH=$(detect_architecture)
echo "${OS}_${ARCH}"
}
confirm () {
if [ "$UNATTENDED" = "true" ]; then
return 0
fi
echo "$1"
printf "[y/n]"
read -r RESPONSE </dev/tty
printf "\n"
case "$RESPONSE" in
"yes" | "y" | "YES" | "Y" ) return 0;;
"no" | "n" | "NO" | "N" ) echo "Aborting." 1>&2; exit 1;;
*) echo "Unrecognized response. Aborting." 1>&2; exit 1;;
esac
}
ensure_command () {
if command -v "$1" 1>/dev/null 2>&1; then
return 0
else
echo "$1 is not installed. Please install it to proceed." 1>&2
exit 1
fi
}
modify_config_file () {
FILE="$1"
BIN_DIR="$2"
BACKUP="${FILE}.bak.$(date | sed 's/ /-/g')"
if ! stat "${FILE}" 1>/dev/null 2>&1; then
return 1
fi
cp "${FILE}" "${BACKUP}"
printf "\n# Added by https://grpc.io/get_grpcurl\nexport PATH=\"\$PATH:%s\"\n" "${BIN_DIR}" >> "${FILE}"
}
ensure_command "curl"
ensure_command "cut"
ensure_command "cp"
ensure_command "date"
ensure_command "grep"
ensure_command "mkdir"
ensure_command "sed"
ensure_command "stat"
ensure_command "uname"
GITHUB_URL="https://github.com"
GRPCURL_REPO="fullstorydev/grpcurl"
PLATFORM=$(detect_platform)
BIN_DIR="${HOME}/.grpcurl/bin"
confirm "This script will perform a user install of grpcurl. Would you like to proceed?"
mkdir -p "${BIN_DIR}"
curl -Ls "${GITHUB_URL}/${GRPCURL_REPO}/releases/latest" | \
grep "href=.*${PLATFORM}.tar.gz" | \
cut -d '"' -f 2 | \
echo "${GITHUB_URL}$(cat -)" | \
curl -sL "$(cat -)" --output - | \
tar -C "${BIN_DIR}" -xzf - grpcurl
modify_config_file "${HOME}/.bashrc" "${BIN_DIR}" || \
modify_config_file "${HOME}/.bash_profile" "${BIN_DIR}" || \
modify_config_file "${HOME}/.profile" "${BIN_DIR}"
echo "${BIN_DIR} has been added to your PATH. Please open a new shell to use grpcurl."