Make arguments simpler to deal with in action functions

<source> was an optional argument, and the second out of three passed,
meaning when only two arguments were given, the action functions would
have to know to use the second argument as <target>, instead of the the
third. Now the second argument is always <target>, and the <source>
argument is always the third, if given.
This commit is contained in:
2014-03-08 13:32:24 +00:00
parent e0f0177301
commit e49c930771
2 changed files with 24 additions and 7 deletions

View File

@@ -1,17 +1,26 @@
dotify-action() {
local action="$1"
local source="$2"
local target="$3"
if [ $# -lt 3 ]; then
local target="$2"
else
local source="$2"
local target="$3"
fi
if [ "$action" == "default" ]; then
action="$DOTIFY_OPT_DEFAULT_ACTION"
fi
! valid_action="$(command -v "dotify-action-${action}")"
! local valid_action="$(command -v "dotify-action-${action}")"
if [ -z "$valid_action" ]; then
echo "ERROR: \"$action\" is not a valid action." >&2
return 1
fi
dotify-action-${action} "$source" "$target"
if [ -n "$source" ]; then
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target" "$source"
else
dotify-action-${action} "$DOTIFY_RUN_MODE" "$target"
fi
}

View File

@@ -6,7 +6,8 @@ source "../../src/lib/dotify-action.sh"
# dotify-action() tests
#
# Set required option ENV
# Set required environment variables.
DOTIFY_RUN_MODE="install"
DOTIFY_OPT_DEFAULT_ACTION="link"
# Simple mock for link action.
@@ -16,10 +17,17 @@ dotify-action-link() {
# Given a specific action.
assert "dotify-action link ackrc .ackrc" "link stub: ackrc .ackrc"
assert "dotify-action link ackrc .ackrc" "link stub: install .ackrc ackrc"
# Given a specific action without a <source>.
assert "dotify-action link .ackrc" "link stub: install .ackrc"
# Given "default" action, it uses configured default action.
assert "dotify-action default ackrc .ackrc" "link stub: ackrc .ackrc"
assert "dotify-action default ackrc .ackrc" "link stub: install .ackrc ackrc"
# Given "default" action without a <source>, it uses configured default
# action.
assert "dotify-action default .ackrc" "link stub: install .ackrc"
# Given a invalid action.
assert_raises "dotify-action foo ackrc .ackrc" 1