-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions_as_config.sh
executable file
·101 lines (79 loc) · 2.23 KB
/
functions_as_config.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
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
#!/usr/bin/env bash
### This would go in config_pre.inc.sh
config_groups=(
location
server
ssl
vhost
)
shopt -s expand_aliases
for group in "${config_groups[@]}"; do
alias "$group"="function config_set.$group"
# eval "function config_set.$group { config_set '$group' \"\$@\"; }"
done
### end of config_pre.inc.sh
# . config_pre.inc.sh
### this would leave us a nice clear looking config file, eg:
server {
port 8080; # trailing semi-colon is ignored but permitted
admin '"Local Admin" <admin@localhost>' # quote within quotes
name "$( cat /etc/hostname )" # use the shell, luke.
}
ssl {
protocols "TLSv1 TLSv1.1 TLSv1.2" # quote anything with spaces
ciphers "DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:!DES"
# '!' needs to be quoted?
}
location {
root "/opt/html"
index index.{htm,html} # brace expansion, can't quote
}
vhost {
name "www.vhost.com"
aka "vhost.com"
aka "*.vhost.com"
root "/opt/virtual/vhost"
}
# . config_post.inc.sh
### and the rest goes in config_post.inc.sh
function config_set {
local arg
printf "config_set "
printf "%q " "$@"
printf "\n"
printf -v arg[0] -- "--%s-%s" "$1" "$2"; shift 2
printf -v arg[1] "%q " "$@"
cmd_args+=( "${arg[@]}" )
}
function transform {
local group=$1; shift
while (( $# )); do
if [[ ${1:0:1} = ' ' ]]; then
printf ' config_set "%s" %s\n' "$group" "$1"
else
printf '%s\n' "$1"
fi
shift
done
}
for group in "${config_groups[@]}"; do
fn="config_set.$group"
if declare -F "$fn" >/dev/null 2>&1
then
contents=()
while IFS= read -r line; do contents+=( "$line" ); done < <( declare -f "$fn")
source <( transform "$group" "${contents[@]}" )
# declare -f "$fn"
$fn
fi
done
echo
echo "./rwasa ${cmd_args[@]}"
###
### RESULT:
### ./rwasa --location-root /opt/html --location-index index.htm index.html --server-port 8080 \
### --server-admin \"Local\ Admin\"\ \<admin@localhost\> --server-name '' \
### --ssl-protocols TLSv1\ TLSv1.1\ TLSv1.2 --ssl-ciphers \
### DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:\!DES --vhost-name www.vhost.com \
### --vhost-aka vhost.com --vhost-aka \*.vhost.com --vhost-root /opt/virtual/vhost
# vim: set ts=4 sts=0 sw=4 noet: