-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-binary.sh
executable file
·136 lines (122 loc) · 3.46 KB
/
install-binary.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env sh
# this was copied from [email protected]:databus23/helm-diff
cd $HELM_PLUGIN_DIR
set -e
if [ "$HELM_DEBUG" = true ]; then
set -x
env | sort
fi
REPO_URL=$(git remote get-url origin)
PROJECT_NAME=${HELM_PLUGIN_DIR##*/}
PROJECT_GH=$(echo "${REPO_URL}" | awk -F'/' '{print $4"/"$5}' | sed -r 's!\.git$!!g')
export GREP_COLOR="never"
# Convert HELM_BIN and HELM_PLUGIN_DIR to unix if cygpath is
# available. This is the case when using MSYS2 or Cygwin
# on Windows where helm returns a Windows path but we
# need a Unix path
if command -v cygpath >/dev/null 2>&1; then
HELM_BIN="$(cygpath -u "${HELM_BIN}")"
HELM_PLUGIN_DIR="$(cygpath -u "${HELM_PLUGIN_DIR}")"
fi
SCRIPT_MODE="install"
if [ "$1" = "-u" ]; then
SCRIPT_MODE="update"
fi
# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5" ;;
armv6*) ARCH="armv6" ;;
armv7*) ARCH="armv7" ;;
aarch64) ARCH="arm64" ;;
x86) ARCH="386" ;;
x86_64) ARCH="amd64" ;;
i686) ARCH="386" ;;
i386) ARCH="386" ;;
*)
echo echo "Arch '$ARCH' not supported!" >&2
exit 1
;;
esac
}
# initOS discovers the operating system for this system.
initOS() {
OS=$(uname -s)
case "$OS" in
Windows_NT) OS='windows' ;;
# Msys support
MSYS*) OS='windows' ;;
# Minimalist GNU for Windows
MINGW*) OS='windows' ;;
CYGWIN*) OS='windows' ;;
Darwin) OS='macos' ;;
Linux) OS='linux' ;;
*)
echo echo "Arch '$OS' not supported!" >&2
exit 1
;;
esac
}
# Temporary dir
mkTempDir() {
HELM_TMP="$(mktemp -d -t "${PROJECT_NAME}-XXXXXX")"
}
rmTempDir() {
if [ -d "${HELM_TMP:-/tmp/${PROJECT_NAME}-tmp}" ]; then
rm -rf "${HELM_TMP:-/tmp/${PROJECT_NAME}-tmp}"
fi
}
# downloadFile downloads the latest binary package and also the checksum
# for that binary.
downloadFile() {
if command -v curl >/dev/null 2>&1; then
DOWNLOAD_CMD="curl -sSf -L"
elif command -v wget >/dev/null 2>&1; then
DOWNLOAD_CMD="wget -q -O -"
else
echo "Either curl or wget is required"
exit 1
fi
version=$(git describe --tags --exact-match 2>/dev/null | sed 's/^v//g' || :)
if [ "$SCRIPT_MODE" = "install" ] && [ -n "$version" ]; then
DOWNLOAD_URL="https://github.com/$PROJECT_GH/releases/download/v$version/${PROJECT_NAME}_${version}_${OS}_${ARCH}.tar.gz"
else
version=$(curl -sL "https://api.github.com/repos/$PROJECT_GH/releases/latest" | grep tag_name | head -1 | sed -r 's/[^:]*: "v([^"]*).*/\1/g')
DOWNLOAD_URL="https://github.com/$PROJECT_GH/releases/download/v$version/${PROJECT_NAME}_${version}_${OS}_${ARCH}.tar.gz"
fi
PLUGIN_TMP_FILE="${HELM_TMP}/${PROJECT_NAME}.tgz"
echo "Downloading $DOWNLOAD_URL"
$DOWNLOAD_CMD "$DOWNLOAD_URL" >"$PLUGIN_TMP_FILE"
}
# installFile verifies the SHA256 for the file, then unpacks and
# installs it.
installFile() {
tar xzf "$PLUGIN_TMP_FILE" -C "$HELM_TMP"
echo "Preparing to install into ${HELM_PLUGIN_DIR}"
cp -r "$HELM_TMP/." "$HELM_PLUGIN_DIR/"
}
# exit_trap is executed if on exit (error or not).
exit_trap() {
result=$?
rmTempDir
if [ "$result" != "0" ]; then
echo "Failed to install $PROJECT_NAME"
echo "For support, go to ${REPO_URL}."
fi
exit $result
}
# testVersion tests the installed client to make sure it is working.
testVersion() {
echo "${PROJECT_NAME} installed into $HELM_PLUGIN_DIR"
"$HELM_BIN" "$HELM_PLUGIN_NAME" -h
}
# Execution
#Stop execution on any error
trap "exit_trap" EXIT
initArch
initOS
mkTempDir
downloadFile
installFile
testVersion