-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkv
177 lines (162 loc) · 4.76 KB
/
kv
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
#
## @file Utils/kv
## @brief Utility functions to store and retrieve key/value pairs
## @author Ronald Joe Record (rr at ronrecord dot com)
## @copyright Copyright (c) 2015, Ronald Joe Record, all rights reserved.
## @date Written 6-Jul-2015
## @version 1.0.1
##
## Downloded from https://github.com/damphat/kv-bash
## Provides persistent storage of key/value pairs during execution
##
## ABOUT kv-bash:
## key/value dabatase
## database store in HOME directory
## each user has 1 database
## imports 5 bash functions via ```$ source kv-bash```
##
## Author: damphat
## Version: 0.1
## Requirements: unix-like environement, no dependencies
##
## USAGE:
## source ./kv-bash # import kv-bash functions
## kvset <key> <value> # assign value to key
## kvget <key> # get value of key
## kvdel <key> # kvdelete by key
## kvlist # list all current key/value pairs
## kvclear # clear database
##
## EXAMPLES:
## $ source ./kv-bash
## $ kvset user mr.bob
## $ kvset pass abc@123
## $ kvlist
## user mr.bob
## pass abc@123
## $ kvget user
## mr.bob
## $ kvget pass
## abc@123
## $ kvdel pass
## $ kvget pass
##
## $ kvclear
########################
# CONSTANTS
########################
KV_USER_DIR="$HOME/.kv-bash"
########################
# LOCAL FUNCTIONS
########################
# print to stderr, red color
kv_echo_err() {
echo -e "\e[01;31m$@\e[0m" >&2
}
# Usage: kv_echo_err_box <err-msg> <function-name>
kv_echo_err_box() {
kv_echo_err " +-------------------------------+"
kv_echo_err " | ERROR: $1"
kv_echo_err " | function: $2"
kv_echo_err " +-------------------------------+"
}
# Usage: kv_validate_key <key>
kv_validate_key() {
[[ "$1" =~ ^[0-9a-zA-Z._:-]+$ ]]
}
########################
# ENSURE THIS-FILE IS CALL BY 'source ./kv-bash'
########################
[[ "${BASH_SOURCE[0]}" != "${0}" ]] || {
kv_echo_err " +------------------------------------------------+"
kv_echo_err " | FALTAL ERROR: wrong usage :( |"
kv_echo_err " | You should use this via source |"
kv_echo_err " | $ source ./kv-bash |"
kv_echo_err " | |"
kv_echo_err " | Examples: |"
kv_echo_err " | $ source ./kv-bash |"
kv_echo_err " | $ kvset user mr.bob |"
kv_echo_err " | $ kvset pass abc@123 |"
kv_echo_err " | $ kvlist |"
kv_echo_err " | user mr.bob |"
kv_echo_err " | pass abc@123 |"
kv_echo_err " | $ kvget user |"
kv_echo_err " | mr.bob |"
kv_echo_err " | $ kvget pass |"
kv_echo_err " | abc@123 |"
kv_echo_err " | $ kvdel pass |"
kv_echo_err " | $ kvget pass |"
kv_echo_err " | |"
kv_echo_err " | $ kvclear |"
kv_echo_err " +------------------------------------------------+"
exit 1
}
########################
# PUBLIC FUNCTIONS
########################
## @fn kvget()
## @brief Retrieves value corresponding to specified key
## @param param1 key
##
## Usage: kvget <key>
kvget() {
key="$1"
kv_validate_key "$key" || {
kv_echo_err_box 'invalid param "key"' 'kvget()'
return 1
}
VALUE="$([ -f "$KV_USER_DIR/$key" ] && cat "$KV_USER_DIR/$key")"
echo "$VALUE"
[ "$VALUE" != "" ]
}
## @fn kvset()
## @brief Creates or updates specified key/value pair
## @param param1 key
## @param param2 value
##
## Usage: kvset <key> [value]
kvset() {
key="$1"
value="$2"
kv_validate_key "$key" || {
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"
}
## @fn kvdel()
## @brief Deletes key/value pair corresponding to specified key
## @param param1 key
##
## Usage: kvdel <key>
kvdel() {
key="$1"
kv_validate_key "$key" || {
kv_echo_err_box 'invalid param "key"' 'kvdel()'
return 1
}
test -f "$KV_USER_DIR/$key" && rm -f "$KV_USER_DIR/$key"
}
## @fn kvlist()
## @brief List all key/value pairs to stdout
## @param none
##
## Usage: kvlist
kvlist() {
for i in "$KV_USER_DIR/"*; do
if [ -f "$i" ]; then
key="$(basename "$i")"
echo "$key" "$(kvget "$key")"
fi
done
}
## @fn kvclear()
## @brief Clear all key/value pairs in database
## @param none
##
## Usage: kvclear
kvclear() {
rm -rf "$KV_USER_DIR"
}