diff options
| author | Martin Schwenke <martin@meltin.net> | 2008-08-15 15:34:22 +1000 |
|---|---|---|
| committer | Martin Schwenke <martin@meltin.net> | 2008-08-15 15:34:22 +1000 |
| commit | f334e8aa5dc18af9ce7d94a2e35f68fe583fc0ca (patch) | |
| tree | 821bd15dc4d521b7fa82cfe26500333ac9ed6173 /functions | |
| parent | 9cadd75c0a109c3c283bd2144f0305c11f00c5e2 (diff) | |
Configuration generalised so that all options can be set via
environment variables, in configuration files or on the command-line.
Changed format of config.default to use defconf rather than
assignment, and include documentation for usage message. All
configuration variables now have an equivalent command-line option.
For example, $NBD_DEVICE can be set on the command-line using the
--nbd-device option. Options specified on the command-line override
options set in any other way. New configuration option $WITH_TSM_NODE
(defaults to 1). Removed -n option - this is implicitly replaced by
--with-tsm-node=0. The -c option can now be specified multiple times
and interspersed with setting individual options - order is important.
New option --dump displays the current configuration settings and then
exits. It can be inserted anywhere on the command-line and takes
immediate effect, so it should be at the end of the command-line to
display all specified settings. Configuration settings can also be
set in autocluster's environment, where they override the settings in
config.default (but not those specified on the command-line, including
via -c). Error trapping moved to after usage processing, so an
invalid command-line option causes the desired usage message to
appear.
Signed-off-by: Martin Schwenke <martin@meltin.net>
Diffstat (limited to 'functions')
| -rw-r--r-- | functions | 160 |
1 files changed, 159 insertions, 1 deletions
@@ -1,4 +1,4 @@ -# common functions for autocluster +# common functions for autocluster (-*- shell-script -*-) # create a MAC address based on a hash of the cluster name # plus the adapter and node number @@ -198,3 +198,161 @@ check_command() { fi } +# Set a variable if it isn't already set. This allows environment +# variables to override default config settings. +defconf() { + local v="$1" + local e="$2" + + [ "${!v+x}" ] || eval "$v=\"$e\"" +} + +# Print the list of config variables defined in config.default. +get_config_options() {( # sub-shell for local declaration of defconf() + local options= + defconf() { options="$options $1" ; } + . "$installdir/config.default" + echo $options +)} + +# Produce a list of long options, suitable for use with getopt, that +# represent the config variables defined in config.default. +getopt_config_options() { + local x=$(get_config_options | tr 'A-Z_' 'a-z-') + echo "${x// /:,}:" +} + +# Unconditionally set the config variable associated with the given +# long option. +setconf_longopt() { + local longopt="$1" + local e="$2" + + local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_') + # unset so defconf will set it + eval "unset $v" + defconf "$v" "$e" +} + +# Dump all of the current config variables. +dump_config() { + local o + for o in $(get_config_options) ; do + echo "${o}=\"${!o}\"" + done + exit 0 +} + +# Print text assuming it starts after other text in $startcol and +# needs to wrap before $fillcol. Subsequent lines start at $startcol. +# Long "words" will extend past $fillcol. +fill_text() { + local startcol="$1" + local fillcol="$2" + local text="$3" + + local width=$(($fillcol - $startcol)) + [ $width -lt 0 ] && width=$((78 - $startcol)) + + local out="" + + local padding=$(printf "\n%${startcol}s" " ") + + while [ -n "$text" ] ; do + local orig="$text" + + # If we already have output then arrange padding on the next line. + [ -n "$out" ] && out="${out}${padding}" + + # Break the text at $width. + out="${out}${text:0:${width}}" + text="${text:${width}}" + + # If we have left over text then the line break may be ugly, + # so let's check and try to break it on a space. + if [ -n "$text" ] ; then + if [ "${text:0:1}" != " " -a "${text: -1:1}" != " " ] ; then + # We didn't break on a space. Arrange for the + # beginning of the broken "word" to appear on the next + # line but not if it will make us loop infinitely. + if [ "${orig}" != "${out##* }${text}" ] ; then + text="${out##* }${text}" + out="${out% *}" + else + # Hmmm, doing that would make us loop, so add the + # rest of the word from the remainder of the text + # to this line and let it extend past $fillcol. + out="${out}${text%% *}" + if [ "${text# *}" != "$text" ] ; then + # Remember the text after the next space for next time. + text="${text# *}" + else + # No text after next space. + text="" + fi + fi + else + # We broke on a space. If it will be at the beginning + # of the next line then remove it. + text="${text# }" + fi + fi + done + + echo "$out" +} + +# Display usage text, trying these approaches in order. +# 1. See if it all fits on one line before $fillcol. +# 2. See if splitting before the default value and indenting it +# to $startcol means that nothing passes $fillcol. +# 3. Treat the message and default value as a string and just us fill_text() +# to format it. +usage_display_text() { + local startcol="$1" + local fillcol="$2" + local desc="$3" + local default="$4" + + local width=$(($fillcol - $startcol)) + + # To avoid clutter, only quote values that need it. + #local q= + #[ -z "$default" -o "$(echo $default)" = "${default// /}" ] || q="\"" + default="(default \"$default\")" + + if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then + echo "${desc} ${default}" + else + local padding=$(printf "%${startcol}s" " ") + + if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then + echo "$desc" + echo "${padding}${default}" + else + fill_text $startcol $fillcol "${desc} ${default}" + fi + fi +} + +# Display usage information for long config options. +usage_config_options(){( # sub-shell for local declaration of defconf() + local def_fillcol=78 + local fillcol=$def_fillcol + local rows=$(stty size | sed -e 's@.* @@') + [ -n "$rows" ] && fillcol=$(($rows - 2)) + + local startcol=33 + + # We need to have at least one column... negative is also a problem. + [ $fillcol -le $startcol ] && fillcol=$def_fillcol + + defconf() { + local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-') + + printf " --%-25s " "${longopt}=${3}" >&2 + + usage_display_text $startcol $fillcol "$4" "$2" >&2 + } + . "$installdir/config.default" +)} |
