Unverified Commit 9911a265 authored by Jeremy Morrell's avatar Jeremy Morrell Committed by GitHub

[kvstore] wrap string with spaces (#594)

* [kvstore] wrap string with spaces
parent 8469d24d
...@@ -19,6 +19,7 @@ kv_set() { ...@@ -19,6 +19,7 @@ kv_set() {
fi fi
} }
# get the value, but don't unwrap quotes
kv_get() { kv_get() {
if [[ $# -eq 2 ]]; then if [[ $# -eq 2 ]]; then
local f=$1 local f=$1
...@@ -28,6 +29,15 @@ kv_get() { ...@@ -28,6 +29,15 @@ kv_get() {
fi fi
} }
kv_get_escaped() {
local value=$(kv_get $1 $2 $3)
if [[ $value =~ [[:space:]]+ ]]; then
echo "\"$value\""
else
echo $value
fi
}
kv_keys() { kv_keys() {
local f=$1 local f=$1
local keys=() local keys=()
...@@ -47,7 +57,7 @@ kv_list() { ...@@ -47,7 +57,7 @@ kv_list() {
kv_keys $f | tr ' ' '\n' | while read -r key; do kv_keys $f | tr ' ' '\n' | while read -r key; do
if [[ -n $key ]]; then if [[ -n $key ]]; then
echo "$key=$(kv_get $f $key)" echo "$key=$(kv_get_escaped $f $key)"
fi fi
done done
} }
...@@ -108,6 +108,16 @@ testKeyValue() { ...@@ -108,6 +108,16 @@ testKeyValue() {
assertEquals "" "$(kv_list $store)" assertEquals "" "$(kv_list $store)"
} }
testKeyValueEscaping() {
local store=$(mktemp)
kv_create $store
kv_set $store "key" "value with a space"
assertEquals "key=\"value with a space\"" "$(kv_list $store)"
assertEquals "value with a space" "$(kv_get $store "key")"
}
# if the file doesn't exist, everything should be a no-op # if the file doesn't exist, everything should be a no-op
testKeyValueNoFile() { testKeyValueNoFile() {
# empty file argument # empty file argument
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment