Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: damphat/kv-bash
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0
Choose a base ref
...
head repository: damphat/kv-bash
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 4 commits
  • 3 files changed
  • 4 contributors

Commits on Apr 25, 2022

  1. Allow users to use different directories based of an env var (#11)

    Co-authored-by: JButler <[email protected]>
    fattredd and JButler authored Apr 25, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    f0936e1 View commit details

Commits on Sep 23, 2022

  1. Copy the full SHA
    62369f1 View commit details
  2. Copy the full SHA
    4118968 View commit details
  3. Corrected minor typo (#12)

    Co-authored-by: Phat <[email protected]>
    digitalkram and damphat authored Sep 23, 2022
    Copy the full SHA
    383233d View commit details
Showing with 47 additions and 10 deletions.
  1. +2 −0 .gitignore
  2. +16 −9 kv-bash
  3. +29 −1 kv-test
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/
25 changes: 16 additions & 9 deletions kv-bash
100755 → 100644
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@
# CONSTANTS
########################

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

########################
# LOCAL FUNCTIONS
@@ -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 " +-------------------------------+"
}

@@ -67,7 +69,7 @@ kv_validate_key() {

[[ "${BASH_SOURCE[0]}" != "${0}" ]] || {
kv_echo_err " +------------------------------------------------+"
kv_echo_err " | FALTAL ERROR: wrong usage :( |"
kv_echo_err " | FATAL ERROR: wrong usage :( |"
kv_echo_err " | You should use this via source |"
kv_echo_err " | $ source ./kv-bash |"
kv_echo_err " | |"
@@ -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" != "" ]
@@ -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>
@@ -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")"
@@ -143,5 +149,6 @@ kvlist() {
# clear all key/value pairs in database
# Usage: kvclear
kvclear() {
rm -rf "$KV_USER_DIR"
}
kv_user_dir=${KV_USER_DIR:-$default_kv_user_dir}
rm -rf "$kv_user_dir"
}
30 changes: 29 additions & 1 deletion kv-test
Original file line number Diff line number Diff line change
@@ -8,19 +8,30 @@ FAILED() { echo -e "[\e[01;31mFAILED\e[0m]"; }
RESULT() { [ $? == 0 ] && OK || FAILED; }

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

# 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:
echo ===================

TESTCASE 'NO KV_USER_DIR: kvclear; kvlist => line count = 0'
kvset cat Tom
kvset mouse Jerry
kvclear
[ $(kvlist | wc -l) == 0 ]
RESULT

KV_USER_DIR="$test_dirA"

TESTCASE 'call kvget for non-exist key should return empty'
[ "$(kvget name)" == "" ]
RESULT
@@ -92,6 +103,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