aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-01-27 19:41:28 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2013-01-27 19:49:58 +0100
commit1e4a96730da70fcfa3b8c153874cbdebad0f9829 (patch)
tree9495f33f6b54621e0a684b553b64a6958744c1e3
parente1daac32c5b7ca0d902c16135d361aa5303f5124 (diff)
downloadapp.sh-1e4a96730da70fcfa3b8c153874cbdebad0f9829.tar.gz
app.sh-1e4a96730da70fcfa3b8c153874cbdebad0f9829.tar.bz2
app.sh-1e4a96730da70fcfa3b8c153874cbdebad0f9829.tar.xz
app.sh-1e4a96730da70fcfa3b8c153874cbdebad0f9829.zip
o Starting on a style guide.
app-conf: Adding 'import' command. app-cat-conf: Adding support for multiple -f flags. The default files can be switched off. A file named "-" is the same as /dev/stdin. app: Adding a way to enable debugging. app-install-file: Import any configuration delivered with the package.
-rw-r--r--STYLE-GUIDE.md10
-rwxr-xr-xapp7
-rwxr-xr-xbin/app-conf29
-rw-r--r--lib/common11
-rwxr-xr-xlibexec/app-cat-conf39
-rwxr-xr-xlibexec/app-install-file9
-rwxr-xr-xlibexec/app-operate25
-rwxr-xr-xlibexec/app-operator-pid (renamed from libexec/app-method-pid)8
-rwxr-xr-xlibexec/app-run-hook2
-rwxr-xr-xtest/app-cat-conf.bats7
-rwxr-xr-xtest/app-conf.bats19
-rwxr-xr-xtest/app-init.bats5
-rw-r--r--test/data/app-a/app.config (renamed from test/data/app-a/root/etc/app.conf)0
13 files changed, 117 insertions, 54 deletions
diff --git a/STYLE-GUIDE.md b/STYLE-GUIDE.md
index 21f1523..1137425 100644
--- a/STYLE-GUIDE.md
+++ b/STYLE-GUIDE.md
@@ -1,9 +1,14 @@
Style Guide
-----------
-Usage
+Basic
=====
+* Indent: two spaces. Spaces >> tabs.
+
+Creating `usage()`
+==================
+
* Always echo to `stderr`.
* Exit with code 1.
* Enclose required arguments in angle brackets: `-v <version>`
@@ -14,4 +19,7 @@ Usage
exit 1
}
+Resources
+---------
+* Parameter expansion: <http://wiki.bash-hackers.org/syntax/pe>
diff --git a/app b/app
index a70b026..8fcc585 100755
--- a/app
+++ b/app
@@ -14,6 +14,7 @@ usage() {
echo " operate" >&2
echo "" >&2
echo "Run $0 -h <group> for more help" >&2
+ exit 1
}
PRG="$0"
@@ -32,16 +33,18 @@ APPSH_HOME=`cd "$APPSH_HOME" && pwd`
. $APPSH_HOME/lib/common
+echo_debug=no
while getopts "h" opt
do
case $opt in
h)
usage
- exit 1
+ ;;
+ D)
+ echo_debug=yes
;;
\?)
usage
- exit 1
;;
esac
done
diff --git a/bin/app-conf b/bin/app-conf
index 3602a39..d281740 100755
--- a/bin/app-conf
+++ b/bin/app-conf
@@ -61,8 +61,16 @@ conf_delete() {
mv $file.tmp $file
}
+conf_import() {
+ local dst=$1; shift
+ local src=$1; shift
+
+ app-cat-conf -f "$src" -f "$dst" > "$dst.tmp"
+ mv "$dst.tmp" "$dst"
+}
+
usage() {
- if [ -n "$1" ]
+ if [ $# -gt 0 ]
then
echo "Error: $@" >&2
fi
@@ -70,12 +78,16 @@ usage() {
echo "usage: $0 conf <command>" >&2
echo ""
echo "Available commands:" >&2
+ echo " get [name] - returns a single value" >&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
+ echo " import [file] - import a file" >&2
exit 1
}
+file=".app/config"
+
if [ $# -gt 0 ]
then
command=$1
@@ -84,8 +96,6 @@ else
command=list
fi
-file=".app/config"
-
assert_is_app -C
case "$command" in
@@ -93,7 +103,6 @@ case "$command" in
if [ $# != 1 ]
then
usage
- exit 1
fi
app-cat-conf -f "$file" -n "$1" | cut -f 2 -d = | format_conf | sed "s, *$,,"
@@ -102,7 +111,6 @@ case "$command" in
if [ $# -gt 0 ]
then
usage "Extra options."
- exit 1
fi
app-cat-conf -f "$file" | format_conf
@@ -111,7 +119,6 @@ case "$command" in
if [ $# -ne 2 ]
then
usage
- exit 1
fi
conf_set "$1" "$2"
@@ -120,11 +127,19 @@ case "$command" in
if [ $# -ne 1 ]
then
usage "Missing [name] argument."
- exit 1
fi
conf_delete "$1"
;;
+ import)
+ if [ $# -ne 1 ]
+ then
+ usage "Missing [file] argument."
+ exit 1
+ fi
+
+ conf_import "$file" "$1"
+ ;;
*)
if [ -z "$command" ]
then
diff --git a/lib/common b/lib/common
index 8c33209..d53f0cf 100644
--- a/lib/common
+++ b/lib/common
@@ -39,6 +39,14 @@ assert_is_app() {
fi
}
+debug() {
+ [[ $echo_debug == no ]] || echo "D: $usage_app: $@" 2>&1
+}
+
+info() {
+ echo "I: $usage_app: $@" 2>&1
+}
+
fatal() {
echo "$usage_app: fatal: $@" 2>&1
exit 1
@@ -192,4 +200,5 @@ run_app() {
PATH=$PATH:$APPSH_HOME/bin:$APPSH_HOME/libexec
# Save for later
-usage_app=$0
+usage_app=${0##*/}
+echo_debug=${echo_debug-no}
diff --git a/libexec/app-cat-conf b/libexec/app-cat-conf
index 5b1c614..857ecaf 100755
--- a/libexec/app-cat-conf
+++ b/libexec/app-cat-conf
@@ -10,14 +10,23 @@ APPSH_HOME=$(cd $(dirname "$0")/.. && pwd)
key_expr="[a-zA-Z][_a-zA-Z0-9]*"
-file=.app/config
+files=()
name=""
+use_default_files=yes
-while getopts "f:n:" opt
+while getopts "f:Dn:" opt
do
case $opt in
f)
file=$OPTARG
+ if [[ $file == "-" ]]
+ then
+ file=/dev/stdin
+ fi
+ files+=($file)
+ ;;
+ D)
+ use_default_files=no
;;
n)
name=$OPTARG
@@ -29,6 +38,19 @@ do
esac
done
+if [[ $use_default_files == yes ]]
+then
+ if [ -r ".app/config" ]
+ then
+ files+=(".app/config")
+ fi
+
+ files+=(${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config})
+fi
+
+# TODO: find config files in the paths above $file's paths and perhaps
+# /etc/appsh/config.
+
if [ -z "$name" ]
then
filter="s,^[ ]*\($key_expr\.$key_expr\)[ ]*=[ ]*\(.*\)$,\1=\2,p"
@@ -36,17 +58,10 @@ else
filter="s,^\($name\)=\(.*\),\1=\2,p"
fi
-APPSH_DEFAULT_CONFIG=${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config}
-
-# TODO: find config files in the paths above $file's paths and perhaps /etc/appsh/config
-
-if [[ ! -r $file ]]
-then
- fatal "No such file: $file"
-fi
+debug "Using files:" "${files[@]}"
-# The awk script makes sure each key only appears once
-cat "$file" "$APPSH_DEFAULT_CONFIG" | \
+# The awk script makes sure each key only appears once. The first one wins
+cat "${files[@]}" | \
sed -n -e "$filter" | \
awk -F = ' (!($1 in a)){a[$1]; print }' | \
sort
diff --git a/libexec/app-install-file b/libexec/app-install-file
index 1e3edb8..c08a632 100755
--- a/libexec/app-install-file
+++ b/libexec/app-install-file
@@ -54,13 +54,10 @@ then
exit 1
fi
-# TODO: This should go away
-if [ -d current/bin ]
+if [ -r versions/$version/app.config ]
then
- (
- cd $name/$instance/current
- find bin -type f | xargs chmod +x
- )
+ debug "Importing config from package"
+ app-conf import versions/$version/app.config
fi
app-run-hook -v "$version" -h pre-install
diff --git a/libexec/app-operate b/libexec/app-operate
index 007948c..452f658 100755
--- a/libexec/app-operate
+++ b/libexec/app-operate
@@ -10,29 +10,16 @@ APPSH_HOME=$(cd $(dirname "$0")/.. && pwd)
assert_is_app
-method="$1"
+method="$1"; shift
+set -x
-bin=`app-conf get app.method`
-bin=${bin-$APPSH_HOME/.app/libexec/app-method-pid}
+bin=`app-conf get app.operator`
+bin=${bin:-$APPSH_HOME/libexec/app-operator-pid}
-if [ ! -x "current/$bin" ]
+if [ ! -x "$bin" ]
then
echo "Invalid executable: $bin" >&2
exit 1
fi
-case "$method" in
- start) run_app "$name" "$instance" "$bin" "start" "$@" ;;
- stop) run_app "$name" "$instance" "$bin" "stop" "$@" ;;
- status) run_app "$name" "$instance" "$bin" "status" "$@" ;;
- restart) run_app "$name" "$instance" "$bin" "restart" "$@" ;;
- run) run_app "$name" "$instance" "$bin" "run" "$@" ;;
- *)
- if [ -z "$method" ]
- then
- method_operate_usage
- else
- method_operate_usage "Unknown method $method"
- fi
- ;;
-esac
+run_app "$bin" "$@"
diff --git a/libexec/app-method-pid b/libexec/app-operator-pid
index 29f6b4f..a0ff097 100755
--- a/libexec/app-method-pid
+++ b/libexec/app-operator-pid
@@ -1,8 +1,14 @@
#!/bin/bash -e
+#!/bin/bash
+
+set -e
set -u
-. $APPSH_HOME/.app/lib/app-conf
+APPSH_HOME=$(cd $(dirname "$0")/.. && pwd)
+
+. $APPSH_HOME/lib/common
+# HEADER END
pid_file=$APPSH_APPS/.app/var/pid/$APPSH_NAME-$APPSH_INSTANCE.pid
bin=`get_conf $APPSH_APPS $APPSH_NAME $APPSH_INSTANCE app.bin`
diff --git a/libexec/app-run-hook b/libexec/app-run-hook
index ec96c58..6f1eab5 100755
--- a/libexec/app-run-hook
+++ b/libexec/app-run-hook
@@ -45,5 +45,5 @@ fi
# TODO: remove after
#chmod +x $bin
-echo "Running hook: $hook"
+debug "Running hook: $hook"
run_app -v $version hooks/$hook
diff --git a/test/app-cat-conf.bats b/test/app-cat-conf.bats
index a95049a..8060cea 100755
--- a/test/app-cat-conf.bats
+++ b/test/app-cat-conf.bats
@@ -41,6 +41,13 @@ setup_inner() {
eq '${#lines[*]}' 1
}
+@test "app-cat-conf can use stdin and multiple files" {
+ x=$(cat $APPSH_HOME/test/data/app-cat-conf/config-3 | \
+ $APPSH_HOME/libexec/app-cat-conf -D -f $APPSH_HOME/test/data/app-cat-conf/config-2 -f -)
+ [[ $x == "foo.bar=wat
+foo.wat=bar" ]]
+}
+
@test "uses \$APPSH_DEFAULT_CONFIG" {
APPSH_DEFAULT_CONFIG=$APPSH_HOME/test/data/app-cat-conf/config-2
app_libexec app-cat-conf -f /dev/null
diff --git a/test/app-conf.bats b/test/app-conf.bats
index 08676d2..f85dbab 100755
--- a/test/app-conf.bats
+++ b/test/app-conf.bats
@@ -4,7 +4,8 @@
load utils
setup_inner() {
- mkdir .app; touch .app/config
+ mkdir .app;
+ echo > .app/config
export APPSH_DEFAULT_CONFIG=/dev/null
}
@@ -116,4 +117,20 @@ setup_inner() {
app conf list; echo_lines
eq '$status' 0
eq '${lines[0]}' "foo.bar awesome "
+ eq '${#lines[*]}' 1
+}
+
+@test "./app conf import" {
+ echo "foo.bar=1" > .app/config
+ echo "foo.baz=1" > config-b
+ echo "foo.bar=2" >> config-b
+
+ app conf import config-b; echo_lines
+ eq '$status' 0
+ eq '${#lines[*]}' 0
+
+ app_libexec app-cat-conf
+ eq '${lines[0]}' "foo.bar=2"
+ eq '${lines[1]}' "foo.baz=1"
+ eq '${#lines[*]}' 2
}
diff --git a/test/app-init.bats b/test/app-init.bats
index 4e7c281..10edc63 100755
--- a/test/app-init.bats
+++ b/test/app-init.bats
@@ -37,9 +37,8 @@ load utils
match '${lines[2]}' "Downloading org.example:app-a:1.0-.*"
eq '${lines[3]}' "Unpacking..."
match '${lines[4]}' "Creating current symlink for version 1.0-.*"
- eq '${lines[5]}' "Running hook: post-install"
- eq '${lines[6]}' "Post install"
- eq '${#lines[*]}' 7
+ eq '${lines[5]}' "Post install"
+ eq '${#lines[*]}' 6
is_directory "my-app/.app"
}
diff --git a/test/data/app-a/root/etc/app.conf b/test/data/app-a/app.config
index d2c3f48..d2c3f48 100644
--- a/test/data/app-a/root/etc/app.conf
+++ b/test/data/app-a/app.config