forked from weaveworks/weave-gitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
149 lines (132 loc) · 2.89 KB
/
functions.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
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
# shellcheck shell=bash
log() {
echo "•" "$@"
}
error() {
log "error:" "$@"
exit 1
}
title_case() {
local param="${1}"
# shellcheck disable=SC2155,SC2086
local titled="$(tr '[:lower:]' '[:upper:]' <<<${param:0:1})${param:1}"
echo "$titled"
}
os() {
uname -s
}
goos() {
local os
os="$(uname -s)"
case "${os}" in
Linux*)
echo linux
;;
Darwin*)
echo darwin
;;
*)
error "unknown OS: ${os}"
;;
esac
}
arch() {
uname -m
}
goarch() {
local arch
arch="$(uname -m)"
case "${arch}" in
armv5*)
echo "armv5"
;;
armv6*)
echo "armv6"
;;
armv7*)
echo "armv7"
;;
aarch64)
echo "arm64"
;;
arm64)
echo "amd64"
;;
x86)
echo "386"
;;
x86_64)
echo "amd64"
;;
i686)
echo "386"
;;
i386)
echo "386"
;;
*)
error "uknown arch: ${arch}"
;;
esac
}
mktempdir() {
mktemp -d 2>/dev/null || mktemp -d -t 'wego'
}
do_curl() {
local path="${1}"
local url="${2}"
log "Downloading ${url}"
curl --progress-bar -fLo "${path}" "${url}"
}
do_curl_binary() {
local cmd="${1}"
local url="${2}"
local default_path="${HOME}/.wego/bin"
local path="${3:-${default_path}}"/"${cmd}"
do_curl "${path}" "${url}"
chmod +x "${path}"
}
do_curl_tarball() {
local cmd="${1}"
local url="${2}"
local default_path="${HOME}/.wego/bin"
local path="${3:-${default_path}}"/"${cmd}"
local checksum_path=""
dldir="$(mktempdir)"
mkdir "${dldir}/${cmd}"
do_curl "${dldir}/${cmd}.tar.gz" "${url}"
## if checksum_path is set validate tarball
if [ -n "${checksum_path}" ]
then
## need to validate file here because unpacking the tar changes the hash
validate_file "${checksum_path}" "${dldir}/${cmd}.tar.gz" "${cmd}"
fi
tar -C "${dldir}/${cmd}" -xvf "${dldir}/${cmd}.tar.gz"
mv "${dldir}/${cmd}/${cmd}" "${path}"
rm -rf "${dldir}"
}
do_curl_tarball_with_path() {
local cmd="${1}"
# shellcheck disable=SC2206
local url_and_path=(${2//;/ })
local default_path="${HOME}/.wego/bin"
local path="${3:-${default_path}}"
dldir="$(mktempdir)"
mkdir -p "${dldir}/${cmd}/${url_and_path[1]}"
do_curl "${dldir}/${cmd}.tar.gz" "${url_and_path[0]}"
tar -C "${dldir}/${cmd}" -xvf "${dldir}/${cmd}.tar.gz"
mv "${dldir}/${cmd}/${url_and_path[1]}" "${path}/${cmd}"
}
validate_file() {
local checksums_path="${1}"
local file_path="${2}"
local cmd="${3}"
# shellcheck disable=SC2155
local digest=$(openssl dgst -sha256 "${file_path}" | cut -d ' ' -f 2)
if ! grep "${digest}" "${checksums_path}"; then
echo "${cmd} is not a valid file"
rm -rf "${file_path}"
exit 1
fi
}