-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.sh
59 lines (50 loc) · 974 Bytes
/
common.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
#!/bin/bash
safe_tput()
{
if [[ -t 1 ]]; then
tput "$@"
fi
}
die() { safe_tput setaf 1; echo "fatal: $*"; safe_tput sgr0; exit 1; }
safe_cd()
{
cd "$1" || die "$1 not found"
}
cfg_count_user()
{
yq ".accounts | length" /srv/config.yml
}
cfg_get_account_user()
{
local index="$1"
local user
user=$(yq ".accounts[$index].user" /srv/config.yml)
if [[ "$user" =~ ^[a-z][-a-z0-9_]*$ ]]; then
echo "$user"
else
die "Illegal user '$user'"
fi
}
cfg_get_account_uid()
{
local index="$1"
local uid
uid=$(yq ".accounts[$index].uid" /srv/config.yml)
case $uid in
''|*[!0-9]*) die "Illegal uid '$uid'" ;;
*) echo "$uid";;
esac
}
cfg_get_account_keys()
{
local index="$1"
yq '.accounts['"$index"'].keys | join("\n")' /srv/config.yml
}
cfg_external_hostname()
{
yq '.external_hostname' /srv/config.yml
}
cfg_external_port()
{
yq '.external_port' /srv/config.yml
}