From 11cf17750885324a58a68d237b0ac29bc3a6269b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 20 Oct 2013 18:09:50 +0200 Subject: o Making all app-conf options except import use <-l location>. o Setting $HOME to the temporary area when testing. o Adding test docs for app-conf. --- .gitignore | 1 + Makefile | 6 ++- STYLE-GUIDE.md | 2 + bin/app-conf | 79 ++++++++++++++++++++------------- docs/Makefile | 9 ++++ docs/app-conf.txt | 24 ++++++++++ lib/common | 47 ++++++++++++++++++++ libexec/app-cat-conf | 25 +++++++---- libexec/app-resolver-file | 4 +- libexec/app-resolver-maven | 5 +-- test/app-cat-conf.bats | 23 +++++++--- test/app-conf.bats | 106 +++++++++++++++++++++------------------------ test/app-init.bats | 6 +-- test/utils.bash | 24 ++++++++++ 14 files changed, 252 insertions(+), 109 deletions(-) create mode 100644 docs/Makefile create mode 100644 docs/app-conf.txt diff --git a/.gitignore b/.gitignore index b11d79c..3c70c2a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ apps.list downloads target +docs/*.html .app/var /*/.gitignore diff --git a/Makefile b/Makefile index d3f3c91..05ffacb 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: test +all: test docs BINS=$(wildcard bin/app-*) $(wildcard libexec/app-*) @@ -17,6 +17,10 @@ show-tests: test: show-tests $(TESTS) .PHONY: test +docs: + @make -C docs +.PHONY: docs + define set_header set_header-$(1): @count=`wc -l lib/header|cut -f 1 -d ' '`; \ diff --git a/STYLE-GUIDE.md b/STYLE-GUIDE.md index 2f9c6e6..c4c3a66 100644 --- a/STYLE-GUIDE.md +++ b/STYLE-GUIDE.md @@ -48,3 +48,5 @@ Resources --------- * Parameter expansion: + + diff --git a/bin/app-conf b/bin/app-conf index 44d128a..88498e9 100755 --- a/bin/app-conf +++ b/bin/app-conf @@ -10,8 +10,15 @@ APPSH_HOME=$(cd $(dirname "$0")/.. && pwd) # TODO: Add a 'get' command that returns a single value # If -r (required) given, exit with 0 if found, 1 otherwise. -# TODO: Add -s (shell) output, `app-conf get -s maven.artifact_id` => MAVEN_ARTIFACT_ID=.. -# Can be used like set -- `app-conf get -s maven.artifact_id maven.group_id` +# +# TODO: Add -s (shell) output: +# +# $ app-conf get -s maven.artifact_id +# MAVEN_ARTIFACT_ID=.. +# +# Can be used like this: +# +# set -- `app-conf get -s maven.artifact_id maven.group_id` PATH=$APPSH_HOME/libexec:$PATH @@ -44,13 +51,19 @@ conf_set() { assert_valid_config_name "$name" - if [ -r $file ] + local d=`dirname "$file"` + if [ ! -d "$d" ] then - sed "/^$name[ ]*=.*/d" $file > $file.tmp + fatal "Not a directory: $d" fi - echo "$name=$value" >> $file.tmp - mv $file.tmp $file + if [ -r "$file" ] + then + sed "/^$name[ ]*=.*/d" "$file" > "$file.tmp" + fi + + echo "$name=$value" >> "$file.tmp" + mv "$file.tmp" "$file" } conf_delete() { @@ -73,7 +86,7 @@ conf_import() { } usage_text() { - echo "usage: $0 conf " + echo "usage: $0 conf [-l location] " echo "" echo "Available commands:" echo " get [name] - returns a single value" @@ -83,12 +96,34 @@ usage_text() { echo " import [file] - import a file" echo "" echo "list is the default command." + echo "" + echo "Location is one of:" + echo " a or app, selects .app/config" + echo " u or user, selects ~/.appconfig" + echo " s or system, selects \$APPSH_HOME/lib/default-config" + echo "" + echo "Location is ignored for import." exit 1 } -app_home=${APP_HOME-.} -app_config="$app_home/.app/config" -user_config="$HOME/.appconfig" +_get_config_file_app config_a + +location=app +while getopts "l:" opt +do + case $opt in + l) + location="$OPTARG" + shift 2; + ;; + get|list|set|delete|import) + usage "Unknown command: $OPTARG" + ;; + esac +done + +file=$location +location_to_file file if [ $# -gt 0 ] then @@ -105,7 +140,7 @@ case "$command" in usage fi - app-cat-conf -f "$app_config" -n "$1" | cut -f 2 -d = | format_conf | sed "s, *$,," + app-cat-conf -l "$location" -n "$1" | cut -f 2 -d = | format_conf | sed "s, *$,," ;; list) if [ $# -gt 0 ] @@ -113,17 +148,7 @@ case "$command" in usage "Extra options." fi - 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 + app-cat-conf -l "$location" | format_conf ;; set) if [ $# -ne 2 ] @@ -131,9 +156,7 @@ case "$command" in usage fi - assert_is_app -C - - conf_set "$app_config" "$1" "$2" + conf_set "$file" "$1" "$2" ;; delete) if [ $# -ne 1 ] @@ -141,9 +164,7 @@ case "$command" in usage "Missing [name] argument." fi - assert_is_app -C - - conf_delete "$app_config" "$1" + conf_delete "$file" "$1" ;; import) if [ $# -ne 1 ] @@ -154,7 +175,7 @@ case "$command" in assert_is_app -C - conf_import "$app_config" "$1" + conf_import "$config_a" "$1" ;; *) if [ -z "$command" ] diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..f9ae4ea --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,9 @@ +TXT=$(wildcard *.txt) +HTML=$(patsubst %.txt,%.html,$(TXT)) +all: $(HTML) + +%.html: %.txt + asciidoc -a data-uri -a icons -a toc -a max-width=55em $< + +clean: + rm -rf $(wildcard *.html) diff --git a/docs/app-conf.txt b/docs/app-conf.txt new file mode 100644 index 0000000..27d995a --- /dev/null +++ b/docs/app-conf.txt @@ -0,0 +1,24 @@ +app-conf(1) +=========== + +NAME +---- +app-conf - configuration management + + +SYNOPSIS +-------- +[verse] +'app-conf' get +'app-conf' list +'app-conf' set +'app-conf' delete +'app-conf' import + +DESCRIPTION +----------- + +Interface to the configuration values used by 'app.sh'. Modelled after +the 'git-config' command. 'list' is the default command + +// vim: set ft=asciidoc: diff --git a/lib/common b/lib/common index d2d29c8..9e0db84 100755 --- a/lib/common +++ b/lib/common @@ -159,6 +159,53 @@ find_versions() { ) } +validate_location() { + local name=$1; shift + + case "${!name}" in + s|system) + eval ${name}=1 + ;; + u|user) + eval ${name}=2 + ;; + a|app) + eval ${name}=3 + ;; + *) + fatal "Invalid location: ${!name}, must be one of 's', 'system', 'u', 'user', 'a' or 'app'." + ;; + esac +} + +location_to_file() { + case "${!1}" in + s|system|1) + _get_config_file_system $1 + ;; + u|user|2) + _get_config_file_user $1 + ;; + a|app|3) + _get_config_file_app $1 + ;; + *) + fatal "Invalid location: ${!1}, must be one of 's', 'system', 'u', 'user', 'a' or 'app'." + esac +} + +_get_config_file_system() { + eval $1="${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config}" +} + +_get_config_file_user() { + eval $1="$HOME/.appconfig" +} + +_get_config_file_app() { + eval $1=".app/config" +} + grep_path() { local regex="$1"; shift local path="$1"; shift diff --git a/libexec/app-cat-conf b/libexec/app-cat-conf index ce0a3ef..49d3284 100755 --- a/libexec/app-cat-conf +++ b/libexec/app-cat-conf @@ -16,7 +16,9 @@ declare -a arg_files name="" use_default_files=yes -while getopts "f:Dn:" opt +location=app + +while getopts "f:Dn:l:" opt do case $opt in f) @@ -33,26 +35,33 @@ do n) name=$OPTARG ;; + l) + location=$OPTARG + ;; \?) - usage "Invalid option: $OPTARG" + usage "Invalid option: $opt" ;; esac done +validate_location location + if [[ $use_default_files == yes ]] then - app_home=${APP_HOME-.} + _get_config_file_system config_s + _get_config_file_user config_u + _get_config_file_app config_a - files+=(${APPSH_DEFAULT_CONFIG-$APPSH_HOME/lib/default-config}) + files+=($config_s) - if [ -r "$HOME/.appconfig" ] + if [ "$location" -ge 2 -a -r "$config_u" ] then - files+=("$HOME/.appconfig") + files+=("$config_u") fi - if [ -r "$app_home/.app/config" ] + if [ "$location" -ge 3 -a -r "$config_a" ] then - files+=("$app_home/.app/config") + files+=("$config_a") fi fi diff --git a/libexec/app-resolver-file b/libexec/app-resolver-file index 500b3a0..5aedf88 100755 --- a/libexec/app-resolver-file +++ b/libexec/app-resolver-file @@ -24,7 +24,7 @@ init() { OPTIND=1 ;; *) - usage "Invalid option: $OPTARG" + usage "Invalid option: $OPTARG" ;; esac done @@ -59,7 +59,7 @@ download_version() { target="$OPTARG" ;; *) - usage "Invalid option: $OPTARG" + usage "Invalid option: $OPTARG" ;; esac done diff --git a/libexec/app-resolver-maven b/libexec/app-resolver-maven index 118223a..5af7596 100755 --- a/libexec/app-resolver-maven +++ b/libexec/app-resolver-maven @@ -18,7 +18,6 @@ slash() { echo $1 | sed "s,\.,/,g" } -# TODO: support file:// repositories # TODO: look in the local repository first get() { local url=$1; shift @@ -149,7 +148,7 @@ download_version() { target="$OPTARG" ;; *) - usage "Invalid option: $OPTARG" + usage "Invalid option: $OPTARG" ;; esac done @@ -189,7 +188,7 @@ init() { OPTIND=1 ;; *) - usage "Invalid option: $OPTARG" + usage "Invalid option: $OPTARG" ;; esac done diff --git a/test/app-cat-conf.bats b/test/app-cat-conf.bats index 62309a5..2acdd99 100755 --- a/test/app-cat-conf.bats +++ b/test/app-cat-conf.bats @@ -65,7 +65,7 @@ foo.wat=bar" ]] eq '${#lines[*]}' 1 } -@test "uses \$APPSH_DEFAULT_CONFIG, check order" { +@test "uses \$APPSH_DEFAULT_CONFIG, with lowest priority" { app_libexec app-cat-conf -f $APPSH_HOME/test/data/app-cat-conf/config-3 echo_lines eq '${lines[0]}' "foo.bar=baz" @@ -73,7 +73,7 @@ foo.wat=bar" ]] eq '${#lines[*]}' 2 } -@test "app-cat-conf read from installation's, user's and then app's config" { +@test "app-cat-conf - read installation's and user's config when outside app" { 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 @@ -83,15 +83,24 @@ foo.wat=bar" ]] eq '${#lines[*]}' 2 } -# With home directory, outside app -@test "./app conf - should read user's conf too, in app" { +@test "app-cat-conf - read \$HOME/.appconfig and .app/config when inside 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[0]}' "foo.bar=2" + eq '${lines[1]}' "foo.baz=3" + eq '${lines[2]}' "foo.foo=2" eq '${#lines[*]}' 3 } + +@test "app-cat-conf -l u - read only \$HOME/.appconfig even when in an app" { + HOME=$APPSH_HOME/test/data/app-cat-conf/home + cd $APPSH_HOME/test/data/app-cat-conf/my-app + app_libexec app-cat-conf -l u; echo_lines + eq '$status' 0 + eq '${lines[0]}' "foo.bar=1" + eq '${lines[1]}' "foo.foo=2" + eq '${#lines[*]}' 2 +} diff --git a/test/app-conf.bats b/test/app-conf.bats index c95a133..d1a2b18 100755 --- a/test/app-conf.bats +++ b/test/app-conf.bats @@ -10,34 +10,29 @@ setup_inner() { } @test "./app conf - happy day" { - app conf; echo_lines + app conf echo "app.bin=bin/app-a" > .app/config eq '$status' 0 eq '${#lines[*]}' 0 - app conf set g.FOO bar; echo_lines - eq '$status' 0 + app conf set g.FOO bar - app conf; echo_lines - eq '$status' 0 + app conf eq '${lines[0]}' "app.bin bin/app-a " eq '${lines[1]}' "g.FOO bar " eq '${#lines[*]}' 2 - app conf get g.FOO; echo_lines - eq '$status' 0 + app conf get g.FOO eq '${lines[0]}' "bar" eq '${#lines[*]}' 1 - app conf get g.foo; echo_lines - eq '$status' 0 + app conf get g.foo eq '${#lines[*]}' 0 - app conf delete g.FOO; echo_lines - eq '$status' 0 + app conf delete g.FOO + eq '${#lines[*]}' 0 - app conf; echo_lines - eq '$status' 0 + app conf eq '${lines[0]}' "app.bin bin/app-a " eq '${#lines[*]}' 1 } @@ -45,82 +40,82 @@ setup_inner() { @test "./app conf - defaults to 'list'" { echo "app.bin=bin/app-a" > .app/config - app conf; echo_lines - eq '$status' 0 + app conf eq '${#lines[*]}' 1 eq '${lines[0]}' "app.bin bin/app-a " } @test "./app conf wat" { - app conf wat; echo_lines - eq '$status' 1 + check_status=no + app conf wat eq '${lines[0]}' "Unknown command: wat" } @test "./app conf list" { echo "app.bin=bin/app-a" > .app/config - app conf; echo_lines - eq '$status' 0 + app conf eq '${#lines[*]}' 1 eq '${lines[0]}' "app.bin bin/app-a " - app conf list; echo_lines - eq '$status' 0 + app conf list eq '${#lines[*]}' 1 eq '${lines[0]}' "app.bin bin/app-a " - app conf list foo; echo_lines + check_status=no + app conf list foo eq '$status' 1 } -#@test "./app conf list-group" { -# app conf set mygroup a 1 -# eq '$status' 0 -# app conf set mygroup b 1 -# eq '$status' 0 -# app conf set mygroup c 2 -# eq '$status' 0 -# app conf set othergroup a 1 -# eq '$status' 0 -# -# app conf list; echo_lines -# eq '$status' 0 -# app conf list-group mygroup; echo_lines -# eq '$status' 0 -# eq '${lines[0]}' "mygroup.a 1 " -# eq '${lines[1]}' "mygroup.b 1 " -# eq '${lines[2]}' "mygroup.c 2 " -# eq '${#lines[*]}' 3 -#} +@test "./app conf list - with duplicate entries" { + echo "foo.bar=awesome" > .app/config + echo "foo.bar=awesome" >> .app/config + + app conf list + eq '${lines[0]}' "foo.bar awesome " + eq '${#lines[*]}' 1 +} + +@test "./app conf list in non-app dir" { + mkdir wat + cd wat + app conf list + eq '${#lines[*]}' 0 +} @test "./app conf set" { echo "app.bin=bin/app-a" > .app/config - app conf set group; echo_lines + check_status=no + app conf set group eq '$status' 1 - app conf set group.foo; echo_lines + check_status=no + app conf set group.foo eq '$status' 1 - app conf set group.foo bar; echo_lines - eq '$status' 0 + app conf set group.foo bar eq '${#lines[*]}' 0 - app conf; echo_lines - eq '$status' 0 + app conf eq '${lines[0]}' "app.bin bin/app-a " eq '${lines[1]}' "group.foo bar " eq '${#lines[*]}' 2 } -@test "./app conf list - with duplicate entries" { - echo "foo.bar=awesome" > .app/config - echo "foo.bar=awesome" >> .app/config +@test "./app conf -l app set" { + echo > .app/config + app conf -l app set a.x 2 + app_libexec app-cat-conf + eq '${lines[0]}' "a.x=2" + eq '${#lines[*]}' 1 +} - app conf list; echo_lines - eq '$status' 0 - eq '${lines[0]}' "foo.bar awesome " +@test "./app conf -l user set" { + echo > .app/config + app conf -l user set a.x 3 + app_libexec app-cat-conf -l user + eq '${lines[0]}' "a.x=3" eq '${#lines[*]}' 1 } @@ -129,12 +124,11 @@ setup_inner() { echo "foo.baz=1" > config-b echo "foo.bar=2" >> config-b - app conf import config-b; echo_lines - eq '$status' 0 + app conf import config-b eq '${lines[0]}' "Importing config from config-b" eq '${#lines[*]}' 1 - app_libexec app-cat-conf; echo_lines + 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 db681a1..3f58890 100755 --- a/test/app-init.bats +++ b/test/app-init.bats @@ -22,7 +22,7 @@ load utils mkzip app-a install_artifact - app init -d my-app maven -r "file://$BATS_TMPDIR/repo" org.example:app-a:1.0-SNAPSHOT; echo_lines + app init -d my-app maven -r "file://$BATS_TMPDIR/repo" org.example:app-a:1.0-SNAPSHOT eq '$status' 0 eq '${lines[0]}' "Resolving Maven version 1.0-SNAPSHOT..." match '${lines[1]}' "Resolved version to 1.0-*" @@ -42,8 +42,8 @@ load utils 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 + app conf -l user set maven.repo "file://$BATS_TMPDIR/repo" + app init -d my-app maven org.example:app-a:1.0 match '${lines[0]}' "Resolved version to 1.0" match '${lines[1]}' "Downloading org.example:app-a:1.0-*" eq '${lines[2]}' "Unpacking..." diff --git a/test/utils.bash b/test/utils.bash index 26be39d..88dd889 100644 --- a/test/utils.bash +++ b/test/utils.bash @@ -15,6 +15,11 @@ setup() { rm -rf $BATS_TMPDIR/app.sh mkdir $BATS_TMPDIR/app.sh + + HOME=$BATS_TMPDIR/app.sh-home + mkdir -p $HOME + rm -f $HOME/.appconfig + cd $BATS_TMPDIR/app.sh REPO=$BATS_TMPDIR/repo @@ -49,9 +54,19 @@ install_artifact() { -DgroupId=org.example -DartifactId=app-a -Dversion=$version -Dpackaging=zip } +check_status=yes + app() { echo app $@ run $APPSH_HOME/app $@ + echo_lines + + if [ "$check_status" = yes ] + then + eq '$status' 0 + fi + + check_status=yes } app_libexec() { @@ -60,6 +75,15 @@ app_libexec() { echo libexec/$@ shift run "$x" $@ + + echo_lines + + if [ "$check_status" = yes ] + then + eq '$status' 0 + fi + + check_status=yes } fix_path_uname=`uname -s` -- cgit v1.2.3