-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.sh
More file actions
83 lines (72 loc) · 1.84 KB
/
common.sh
File metadata and controls
83 lines (72 loc) · 1.84 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
#!/usr/bin/env bash
# require_not_root ensures the script is not run as root or with sudo.
# unless running in a container
require_not_root() {
# skip this check if running in a container
if [[ "${IN_CONTAINER_BUILD:-0}" == "1" ]]; then
echo "🐳 Container build detected, skipping root check"
return 0
fi
local uid
uid="${EUID:-$(id -u)}"
if [[ "$uid" -eq 0 ]]; then
if [[ -n "${SUDO_USER:-}" ]]; then
cat >&2 <<EOF
❌ This script was run with sudo.
Re-run it without sudo:
${0##*/}
EOF
else
cat >&2 <<EOF
❌ This script is running as root.
Please run it as a normal user.
EOF
fi
exit 1
fi
}
# detect_arch detects the instruction set architecture (amd64, arm64, arm, 386)
# returns: amd64, arm64, arm, 386 or 1 if not detected
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo amd64 ;;
aarch64|arm64) echo arm64 ;;
armv7l|armv7*) echo arm ;;
i386|i686) echo 386 ;;
*) return 1 ;;
esac
}
# detect_os detects the operating system (linux, darwin, unknown)
# returns: linux, darwin or 1 if not known
detect_os() {
case "$(uname -s)" in
Linux*) echo linux ;;
Darwin*) echo darwin ;;
*) return 1 ;;
esac
}
# version_ge compares two semantic version strings
# returns 0 if $1 >= $2
version_ge() {
[ "$(printf '%s\n' "$2" "$1" | sort -V | head -n1)" = "$2" ]
}
# get path to yq
get_yq_path() {
if ! command -v yq &> /dev/null; then
OS="$(detect_os)" || { echo "❌ Unsupported OS"; exit 1; }
if [[ "$OS" == "darwin" ]]; then
# macOS path
echo "/usr/local/bin/yq"
else
# Linux path
echo "$HOME/.local/bin/yq"
fi
fi
}
# put yq on PATH in current shell
put_yq_on_path() {
# Add to PATH for current shell
if ! echo "$PATH" | grep -q "$(get_yq_path)"; then
export PATH="$(get_yq_path):$PATH"
fi
}