]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/lib/kvs.sh
script/makepkg: exclude current port from port_available_deps; sort output too
[CDN/Mosi.git] / script / lib / kvs.sh
1 # Simple bourne shell based key-value store
2
3 if [ -z "${__kvs_sh_loaded}" ]
4 then
5   __kvs_sh_loaded=yes
6
7 t="$(printf '\t')"
8
9 [ "${kvs}" ] || {
10   cat <<EOF
11 KVS v0.1
12 Copyright - http://wiki.cyberleo.net/wiki/CyberLeo/COPYRIGHT?version=4
13
14 Set the following variables before sourcing this library:
15
16  kvs  - Path and filename of the kvdb database file
17 EOF
18   kill $$
19   exit 1
20 }
21
22
23 [ -s "${kvs}" ] || echo "${t}${t}# KVSv0 id-key-value database" > "${kvs}"
24
25 # Add or replace a value stored for a given key and id in the kvs
26 kvs_set() {
27   [ "${1}" -a "${2}" ] || return 255
28   local id="${1}"
29   local var="${2}"
30   local val="${3}"
31   printf "%s\t%s\t%s\n" "${id}" "${var}" "${val}" >> "${kvs}"
32 }
33
34 # Fetch the most recent value stored for a given key and id from the kvs
35 kvs_get() {
36   [ "${1}" -a "${2}" ] || return 255
37   local id="${1}"
38   local var="${2}"
39   grep "^${id}${t}${var}${t}" "${kvs}" | tail -n 1 | cut -d"${t}" -f3-
40 }
41
42 # Does the kvs have any entries for a given ID?
43 kvs_has_id() {
44   [ "${1}" ] || return 255
45   local id="${1}"
46   grep -q "^${id}${t}" "${kvs}"
47 }
48
49 # Does the kvs have a given key for a given ID?
50 kvs_has_key() {
51   [ "${1}" -a "${2}" ] || return 255
52   local id="${1}"
53   local var="${2}"
54   grep -q "^${id}${t}${var}${t}" "${kvs}"
55 }
56
57 # Remove a given key with a given ID from the kvs
58 kvs_unset() {
59   [ "${1}" -a "${2}" ] || return 255
60   local id="${1}"
61   local var="${2}"
62   grep -v "^${id}${t}${var}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"
63 }
64
65 # Remove all keys with a given ID from the kvs
66 kvs_unset_all() {
67   [ "${1}" ] || return 255
68   local id="${1}"
69   grep -v "^${id}${t}" "${kvs}" > "${kvs}.tmp" && mv -f "${kvs}.tmp" "${kvs}" || rm -f "${kvs}.tmp"
70 }
71
72 fi