aboutsummaryrefslogtreecommitdiff
path: root/.app/lib/app-conf
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-10-07 13:11:45 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-10-07 13:11:45 +0200
commit1c299756deb763ae30223bc74ed714998437e99e (patch)
tree18c9da6d9ec2aebea70c3017a25565b5d956099a /.app/lib/app-conf
parentbe42cc56daadba8dd32418c6c4fb064e5627ac7a (diff)
downloadapp.sh-1c299756deb763ae30223bc74ed714998437e99e.tar.gz
app.sh-1c299756deb763ae30223bc74ed714998437e99e.tar.bz2
app.sh-1c299756deb763ae30223bc74ed714998437e99e.tar.xz
app.sh-1c299756deb763ae30223bc74ed714998437e99e.zip
o Adding 'conf' method to list, set and delete configuration values.
Diffstat (limited to '.app/lib/app-conf')
-rw-r--r--.app/lib/app-conf138
1 files changed, 138 insertions, 0 deletions
diff --git a/.app/lib/app-conf b/.app/lib/app-conf
new file mode 100644
index 0000000..8cc4949
--- /dev/null
+++ b/.app/lib/app-conf
@@ -0,0 +1,138 @@
+#!/bin/bash
+
+key_expr="[a-zA-Z][_a-zA-Z0-9]*\.[a-zA-Z][_a-zA-Z0-9]*"
+
+get_conf() {
+ key=$1
+ default=$2
+
+ file=$BASEDIR/$name/$instance/latest/etc/app.conf
+
+ if [ ! -r $file ]
+ then
+ echo "$default"
+ return 0
+ fi
+
+ value=`sed -n "s,^${key}[ ]*=[ ]*\(.*\)$,\1,p" $file`
+
+ if [ -z "$value" ]
+ then
+ echo "$default"
+ fi
+
+ echo "$value"
+}
+
+get_conf_all() {
+ local file=$BASEDIR/$name/$instance/latest/etc/app.conf
+
+ if [ ! -r $file ]
+ then
+ return 0
+ fi
+
+ sed -n "s,^[ ]*\($key_expr\)[ ]*=[ ]*\(.*\)$,\1=\2,p" $file | sort
+}
+
+get_conf_in_group() {
+ prefix=$1
+
+ get_conf_all | sed -n "s,^[ ]*${prefix}\.\([._a-zA-Z]*\)=\(.*\)$,\1=\2,p"
+}
+
+assert_key() {
+ key=$1
+
+ local x=`echo $key | sed -n "/^$key_expr$/p"`
+ if [ -z "$x" ]
+ then
+ echo "Invalid key: $key" >&2
+ exit 1
+ fi
+}
+
+conf_set() {
+ key=$1
+ value=$2
+
+ local file=$BASEDIR/$name/$instance/latest/etc/app.conf
+
+ assert_key "$key"
+
+ sed "/^$key[ ]*=.*/d" $file > $file.tmp
+ echo "$key=$value" >> $file.tmp
+ mv $file.tmp $file
+}
+
+conf_delete() {
+ key=$1
+
+ local file=$BASEDIR/$name/$instance/latest/etc/app.conf
+
+ assert_key "$key"
+
+ sed "/^$key[ ]*=.*/d" $file > $file.tmp
+ mv $file.tmp $file
+}
+
+conf_usage() {
+ if [ -n "$1" ]
+ then
+ echo "Error:" $@ >&2
+ fi
+
+ echo "usage:" >&2
+ echo ""
+ echo " $0 conf -n name -i instance" >&2
+ echo " $0 conf -n name -i instance -s [group.key] [value]" >&2
+ exit 1
+}
+
+method_conf() {
+ local mode="list"
+
+ while getopts "n:i:s:d:" opt
+ do
+ case $opt in
+ n)
+ name=$OPTARG
+ ;;
+ i)
+ instance=$OPTARG
+ ;;
+ d)
+ mode="delete"
+ key=$OPTARG
+ ;;
+ s)
+ mode="set"
+ key=$OPTARG
+ # Remove all options so far
+ shift $((OPTIND-1))
+ value=$1
+ ;;
+ \?)
+ conf_usage "Invalid option: -$OPTARG"
+ ;;
+ esac
+ done
+
+ assert_is_instance conf_usage "$name" "$instance"
+
+ case $mode in
+ list)
+ get_conf_all | (IFS==; while read key value
+ do
+ printf "%-20s %-20s" "$key" "$value"
+ echo
+ done)
+ ;;
+ set)
+ conf_set "$key" "$value"
+ ;;
+ delete)
+ conf_delete "$key"
+ ;;
+ esac
+}