1 # Copyright 2012 Google Inc.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of Google Inc. nor the names of its contributors
14 # may be used to endorse or promote products derived from this software
15 # without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # Configuration file processing and queries.
35 # List of valid configuration variables.
37 # This is initialized by shtk_config_init and should remain unchanged thorough the
38 # execution of the program.
42 # List of overrides to apply by shtk_config_apply_overrides.
44 # The overrides are recorded and applied separately because they need to happen
45 # after a configuration file has been loaded. The contents of this list are
46 # words of the form: set:<variable> or unset:<variable>. For those variables
47 # listed in set:, there is a corresponding shtk_config_override_var_<variable>
48 # variable containing the new value.
49 _Shtk_ConfigOverrides=
52 # Initializes the configuration module.
54 # \param ... List of configuration variables to recognize in configuration files
57 _Shtk_ConfigVars="${@}"
61 # Checks if a configuration variable is known.
63 # \param var Name of the variable to check.
65 # \return True if the variable was registered by shtk_config_init, false otherwise.
66 shtk_config_is_valid() {
67 local var="${1}"; shift
70 for known_var in ${_Shtk_ConfigVars}; do
71 if [ "${known_var}" = "${var}" ]; then
79 # Checks if a configuration variable is defined.
81 # \param var The name of the variable to check.
83 # \return True if the variable is defined (even if empty), false otherwise.
85 local var="${1}"; shift
88 eval is_unset="\${shtk_config_var_${var}-yes_this_is_unset}"
89 if [ "${is_unset}" = yes_this_is_unset ]; then
97 # Gets the value of a defined configuration variable.
99 # \post The value of the variable is printed to stdout, if any.
101 # \post If the variable is not defined, this terminates execution with an error.
103 # \param var Name of the configuration variable to query.
105 local var="${1}"; shift
107 if shtk_config_has "${var}"; then
108 eval echo "\${shtk_config_var_${var}}"
110 shtk_cli_error "Required configuration variable ${var} not set"
115 # Gets the value of configuration variable interpreting it as a boolean.
117 # \param var The variable to query.
119 # \return True if the variable is set to a truth value, false if its value is
120 # false or if it is not defined.
121 shtk_config_get_bool() {
122 local var="${1}"; shift
124 if shtk_config_has "${var}"; then
125 case "$(shtk_config_get "${var}")" in
126 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee])
129 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee])
133 shtk_cli_error "Invalid boolean value in variable ${var}"
142 # Gets the value of a configuration variable with a default fallback.
144 # \post The value of the variable is printed to stdout, if any.
146 # \param var Name of the configuration variable to query.
147 # \param default Default value to return if the variable is not defined.
148 shtk_config_get_default() {
149 local var="${1}"; shift
150 local default="${1}"; shift
152 if shtk_config_has "${var}"; then
153 shtk_config_get "${var}"
160 # Sets a configuration variable.
162 # \post Execution terminates if the variable is not valid.
164 # \param var The name of the configuration variable to set.
165 # \param value The value to set.
167 local var="${1}"; shift
168 local value="${1}"; shift
170 shtk_config_is_valid "${var}" \
171 || shtk_cli_usage_error "Unknown configuration variable ${var}"
172 eval "shtk_config_var_${var}=\"\${value}\""
176 # Unsets a configuration variable.
178 # \param var The name of the configuration variable to unset.
179 shtk_config_unset() {
180 local var="${1}"; shift
182 shtk_config_is_valid "${var}" \
183 || shtk_cli_usage_error "Unknown configuration variable ${var}"
184 eval unset "shtk_config_var_${var}"
188 # Loads a configuration file.
190 # \pre shtk_config_init should have been called to register the valid configuration
191 # variables. Any non-registered configuration variable found in the file will
192 # not be available through any of the functions in this module.
194 # \post The configuration module is updated with the values defined in the
195 # configuration file.
197 # \post Any errors in the processing of the configuration file terminate the
198 # execution of the script.
200 # \param config_file Path to the file to load.
202 local config_file="${1}"; shift
204 [ -e "${config_file}" ] || shtk_cli_error "Configuration file" \
205 "${config_file} does not exist"
207 # User-facing variables.
209 for var in ${_Shtk_ConfigVars}; do
210 unset "${var}" || true
214 local real_config_file
215 case "${config_file}" in
216 */*) real_config_file="${config_file}" ;;
217 *) real_config_file="./${config_file}" ;;
219 ( . "${real_config_file}" ) || shtk_cli_error "Failed to load" \
220 "configuration file ${config_file}"
221 . "${real_config_file}"
223 for var in ${_Shtk_ConfigVars}; do
225 eval value="\${${var}-unset}"
226 [ "${value-unset}" != unset ] || continue
228 if [ -n "${value}" ]; then
229 shtk_config_set "${var}" "${value}"
231 unset "shtk_config_var_${var}" || true
235 shtk_config_apply_overrides
239 # Applies recorded overrides to the current configuration.
241 # This is just a helper function for shtk_config_load and should not be used
244 # \post The configuration data in memory is modified according to the data
245 # recorded in the overrides.
247 # \post The contents of _Shtk_ConfigOverrides is cleared.
248 shtk_config_apply_overrides() {
249 for override in ${_Shtk_ConfigOverrides}; do
250 case "${override}" in
252 local var="$(echo ${override} | cut -d : -f 2)"
254 eval value="\"\${shtk_config_override_var_${var}}\""
255 shtk_config_set "${var}" "${value}"
258 local var="$(echo ${override} | cut -d : -f 2)"
259 unset "shtk_config_var_${var}" || true
263 _Shtk_ConfigOverrides=
267 # Records an override to be applied to the configuration.
269 # Overrides are configuration variables set through the command line. These can
270 # be set before loading the configuration file with shtk_config_load.
272 # \post Any errors in the processing of the configuration override terminate the
273 # execution of the script.
275 # \param arg An override of the form variable=value.
276 shtk_config_override() {
277 local arg="${1}"; shift
283 shtk_cli_usage_error "Invalid configuration override" \
284 "${arg}; must be of the form variable=value"
287 local var="$(echo "${arg}" | cut -d = -f 1)"
288 local value="$(echo "${arg}" | cut -d = -f 2-)"
290 [ -n "${var}" ] || shtk_cli_usage_error "Invalid configuration override" \
291 "${arg}; must be of the form variable=value"
292 shtk_config_is_valid "${var}" \
293 || shtk_cli_usage_error "Unknown configuration variable ${var}"
295 if [ -n "${value}" ]; then
296 eval "shtk_config_override_var_${var}=\"${value}\""
297 _Shtk_ConfigOverrides="${_Shtk_ConfigOverrides} set:${var}"
299 _Shtk_ConfigOverrides="${_Shtk_ConfigOverrides} unset:${var}"