-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.bash
More file actions
124 lines (102 loc) · 2.25 KB
/
core.bash
File metadata and controls
124 lines (102 loc) · 2.25 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
#!/usr/bin/env bash
#shellcheck disable=SC2206,SC2207,SC2016
# Return true if it is: true, 1 yes, y, on or enable
_check_true() {
[[ ${1:-} == true || ${1:-} == 1 || ${1:-} == yes || ${1:-} == y || ${1:-} == on || ${1:-} == enable ]]
}
# Return false if it is: false, 0 no, n, off or disable
_check_false() {
! [[ ${1:-} == false || ${1:-} == 0 || ${1:-} == no || ${1:-} == n || ${1:-} == off || ${1:-} == disable ]]
}
_w() {
echo "$*"
}
_info() {
_w "$*" >&2
}
_i() {
_info "$*"
}
_warn() {
_info "WARNING: $*" | _log "== FATAL ERROR =="
_log "WARNING: $*"
exit 4
}
_debug() {
_check_true "${DEBUG:-false}" && _info "$*"
}
_d() {
_debug "$*"
}
_log () {
if [[ $# -gt 0 ]]; then
printf "%s\n" "$@" | tee -a "${LOG_FILE:-${HOME}/wireguard-setup.log}" &> /dev/null
fi
_debug "$@"
if [[ ! -t 0 ]]; then
printf "%s\n" "$(< /dev/stdin)" | tee -a "${LOG_FILE:-${HOME}/wireguard-setup.log}"
fi
echo | tee -a "${LOG_FILE:-${HOME}/wireguard-setup.log}" &> /dev/null
}
_log_exec() {
echo "$*" | _log "Executing command" &> /dev/null
"$@" 2>&1 | _log "Command output"
echo "End of command execution" | _log &> /dev/null
}
_set() {
local -r var_name="${1:-}"
[[ -z "${var_name}" ]] && _warn "No variable name provided"
shift
_debug "Setting var '${var_name}' to the value(s) '$*'"
if [[ $# -gt 1 ]]; then
eval "${var_name}=(${*})"
else
eval "${var_name}=\"${1:-\"\"}\""
fi
}
_s() {
_set "$@"
}
_set_secret() {
local -r var_name="${1:-}"
[[ -z "${var_name}" ]] && _warn "No variable name provided"
shift
_debug "Setting var '${var_name}'"
if [[ $# -gt 1 ]]; then
eval "${var_name}=(${*})"
else
eval "${var_name}=${1:-}"
fi
}
_ss() {
_set_secret "$@"
}
_unset() {
[[ $# -eq 0 ]] && return
_d "Unsetting var(s) $(printf "%s " "$@")"
unset "$@"
}
_u() {
_unset "$@"
}
start_sudo() {
if ! has_sudo; then
command sudo -v -B
if has_sudo && [[ -z "${SUDO_PID:-}" ]]; then
(while true; do
command sudo -v
command sleep 30
done) &
SUDO_PID="$!"
builtin trap stop_sudo SIGINT SIGTERM
fi
fi
}
stop_sudo() {
builtin kill "$SUDO_PID" &> /dev/null
builtin trap - SIGINT SIGTERM
command sudo -k
}
has_sudo() {
command sudo -n -v &> /dev/null
}