-
Notifications
You must be signed in to change notification settings - Fork 0
/
chelper
executable file
·85 lines (78 loc) · 2.09 KB
/
chelper
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
#!/usr/bin/env bash
#
# Caddy Helper Script
#
# Homepage: https://github.com/niklas-heer/chelper
# Issues: https://github.com/niklas-heer/chelper/issues
# Requires: bash, curl or wget and systemd
#
# Hi! This is a small script that helps you with common tasks with caddy.
# Use it like this:
#
CHELPER_VERSION="1.0.0"
CHELPER_CONFIG_PATH="config-example.yml"
#########################
# The command line help #
#########################
chelper_display_help() {
echo "Usage: chelper [option...]" >&2
echo
echo " -h, help Displays this help page."
echo " -u, update Updates caddy with all the wanted plugins."
echo " -f, fix Fixes systemd related issue with caddy."
echo
}
# Source: https://gist.github.com/epiloque/8cf512c6d64641bde388
parse_yaml() {
local prefix=$2
local s
local w
local fs
s='[[:space:]]*'
w='[a-zA-Z0-9_]*'
fs="$(echo @|tr @ '\034')"
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, $3);
}
}' | sed 's/_=/+=/g'
}
# get config
eval $(parse_yaml $CHELPER_CONFIG_PATH "config_")
CHELPER_CADDY_PLUGINS="$(printf '%s,' ${config_chelper_plugins[@]} | sed 's/.$//')"
chelper_fix() {
# fix owner
chown $config_chelper_user:$config_chelper_user $config_chelper_bin
# fix systemd
sudo setcap cap_net_bind_service=+ep $config_chelper_bin &>/dev/null
}
##################
# Helper command #
##################
case "$1" in
-h | help)
chelper_display_help
;;
-u | update)
curl https://getcaddy.com | bash -s $CHELPER_CADDY_PLUGINS
chelper_fix
;;
-f | fix)
chelper_fix
;;
-v | --version)
echo $CHELPER_VERSION
;;
-*)
echo "Error: Unknown option: $1" >&2
;;
*) # No more options
chelper_display_help
;;
esac