diff options
Diffstat (limited to 'bin/app-conf')
-rwxr-xr-x | bin/app-conf | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/bin/app-conf b/bin/app-conf new file mode 100755 index 0000000..7193c20 --- /dev/null +++ b/bin/app-conf @@ -0,0 +1,135 @@ +#!/bin/bash + +# TODO: Add a 'get' command that returns a single value +# Exit with 0 if found, 1 otherwise. + +if [[ $APPSH_HOME == "" ]] +then + APPSH_HOME=`dirname "$0"` + APPSH_HOME=`cd "$APPSH_HOME/.." && pwd` +fi + +. $APPSH_HOME/lib/common + +PATH=$APPSH_HOME/libexec:$PATH + +key_expr="[a-zA-Z][_a-zA-Z0-9]*" + +format_conf() { + local IFS== + while read key value + do + printf "%-20s %-20s" "$key" "$value" + echo + done +} + +assert_valid_config_name() { + local name=$1 + + local x=`echo $name | sed -n "/^$key_expr\\.$key_expr$/p"` + if [ -z "$x" ] + then + echo "Invalid name: $name" >&2 + exit 1 + fi +} + +conf_set() { + local name=$1; shift + local value=$1; shift + + assert_valid_config_name "$name" + + if [ -r $file ] + then + sed "/^$name[ ]*=.*/d" $file > $file.tmp + fi + + echo "$name=$value" >> $file.tmp + mv $file.tmp $file +} + +conf_delete() { + local name=$1; shift + + assert_valid_config_name "$name" + + sed "/^$name[ ]*=.*/d" $file > $file.tmp + mv $file.tmp $file +} + +usage() { + if [ -n "$1" ] + then + echo "Error: $@" >&2 + fi + + echo "usage: $0 conf <command>" >&2 + echo "" + echo "Available commands:" >&2 + echo " list - list all config values" >&2 + echo " set [name] [value] - set a config parameter" >&2 + echo " delete [name] - deletes a config parameter" >&2 + exit 1 +} + +if [ $# -gt 0 ] +then + command=$1 + shift +else + command=list +fi + +file=".app/config" + +assert_is_app -C + +case "$command" in + get) + if [ $# != 1 ] + then + usage + exit 1 + fi + + app-cat-conf -f "$file" -n "$1" | cut -f 2 -d = | format_conf | sed "s, *$,," + ;; + list) + if [ $# -gt 0 ] + then + usage "Extra options." + exit 1 + fi + + app-cat-conf -f "$file" | format_conf + ;; + set) + if [ $# -ne 2 ] + then + usage + exit 1 + fi + + conf_set "$1" "$2" + ;; + delete) + if [ $# -ne 1 ] + then + usage "Missing [name] argument." + exit 1 + fi + + conf_delete "$1" "$2" + ;; + *) + if [ -z "$command" ] + then + usage + else + usage "Unknown command: $command" + fi + exit 1 + ;; +esac |