aboutsummaryrefslogtreecommitdiff
path: root/bin/app-conf
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-01-26 23:58:22 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2013-01-26 23:58:22 +0100
commit110ffae47db27a49bbc43f86ba3737bccc1b3085 (patch)
treed74934b12e2904b8aff5fe87421a6501b8ea5c8e /bin/app-conf
parentca27d6f6d9ccc35bf55db3d360d1d464d5e206e7 (diff)
downloadapp.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.gz
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.bz2
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.tar.xz
app.sh-110ffae47db27a49bbc43f86ba3737bccc1b3085.zip
o Rewriting most of this stuff to make it feel more like git.
Diffstat (limited to 'bin/app-conf')
-rwxr-xr-xbin/app-conf135
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