-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv-bash
executable file
·156 lines (141 loc) · 4.25 KB
/
kv-bash
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
#!/usr/bin/env bash
# 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
########################
# 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" != "" ]
}
# 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"
}
# 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"
}
# list all key/value pairs to stdout
# Usage: kvlist
kvlist() {
for i in "$KV_USER_DIR/"*; do
if [ -f "$i" ]; then
key="$(basename "$i")"
if((key!="count"))
then
not_empty=1
echo "$key" "$(kvget "$key")"
fi
fi
if [[ 1 -ne not_empty ]]
then
echo "Do you really have nothing to do today?"
return 6 #for todo's update function
fi
done
}
# clear all key/value pairs in database
# Usage: kvclear
kvclear() {
rm -rf "$KV_USER_DIR"
}