-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.subr
278 lines (227 loc) · 7.95 KB
/
config.subr
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright 2012 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
shtk_import bool
shtk_import cli
# List of valid configuration variables.
#
# This is initialized by shtk_config_init and should remain unchanged thorough
# the execution of the program.
_Shtk_ConfigVars=
# List of overrides to apply by shtk_config_apply_overrides.
#
# The overrides are recorded and applied separately because they need to happen
# after a configuration file has been loaded. The contents of this list are
# words of the form: set:<variable> or unset:<variable>. For those variables
# listed in set:, there is a corresponding shtk_config_override_var_<variable>
# variable containing the new value.
_Shtk_ConfigOverrides=
# Directory in which the currently-parsed configuration file lives.
#
# This is used by shtk_config_include to determine the path to included files
# when they are specified as relative paths. The value of this variable
# potentially changes with each call to shtk_config_include.
_Shtk_ConfigDirectory=
# Applies recorded overrides to the current configuration.
#
# This is just a helper function for shtk_config_load and should not be used
# directly.
#
# The configuration data in memory is modified according to the data
# recorded in the overrides.
#
# The contents of _Shtk_ConfigOverrides is cleared.
_shtk_config_apply_overrides() {
for override in ${_Shtk_ConfigOverrides}; do
case "${override}" in
set:*)
local var="$(echo ${override} | cut -d : -f 2)"
local value
eval value="\"\${shtk_config_override_var_${var}}\""
shtk_config_set "${var}" "${value}"
;;
unset:*)
local var="$(echo ${override} | cut -d : -f 2)"
unset "shtk_config_var_${var}" || true
;;
esac
done
_Shtk_ConfigOverrides=
}
# Checks if a configuration variable is known.
#
# Returns true if the variable was registered by shtk_config_init, false
# otherwise.
_shtk_config_is_valid() {
local var="${1}"; shift
local known_var
for known_var in ${_Shtk_ConfigVars}; do
if [ "${known_var}" = "${var}" ]; then
return 0
fi
done
return 1
}
shtk_config_init() {
_Shtk_ConfigVars="${@}"
}
shtk_config_has() {
local var="${1}"; shift
local is_unset
eval is_unset="\${shtk_config_var_${var}-yes_this_is_unset}"
if [ "${is_unset}" = yes_this_is_unset ]; then
return 1
else
return 0
fi
}
shtk_config_get() {
local var="${1}"; shift
if shtk_config_has "${var}"; then
eval echo "\${shtk_config_var_${var}}"
else
shtk_cli_error "Required configuration variable ${var} not set"
fi
}
shtk_config_get_bool() {
local var="${1}"; shift
if shtk_config_has "${var}"; then
shtk_bool_check "$(shtk_config_get "${var}")" \
"Invalid boolean value in variable ${var}" || return 1
return 0
else
return 1
fi
}
shtk_config_get_default() {
local var="${1}"; shift
local default="${1}"; shift
if shtk_config_has "${var}"; then
shtk_config_get "${var}"
else
echo "${default}"
fi
}
shtk_config_set() {
local var="${1}"; shift
local value="${1}"; shift
_shtk_config_is_valid "${var}" \
|| shtk_cli_usage_error "Unknown configuration variable ${var}"
eval "shtk_config_var_${var}=\"\${value}\""
}
shtk_config_unset() {
local var="${1}"; shift
_shtk_config_is_valid "${var}" \
|| shtk_cli_usage_error "Unknown configuration variable ${var}"
eval unset "shtk_config_var_${var}"
}
shtk_config_load() {
local config_file="${1}"; shift
[ -e "${config_file}" ] || shtk_cli_error "Configuration file" \
"${config_file} does not exist"
# Ensure all configuration variables are unset from the point of view of the
# user. Any defaults that the caller has set are not clobbered by this.
local var
for var in ${_Shtk_ConfigVars}; do
eval local "${var}"
unset "${var}" || true
done
local real_config_file
case "${config_file}" in
*/*) real_config_file="${config_file}" ;;
*) real_config_file="./${config_file}" ;;
esac
local config_file_dirname="$(dirname "${real_config_file}")"
_Shtk_ConfigDirectory="${config_file_dirname}"
( . "${real_config_file}" ) || shtk_cli_error "Failed to load" \
"configuration file ${config_file}"
. "${real_config_file}"
_Shtk_ConfigDirectory=
for var in ${_Shtk_ConfigVars}; do
local value
eval value="\${${var}-unset}"
[ "${value-unset}" != unset ] || continue
if [ -n "${value}" ]; then
shtk_config_set "${var}" "${value}"
else
unset "shtk_config_var_${var}" || true
fi
done
_shtk_config_apply_overrides
}
shtk_config_include() {
local file="${1}"; shift
local file_dirname="$(dirname "${file}")"
local old_configdirectory="${_Shtk_ConfigDirectory}"
case "${file_dirname}" in
/*)
_Shtk_ConfigDirectory="${file_dirname}"
. "${file}"
;;
*)
_Shtk_ConfigDirectory="${_Shtk_ConfigDirectory}/${file_dirname}"
. "${old_configdirectory}/${file}"
;;
esac
_Shtk_ConfigDirectory="${old_configdirectory}"
}
shtk_config_override() {
local arg="${1}"; shift
case "${arg}" in
*=*)
;;
*)
shtk_cli_usage_error "Invalid configuration override" \
"${arg}; must be of the form variable=value"
;;
esac
local var="$(echo "${arg}" | cut -d = -f 1)"
local value="$(echo "${arg}" | cut -d = -f 2-)"
[ -n "${var}" ] || shtk_cli_usage_error "Invalid configuration override" \
"${arg}; must be of the form variable=value"
_shtk_config_is_valid "${var}" \
|| shtk_cli_usage_error "Unknown configuration variable ${var}"
if [ -n "${value}" ]; then
eval "shtk_config_override_var_${var}=\"${value}\""
_Shtk_ConfigOverrides="${_Shtk_ConfigOverrides} set:${var}"
else
_Shtk_ConfigOverrides="${_Shtk_ConfigOverrides} unset:${var}"
fi
}
shtk_config_run_hook() {
local hook="${1}"; shift
(
for var in ${_Shtk_ConfigVars}; do
if shtk_config_has "${var}"; then
eval "${var}"=\"$(shtk_config_get "${var}")\"
else
unset "${var}"
fi
done
"${hook}" "${@}"
) || shtk_cli_error "The hook ${hook} returned an error"
}