-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.sh
More file actions
60 lines (52 loc) · 1.44 KB
/
Copy pathutils.sh
File metadata and controls
60 lines (52 loc) · 1.44 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
#!/usr/bin/env bash
UTILS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
PURPLE="\033[0;35m"
CYAN="\033[1;36m"
RESET="\033[0m"
NC="$RESET"
utils::refuse_root_user_install() {
local install_mode="${1:-user}"
if [[ "$install_mode" == "--system" ]]; then
install_mode="system"
fi
if [[ "$install_mode" != "system" ]] && [[ "$EUID" -eq 0 ]]; then
echo -e "${RED}[-] Refusing to run user install as root. Re-run without sudo, or use --system.${RESET}" >&2
exit 1
fi
}
utils::resolve_target_user() {
if [[ "$EUID" -eq 0 ]]; then
TARGET_USER="${SUDO_USER:-}"
if [[ -z "$TARGET_USER" ]]; then
return 1
fi
TARGET_HOME="$(getent passwd "$TARGET_USER" | cut -d: -f6)"
if [[ -z "$TARGET_HOME" ]]; then
TARGET_HOME="/home/$TARGET_USER"
fi
AS_ROOT=true
else
TARGET_USER="$USER"
TARGET_HOME="$HOME"
AS_ROOT=false
fi
}
utils::run_as_target() {
local command="$1"
if [[ "${AS_ROOT:-false}" == true ]]; then
sudo -u "$TARGET_USER" env HOME="$TARGET_HOME" bash -lc "$command"
else
HOME="$TARGET_HOME" bash -lc "$command"
fi
}
utils::exec_as_target() {
if [[ "${AS_ROOT:-false}" == true ]]; then
sudo -u "$TARGET_USER" env HOME="$TARGET_HOME" "$@"
else
HOME="$TARGET_HOME" "$@"
fi
}