Skip to content

Commit

Permalink
Allow users to use different directories based of an env var (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: JButler <[email protected]>
  • Loading branch information
fattredd and JButler authored Apr 25, 2022
1 parent d7ac6c9 commit f0936e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
testing_dirA/
testing_dirB/
18 changes: 12 additions & 6 deletions kv-bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# CONSTANTS
########################

KV_USER_DIR="$HOME/.kv-bash"
default_kv_user_dir="$HOME/.kv-bash"

########################
# LOCAL FUNCTIONS
Expand All @@ -50,9 +50,11 @@ kv_echo_err() {

# Usage: kv_echo_err_box <err-msg> <function-name>
kv_echo_err_box() {
kv_user_dir=${KV_USER_DIR:-$default_kv_user_dir}
kv_echo_err " +-------------------------------+"
kv_echo_err " | ERROR: $1"
kv_echo_err " | function: $2"
kv_echo_err " | dir: $kv_user_dir"
kv_echo_err " +-------------------------------+"
}

Expand Down Expand Up @@ -101,7 +103,8 @@ kvget() {
kv_echo_err_box 'invalid param "key"' 'kvget()'
return 1
}
VALUE="$([ -f "$KV_USER_DIR/$key" ] && cat "$KV_USER_DIR/$key")"
kv_user_dir=${KV_USER_DIR:-$default_kv_user_dir}
VALUE="$([ -f "$kv_user_dir/$key" ] && cat "$kv_user_dir/$key")"
echo "$VALUE"

[ "$VALUE" != "" ]
Expand All @@ -115,8 +118,9 @@ kvset() {
kv_echo_err_box 'invalid param "key"' 'kvset()'
return 1
}
test -d "$KV_USER_DIR" || mkdir "$KV_USER_DIR"
echo "$value" > "$KV_USER_DIR/$key"
kv_user_dir=${KV_USER_DIR:-$default_kv_user_dir}
test -d "$kv_user_dir" || mkdir "$kv_user_dir"
echo "$value" > "$kv_user_dir/$key"
}

# Usage: kvdel <key>
Expand All @@ -126,13 +130,15 @@ kvdel() {
kv_echo_err_box 'invalid param "key"' 'kvdel()'
return 1
}
test -f "$KV_USER_DIR/$key" && rm -f "$KV_USER_DIR/$key"
kv_user_dir=${KV_USER_DIR:-$default_kv_user_dir}
test -f "$kv_user_dir/$key" && rm -f "$kv_user_dir/$key"
}

# list all key/value pairs to stdout
# Usage: kvlist
kvlist() {
for i in "$KV_USER_DIR/"*; do
kv_user_dir=${KV_USER_DIR:-$default_kv_user_dir}
for i in "$kv_user_dir/"*; do
if [ -f "$i" ]; then
key="$(basename "$i")"
echo "$key" "$(kvget "$key")"
Expand Down
22 changes: 21 additions & 1 deletion kv-test
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ FAILED() { echo -e "[\e[01;31mFAILED\e[0m]"; }
RESULT() { [ $? == 0 ] && OK || FAILED; }

TEST_COUNT=0
test_dirA="./testingdirA"
test_dirB="./testingdirB"
KV_USER_DIR="$test_dirA"

# Usage: TESTCASE [description string]
function TESTCASE() {
((TEST_COUNT++))
printf "%3s %-50s" "$TEST_COUNT" "$1"
}

rm -rf "$KV_USER_DIR"
rm -rf "$test_dirA" "$test_dirB"

echo
echo RUN ALL TEST CASES:
Expand Down Expand Up @@ -92,6 +95,23 @@ TESTCASE 'spaces in value'
[ "$(kvget name)" == ' phat dam ' ]
RESULT
kvdel name

TESTCASE 'can change user dir'
kvclear
kvset Foo Bar
KV_USER_DIR="$test_dirB"
kvset var1 value1
kvset var2 value2
kvset var3 value3
[ $(kvlist | wc -l) == 3 ]
RESULT
kvclear

TESTCASE 'can return to old user dir'
KV_USER_DIR="$test_dirA"
[ $(kvlist | wc -l) == 1 ]
RESULT
kvclear

#more testcase here

Expand Down

0 comments on commit f0936e1

Please sign in to comment.