forked from nathanchance/env
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon
More file actions
executable file
·316 lines (255 loc) · 7.77 KB
/
common
File metadata and controls
executable file
·316 lines (255 loc) · 7.77 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2016-2019 Nathan Chancellor
#
# Helper functions
###############
# #
# VARIABLES #
# #
###############
# What OS are we on?
HOST=$(uname -n | tr '[:upper:]' '[:lower:]')
# Location of "scripts" folder
SCRIPTS_FOLDER=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" || return; pwd)
BIN_FOLDER=${SCRIPTS_FOLDER}/bin
# Common locations amongst scripts
BUILD_FOLDER=/mnt/build
MAIN_FOLDER=${HOME}
CBL_FOLDER=${MAIN_FOLDER}/cbl
KERNEL_FOLDER=${MAIN_FOLDER}/kernels
MISC_FOLDER=${MAIN_FOLDER}/misc
REPO_FOLDER=${MAIN_FOLDER}/repos
ROM_FOLDER=${MAIN_FOLDER}/roms
TC_FOLDER=${MAIN_FOLDER}/toolchains
WEB_FOLDER=/var/www
# Colors
BLINK_RED="\033[05;31m"
BLUE="\033[01;34m"
BOLD="\033[1m"
GRN="\033[01;32m"
RED="\033[01;31m"
RST="\033[0m"
YLW="\033[01;33m"
# Number of make threads
THREADS=$(($(nproc --all) + 1))
JOBS_FLAG="-j${THREADS}"
# Common ccache variable
CCACHE=$(command -v ccache)
# Telegram chat IDs
MARLIN_CHANNEL=-1001169681748
MARLIN_GROUP=-1001132593794
OP6_CHANNEL=-1001088909643
OP6_GROUP=-1001192694857
WAHOO_CHANNEL=-1001354325766
WAHOO_GROUP=-1001298464571
# Server IPs
RYZEN_IP="95.216.41.81"
VULTR_IP="209.250.235.204"
# Latest AOSP Clang version
LACV=clang-r349610b
# Export everything to satisfy shellcheck
export HOST SCRIPTS_FOLDER BIN_FOLDER CBL_FOLDER KERNEL_FOLDER MISC_FOLDER REPO_FOLDER \
BUILD_FOLDER ROM_FOLDER TC_FOLDER WEB_FOLDER MAIN_FOLDER \
BLINK_RED BLUE BOLD GRN RED RST YLW \
THREADS JOBS_FLAG CCACHE MARLIN_CHANNEL MARLIN_GROUP OP6_CHANNEL \
OP6_GROUP WAHOO_CHANNEL WAHOO_GROUP RYZEN_IP VULTR_IP LACV
###############
# #
# FUNCTIONS #
# #
###############
# Alias for echo to print escape codes
function echo() {
command echo -e "${@}"
}
# Prints a formatted header to point out what is being done to the user
function header() {
if [[ -n ${2} ]]; then
COLOR=${2}
else
COLOR=${RED}
fi
echo "${COLOR}"
echo "====$(for i in $(seq ${#1}); do echo "=\c"; done)===="
echo "== ${1} =="
# SC2034: i appears unused. Verify it or export it.
# shellcheck disable=SC2034
echo "====$(for i in $(seq ${#1}); do echo "=\c"; done)===="
echo "${RST}"
}
# Formats the time
function format_time() {
local TIME_STRING
MINS=$(((${2} - ${1}) / 60))
SECS=$(((${2} - ${1}) % 60))
if [[ ${MINS} -ge 60 ]]; then
HOURS=$((MINS / 60))
MINS=$((MINS % 60))
fi
if [[ ${HOURS} -eq 1 ]]; then
TIME_STRING+="1 HOUR, "
elif [[ ${HOURS} -ge 2 ]]; then
TIME_STRING+="${HOURS} HOURS, "
fi
if [[ ${MINS} -eq 1 ]]; then
TIME_STRING+="1 MINUTE"
else
TIME_STRING+="${MINS} MINUTES"
fi
if [[ ${SECS} -eq 1 && -n ${HOURS} ]]; then
TIME_STRING+=", AND 1 SECOND"
elif [[ ${SECS} -eq 1 && -z ${HOURS} ]]; then
TIME_STRING+=" AND 1 SECOND"
elif [[ ${SECS} -ne 1 && -n ${HOURS} ]]; then
TIME_STRING+=", AND ${SECS} SECONDS"
elif [[ ${SECS} -ne 1 && -z ${HOURS} ]]; then
TIME_STRING+=" AND ${SECS} SECONDS"
fi
echo "${TIME_STRING}"
}
# Prints an error in bold red
function display_error() {
echo
echo "${RED}${1}${RST}"
[[ -z ${2} ]] && echo
}
# Prints an error in bold red and exits the script
function die() {
if [[ ${TG} ]]; then
local TMP_FILE
TMP_FILE=$(mktemp)
# Info must be resourced so public chats don't get notified
load_botinfo
{
echo "\`\`\`"
echo "An error was detected while running the following command:"
echo
echo "$(basename "${0}") ${PARAMS}"
echo
echo "The error was:"
echo
echo "${*}"
echo "\`\`\`"
} > "${TMP_FILE}"
tg_msg "$(cat "${TMP_FILE}")"
rm -f "${TMP_FILE}"
fi
display_error "${@}"
if type -p pre_exit_commands; then
pre_exit_commands
fi
exit 1
}
# Prints a warning in bold yellow
function warn() {
echo
echo "${YLW}${1}${RST}"
[[ -z ${2} ]] && echo
}
# Enforces the value needed for two-part flags
function enforce_value() {
[[ ${#} -lt 1 ]] && die "A additional value is needed for one of the flags passed to this script!"
}
# Add a remote if it doesn't exist
function add_remote() {
if ! git remote | grep -q -m 1 "${1}"; then
git remote add "${1}" "${2}"
fi
}
# Convert a file location to web link
function web_link() {
echo "${1}" | sed s/"$(echo "${WEB_FOLDER}" | sed 's/\//\\\//g')"/https:\\/\\/nathanchance.me/
}
# Set up a virtual environment for Python
function mkavenv {
virtualenv2 "${MAIN_FOLDER}/venv"
source "${MAIN_FOLDER}/venv/bin/activate"
}
# Remove virtual environment
function rmvenv {
deactivate
rm -rf "${MAIN_FOLDER}/venv"
}
# Source Telegram bot information
function load_botinfo() {
[[ -f ${MAIN_FOLDER}/.botinfo ]] && source ~/.botinfo
}
load_botinfo
# Telegram notifier function
function tg_msg() {
if [[ -z ${TOKEN} ]]; then
display_error "tg_msg() was called but there was no token!"
return 1
fi
if [[ -z ${CHAT_ID} ]]; then
display_error "tg_msg() was called but there was no chat ID!"
return 1
fi
curl -s -X POST https://api.telegram.org/bot"${TOKEN}"/sendMessage \
-d chat_id="${CHAT_ID}" \
-d parse_mode="Markdown" \
-d text="${*}" 1>/dev/null
}
# Telegram uploader function
function tg_upload() {
local FILE; FILE=${1}; shift
if [[ -z ${TOKEN} ]]; then
display_error "tg_upload() was called but there was no token!"
return 1
fi
if [[ -z ${CHAT_ID} ]]; then
display_error "tg_upload() was called but there was no chat ID!"
return 1
fi
if [[ ! -f ${FILE} ]]; then
display_error "tg_upload() failed to find ${FILE}!"
return 1
fi
curl -s -F chat_id="${CHAT_ID}" \
-F document=@"${FILE}" \
-F caption="${*}" \
-X POST https://api.telegram.org/bot"${TOKEN}"/sendDocument 1>/dev/null
}
# Remove URLs from Clang version
function clang_version() {
[[ ${#} -lt 1 ]] && { display_error "This function takes an argument!"; return; }
local CLANG POS
CLANG=${1}
if [[ $(basename "${CLANG}") != "clang" ]]; then
CLANG=$(find "${CLANG}" -name clang | head -n1)
[[ -z ${CLANG} ]] && { display_error "You didn't supply a Clang folder/binary!"; return; }
else
[[ ! -f ${CLANG} ]] && { display_error "Clang binary supplied doesn't exist!"; return; }
fi
case "${CLANG}" in
*aosp*) POS="1,2" ;;
*) POS="1" ;;
esac
"${CLANG}" --version | head -n 1 | cut -d \( -f "${POS}" | sed 's/[[:space:]]*$//' || display_error "Something went wrong!"
}
# Is GPG passphrase cached?
# https://demu.red/blog/2016/06/how-to-check-if-your-gpg-key-is-in-cache/
function gpg_available() {
NUM=$(gpg-connect-agent 'keyinfo --list' /bye 2>/dev/null | awk 'BEGIN{CACHED=0} /^S/ {if($7==1){CACHED=1}} END{if($0!=""){print CACHED} else {print "none"}}')
if [[ ${NUM} = "none" || ${NUM} -eq 0 ]]; then
die "Please run 'echo \"test\" | gpg --clearsign &>/dev/null' to cache gpg passphrase!"
fi
}
# ripgrep wrapper
function rg() {
# Bail out if rg is not installed
command -v rg &>/dev/null || { warn "ripgrep is not installed!"; return; }
# Colors match ag
command rg --colors "path:fg:green" \
--colors "path:style:bold" \
--colors "line:fg:yellow" \
--colors "line:style:bold" \
--colors "column:fg:yellow" \
--colors "column:style:bold" \
--colors "match:fg:black" \
--colors "match:bg:yellow" \
"${@}"
}