From d2e3a3795f345fd78ead2cf06b1134b46f9d4bc4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 20 Oct 2013 12:43:41 +0200 Subject: o Getting app-cat-conf to read from ~/.appconfig too. --- Makefile | 8 +++---- app | 9 +++++--- bin/app-conf | 37 +++++++++++++++++++++++-------- libexec/app-cat-conf | 24 ++++++++++++++++---- test/app-cat-conf.bats | 36 +++++++++++++++++++++++++++++- test/app-conf.bats | 2 +- test/app-init.bats | 25 ++++++++++++++++++--- test/data/app-cat-conf/config-4 | 1 + test/data/app-cat-conf/home/.appconfig | 2 ++ test/data/app-cat-conf/my-app/.app/config | 2 ++ test/utils.bash | 3 ++- 11 files changed, 123 insertions(+), 26 deletions(-) create mode 100644 test/data/app-cat-conf/config-4 create mode 100644 test/data/app-cat-conf/home/.appconfig create mode 100644 test/data/app-cat-conf/my-app/.app/config diff --git a/Makefile b/Makefile index a3e5e42..d3f3c91 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,11 @@ all: test BINS=$(wildcard bin/app-*) $(wildcard libexec/app-*) BATS=$(sort $(patsubst test/%,%,$(filter-out test/X-%,$(wildcard test/*.bats)))) -TESTS=$(addprefix bats-,$(BATS)) +TESTS=$(addprefix test-,$(BATS)) -bats-%: - @echo === test/$(patsubst bats-%,%,$@) - @bats test/$(patsubst bats-%,%,$@) +test-%: + @echo === $@ + @bats $(patsubst test-%,test/%,$@) show-tests: @echo BATS=$(BATS) diff --git a/app b/app index 4993176..81c2c36 100755 --- a/app +++ b/app @@ -27,7 +27,7 @@ APPSH_HOME=`cd "$APPSH_HOME" && pwd` . $APPSH_HOME/lib/common echo_debug=no -while getopts "h" opt +while getopts ":hD:" opt do case $opt in h) @@ -35,9 +35,11 @@ do ;; D) echo_debug=yes + eval OPTIND=$((OPTIND-1)) + shift ;; - \?) - usage + *) + break ;; esac done @@ -66,4 +68,5 @@ PATH=$APPSH_HOME/bin:$PATH # TODO: this is probably a good place to clean up the environment exec env \ "APPSH_HOME=$APPSH_HOME" \ + "echo_debug=$echo_debug" \ "$bin" "$@" diff --git a/bin/app-conf b/bin/app-conf index fde357f..44d128a 100755 --- a/bin/app-conf +++ b/bin/app-conf @@ -38,6 +38,7 @@ assert_valid_config_name() { } conf_set() { + local file=$1; shift local name=$1; shift local value=$1; shift @@ -53,6 +54,7 @@ conf_set() { } conf_delete() { + local file=$1; shift local name=$1; shift assert_valid_config_name "$name" @@ -66,7 +68,7 @@ conf_import() { local src=$1; shift echo "Importing config from $src" - app-cat-conf -f "$src" -f "$dst" > "$dst.tmp" + app-cat-conf -f "$dst" -f "$src" > "$dst.tmp" mv "$dst.tmp" "$dst" } @@ -79,11 +81,14 @@ usage_text() { echo " set [name] [value] - set a config parameter" echo " delete [name] - deletes a config parameter" echo " import [file] - import a file" + echo "" + echo "list is the default command." exit 1 } app_home=${APP_HOME-.} -file="$app_home/.app/config" +app_config="$app_home/.app/config" +user_config="$HOME/.appconfig" if [ $# -gt 0 ] then @@ -93,8 +98,6 @@ else command=list fi -assert_is_app -C - case "$command" in get) if [ $# != 1 ] @@ -102,7 +105,7 @@ case "$command" in usage fi - app-cat-conf -f "$file" -n "$1" | cut -f 2 -d = | format_conf | sed "s, *$,," + app-cat-conf -f "$app_config" -n "$1" | cut -f 2 -d = | format_conf | sed "s, *$,," ;; list) if [ $# -gt 0 ] @@ -110,7 +113,17 @@ case "$command" in usage "Extra options." fi - app-cat-conf -f "$file" | format_conf + if [ ! -r "$app_config" ] + then + app_config="" + fi + + if [ ! -r "$user_config" ] + then + user_config="" + fi + + app-cat-conf "${user_config+-f ${user_config}}" -f "$app_config" | format_conf ;; set) if [ $# -ne 2 ] @@ -118,7 +131,9 @@ case "$command" in usage fi - conf_set "$1" "$2" + assert_is_app -C + + conf_set "$app_config" "$1" "$2" ;; delete) if [ $# -ne 1 ] @@ -126,7 +141,9 @@ case "$command" in usage "Missing [name] argument." fi - conf_delete "$1" + assert_is_app -C + + conf_delete "$app_config" "$1" ;; import) if [ $# -ne 1 ] @@ -135,7 +152,9 @@ case "$command" in exit 1 fi - conf_import "$file" "$1" + assert_is_app -C + + conf_import "$app_config" "$1" ;; *) if [ -z "$command" ] diff --git a/libexec/app-cat-conf b/libexec/app-cat-conf index af382f7..ce0a3ef 100755 --- a/libexec/app-cat-conf +++ b/libexec/app-cat-conf @@ -11,6 +11,8 @@ APPSH_HOME=$(cd $(dirname "$0")/.. && pwd) key_expr="[a-zA-Z][_a-zA-Z0-9]*" files=() +arg_files=() +declare -a arg_files name="" use_default_files=yes @@ -18,12 +20,12 @@ while getopts "f:Dn:" opt do case $opt in f) - file=$OPTARG + file="$OPTARG" if [[ $file == "-" ]] then file=/dev/stdin fi - files+=($file) + arg_files+=($file) ;; D) use_default_files=no @@ -40,12 +42,25 @@ done if [[ $use_default_files == yes ]] then app_home=${APP_HOME-.} + + files+=(${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config}) + + if [ -r "$HOME/.appconfig" ] + then + files+=("$HOME/.appconfig") + fi + if [ -r "$app_home/.app/config" ] then files+=("$app_home/.app/config") fi +fi - files+=(${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config}) +# Even if arg_files is declared above, the files+= statement will fail +# with "unbound" variable. bash-4.2.45. +if [ "${#arg_files[@]}" -gt 0 ] +then + files+=("${arg_files[@]}") fi # TODO: find config files in the paths above $file's paths and perhaps @@ -61,7 +76,8 @@ fi debug "Using files:" "${files[@]}" # The awk script makes sure each key only appears once. The first one wins -cat "${files[@]}" | \ + +(for ((idx=${#files[@]}-1 ; idx>=0 ; idx-- )); do cat ${files[idx]}; done) | \ sed -n -e "$filter" | \ awk -F = ' (!($1 in a)){a[$1]; print }' | \ sort diff --git a/test/app-cat-conf.bats b/test/app-cat-conf.bats index 8060cea..62309a5 100755 --- a/test/app-cat-conf.bats +++ b/test/app-cat-conf.bats @@ -43,21 +43,55 @@ setup_inner() { @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 -) + $APPSH_HOME/libexec/app-cat-conf -D -f - -f $APPSH_HOME/test/data/app-cat-conf/config-2) [[ $x == "foo.bar=wat foo.wat=bar" ]] } +@test "app-cat-conf read multiple files, last file wins" { + app_libexec app-cat-conf \ + -f $APPSH_HOME/test/data/app-cat-conf/config-2 \ + -f $APPSH_HOME/test/data/app-cat-conf/config-4 + echo_lines + eq '${lines[0]}' "foo.bar=foo" + eq '${#lines[*]}' 1 +} + @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 echo_lines eq '${lines[0]}' "foo.bar=wat" eq '${#lines[*]}' 1 +} +@test "uses \$APPSH_DEFAULT_CONFIG, check order" { app_libexec app-cat-conf -f $APPSH_HOME/test/data/app-cat-conf/config-3 echo_lines eq '${lines[0]}' "foo.bar=baz" eq '${lines[1]}' "foo.wat=bar" eq '${#lines[*]}' 2 } + +@test "app-cat-conf read from installation's, user's and then app's config" { + HOME=$APPSH_HOME/test/data/app-cat-conf/home + APPSH_DEFAULT_CONFIG=$APPSH_HOME/test/data/app-cat-conf/config-2 + app_libexec app-cat-conf; echo_lines + eq '$status' 0 + eq '${lines[0]}' "foo.bar=1" + eq '${lines[1]}' "foo.foo=2" + eq '${#lines[*]}' 2 +} + +# With home directory, outside app +@test "./app conf - should read user's conf too, in app" { + HOME=$APPSH_HOME/test/data/app-cat-conf/home + APPSH_DEFAULT_CONFIG=$APPSH_HOME/test/data/app-cat-conf/config-2 + cd $APPSH_HOME/test/data/app-cat-conf/my-app + app_libexec app-cat-conf; echo_lines + eq '$status' 0 + eq '${lines[0]}' "foo.bar=2" + eq '${lines[1]}' "foo.baz=3" + eq '${lines[2]}' "foo.foo=2" + eq '${#lines[*]}' 3 +} diff --git a/test/app-conf.bats b/test/app-conf.bats index 13dc18b..c95a133 100755 --- a/test/app-conf.bats +++ b/test/app-conf.bats @@ -134,7 +134,7 @@ setup_inner() { eq '${lines[0]}' "Importing config from config-b" eq '${#lines[*]}' 1 - app_libexec app-cat-conf + app_libexec app-cat-conf; echo_lines 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 ebf850c..db681a1 100755 --- a/test/app-init.bats +++ b/test/app-init.bats @@ -25,11 +25,11 @@ load utils app init -d my-app maven -r "file://$BATS_TMPDIR/repo" org.example:app-a:1.0-SNAPSHOT; echo_lines eq '$status' 0 eq '${lines[0]}' "Resolving Maven version 1.0-SNAPSHOT..." - match '${lines[1]}' "Resolved version to 1.0-.*" - match '${lines[2]}' "Downloading org.example:app-a:1.0-.*" + match '${lines[1]}' "Resolved version to 1.0-*" + match '${lines[2]}' "Downloading org.example:app-a:1.0-*" eq '${lines[3]}' "Unpacking..." match '${lines[4]}' "Importing config from versions/1.0-*" - match '${lines[5]}' "Creating current symlink for version 1.0-.*" + match '${lines[5]}' "Creating current symlink for version 1.0-*" eq '${lines[6]}' "Post install" eq '${#lines[*]}' 7 @@ -37,3 +37,22 @@ load utils # Created by post-install is_directory "my-app/logs" } + +@test "Install release artifact" { + mkzip app-a + install_artifact 1.0 + + app init -d my-app maven -r "file://$BATS_TMPDIR/repo" org.example:app-a:1.0; echo_lines + eq '$status' 0 + match '${lines[0]}' "Resolved version to 1.0" + match '${lines[1]}' "Downloading org.example:app-a:1.0-*" + eq '${lines[2]}' "Unpacking..." + match '${lines[3]}' "Importing config from versions/1.0-*" + match '${lines[4]}' "Creating current symlink for version 1.0-*" + eq '${lines[5]}' "Post install" + eq '${#lines[*]}' 6 + + is_directory "my-app/.app" + # Created by post-install + is_directory "my-app/logs" +} diff --git a/test/data/app-cat-conf/config-4 b/test/data/app-cat-conf/config-4 new file mode 100644 index 0000000..8b3d4e9 --- /dev/null +++ b/test/data/app-cat-conf/config-4 @@ -0,0 +1 @@ +foo.bar=foo diff --git a/test/data/app-cat-conf/home/.appconfig b/test/data/app-cat-conf/home/.appconfig new file mode 100644 index 0000000..a505098 --- /dev/null +++ b/test/data/app-cat-conf/home/.appconfig @@ -0,0 +1,2 @@ +foo.bar=1 +foo.foo=2 diff --git a/test/data/app-cat-conf/my-app/.app/config b/test/data/app-cat-conf/my-app/.app/config new file mode 100644 index 0000000..e3624d5 --- /dev/null +++ b/test/data/app-cat-conf/my-app/.app/config @@ -0,0 +1,2 @@ +foo.baz=3 +foo.bar=2 diff --git a/test/utils.bash b/test/utils.bash index ad80629..26be39d 100644 --- a/test/utils.bash +++ b/test/utils.bash @@ -42,10 +42,11 @@ mkzip() { } install_artifact() { + local version=${1-1.0-SNAPSHOT} describe -Dfile=`fix_path $APPSH_HOME/test/data/app-a.zip` -DgeneratePom PATH=$ORIG_PATH mvn deploy:deploy-file -Durl=$FIXED_REPO_URL \ -Dfile=`fix_path $APPSH_HOME/test/data/app-a.zip` -DgeneratePom \ - -DgroupId=org.example -DartifactId=app-a -Dversion=1.0-SNAPSHOT -Dpackaging=zip + -DgroupId=org.example -DartifactId=app-a -Dversion=$version -Dpackaging=zip } app() { -- cgit v1.2.3