Skip to content

Commit

Permalink
Allow var=key pgets in varp, add tests for varp to the hackish bashis…
Browse files Browse the repository at this point in the history
…h-test
  • Loading branch information
akatrevorjay committed May 13, 2012
1 parent 85cbe9f commit 4ba7c94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 14 additions & 0 deletions bashism-test
Expand Up @@ -29,6 +29,20 @@ block_normal() {
e "this is a test"
error "Testing error"

e "Testing VARP"
e "- Setting testkey=testval"
$b.pset "testkey=testval"

e "- Getting testkey via \$ret"
$b.pget 'testkey'
[[ "${ret[0]}" == "testval" ]] || death "Got bad ret=${ret[0]}"
e "-- ret=${ret[0]}"

e "- Getting testkey via var=key"
$b.pget 'testget=testkey'
[[ "$testget" == "testval" ]] || death "Got bad testget=$testget"
e "-- testget=$testget"

death "To this block (or true)" || true
}

Expand Down
24 changes: 17 additions & 7 deletions bashism.d/varp.sh
Expand Up @@ -12,18 +12,28 @@ function bashism.var.pset() {
done
}

## -> b.pget(@key)
## Gets bashism varpiables
## -> b.pget(@key|@var=key)
## Gets bashism varpiables, places result into var if in var=key form
function bashism.var.pget() {
local i=; for i in "$@"; do
# This is not exactly meant to be used with user data, so it's not shell safe
local i= v=; for i in "$@"; do
# If key=val form
v=; if [[ "$i" == *=* ]]; then
v="${i%%=*}"; i="${i#*=}"
fi

if [[ -f "${__BASHISM[VARP_DIR]}/$i" ]]; then
__BASHISM_VARP["$i"]=$(< "${__BASHISM[VARP_DIR]}/$i")
__BASHISM_VARP[$i]=$(< "${__BASHISM[VARP_DIR]}/$i")
else
unset __BASHISM_VARP["$i"]
unset __BASHISM_VARP[$i]
fi

builtin echo "${__BASHISM_VARP[$i]}"
ret=("${__BASHISM_VARP[$i]}")

# If params were in key=val form
if [[ -n "$v" ]]; then
# Set var ${!key} equal to $val
eval $v=\"${ret[0]}\"
fi
done
}

Expand Down

0 comments on commit 4ba7c94

Please sign in to comment.