-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.namevalue
More file actions
165 lines (143 loc) · 6.05 KB
/
bash.namevalue
File metadata and controls
165 lines (143 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# SCANPASS
# Bash Key-Value Store Functions
# This script provides a simple file-based key-value store.
# It stores data in a file (default: ~/.bash_kv_store) in the format 'key="value"'.
function kv_init() {
# --- Configuration ---
# Define the path to the key-value store file.
# You can change this path if you prefer a different location.
KV_STORE_FILE="${HOME}/.bash_kv_store"
# --- Initialization ---
# Create the store file if it doesn't exist and set appropriate permissions.
# This ensures the file is ready for use and restricts access to the owner.
if [[ ! -f "$KV_STORE_FILE" ]]; then
touch "$KV_STORE_FILE" || { echo "Error: Could not create KV store file at $KV_STORE_FILE" >&2; return 1; }
chmod 600 "$KV_STORE_FILE" || { echo "Error: Could not set permissions for $KV_STORE_FILE" >&2; return 1; }
fi
}
# --- Core Functions ---
# kv_set <key> <value>
# Persists or updates a name-value pair in the store.
# If the key already exists, its value will be updated.
# Otherwise, a new entry will be added.
function kv_set() {
local key="$1"
local value="$2"
core.logdebug
# Validate input: ensure a key is provided.
if [[ -z "$key" ]]; then
echo "Usage: kv_set <key> <value>" >&2
return 1
fi
# Escape double quotes within the value.
# This prevents issues when the value itself contains double quotes
# and ensures the 'key="value"' format remains valid.
local escaped_value=$(echo "$value" | sed 's/"/\\"/g')
# Check if the key already exists in the store file.
# '2>/dev/null' suppresses error messages if the file doesn't exist (though we initialize it above).
if grep -q "^${key}=\"" "$KV_STORE_FILE" 2>/dev/null; then
# Key exists: update the existing line.
# 'sed -i.bak' performs an in-place edit and creates a backup file.
# The regex 's|^${key}=\".*\"\$|${key}=\"${escaped_value}\"|'
# matches the entire line starting with 'key="' and replaces it.
sed -i.bak "s|^${key}=\".*\"\$|${key}=\"${escaped_value}\"|" "$KV_STORE_FILE"
rm -f "${KV_STORE_FILE}.bak" # Remove the backup file.
#printDebug "Updated key: '$key'"
else
# Key does not exist: append a new entry to the file.
echo "${key}=\"${escaped_value}\"" >> "$KV_STORE_FILE"
#printDebug "Added new key: '$key'"
fi
core.logreset
}
# kv_get <key>
# Retrieves the value associated with a given key from the store.
# Prints the value to standard output if found, otherwise prints an error message.
function kv_get() {
local key="$1"
# Validate input: ensure a key is provided.
if [[ -z "$key" ]]; then
echo "Usage: kv_get <key>" >&2
return 1
fi
# Find the line corresponding to the key.
# 'grep' searches for lines starting with 'key="' to ensure exact key match.
local line=$(grep "^${key}=\"" "$KV_STORE_FILE" 2>/dev/null)
if [[ -n "$line" ]]; then
# Extract the value from the matched line.
# We use Bash's regex matching for robustness.
# The regex captures the content inside the double quotes.
if [[ "$line" =~ ^[^=]+=\"(.*)\"$ ]]; then
local escaped_value="${BASH_REMATCH[1]}"
# Unescape double quotes that were escaped during storage.
echo "$escaped_value" | sed 's/\\"/"/g'
else
echo "Error: Malformed entry for key '$key' in store file." >&2
return 1
fi
else
echo "Key '$key' not found." >&2
return 1
fi
}
# kv_delete <key>
# Deletes a name-value pair from the store based on the provided key.
# If the key is not found, it prints an informational message.
function kv_delete() {
local key="$1"
# Validate input: ensure a key is provided.
if [[ -z "$key" ]]; then
echo "Usage: kv_delete <key>" >&2
return 1
fi
# Check if the key exists before attempting to delete.
if grep -q "^${key}=\"" "$KV_STORE_FILE" 2>/dev/null; then
# Key exists: delete the line.
# 'sed -i.bak' performs an in-place deletion and creates a backup file.
# The regex '/^${key}=\"/d' deletes lines starting with 'key="'.
sed -i.bak "/^${key}=\"/d" "$KV_STORE_FILE"
rm -f "${KV_STORE_FILE}.bak" # Remove the backup file.
echo "Deleted key: '$key'"
else
echo "Key '$key' not found." >&2
return 1
fi
}
# kv_list
# Lists all stored name-value pairs in the store.
# Prints each key and its corresponding unescaped value.
function kv_list() {
if [[ ! -f "$KV_STORE_FILE" || ! -s "$KV_STORE_FILE" ]]; then
echo "No key-value pairs stored yet."
return 0
fi
echo "Stored Key-Value Pairs:"
# Read the file line by line.
while IFS= read -r line; do
# Use Bash regex matching to parse each line in 'key="value"' format.
if [[ "$line" =~ ^([^=]+)=\"(.*)\"$ ]]; then
local key="${BASH_REMATCH[1]}"
local escaped_value="${BASH_REMATCH[2]}"
# Unescape double quotes for display.
local value=$(echo "$escaped_value" | sed 's/\\"/"/g')
echo " $key = $value"
fi
done < "$KV_STORE_FILE"
}
# --- Help Function ---
# kv_help
# Displays usage information for all key-value store functions.
function kv_help() {
echo "Bash Key-Value Store Functions:"
echo " kv_set <key> <value> - Persist or update a name-value pair."
echo " kv_get <key> - Retrieve the value for a given name."
echo " kv_delete <key> - Delete a name-value pair."
echo " kv_list - List all stored name-value pairs."
echo " kv_help - Display this help message."
echo ""
echo "Storage file: $KV_STORE_FILE"
echo "Note: Keys should ideally be simple strings (alphanumeric, underscores)."
echo " Keys cannot contain double quotes (\") or newlines."
echo " Values can contain spaces and double quotes, which will be escaped/unescaped automatically."
echo " This script is not designed for concurrent access from multiple processes."
}