|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Supports only yaml syntax database. |
| 3 | +# Support only Yaml syntax database. |
4 | 4 |
|
5 | | -# _db.isKeyExist(key,file) ~ bool |
6 | | -# Checks that key is exist or not. |
7 | | -# |
| 5 | +# _db.isfile(file) -> bool |
| 6 | +# Checks that db file exist or not. |
8 | 7 | # Args: |
9 | | -# - key (str): takes key of db. |
10 | | -# - file (str): takes file path. |
11 | | -_db.isKeyExist(){ |
| 8 | +# file (str) > takes file path. |
| 9 | +_db.isfile(){ |
| 10 | + # checking file exist or not. |
| 11 | + test -f "${1}"; |
| 12 | +} |
| 13 | + |
| 14 | +# _db.iskey(key,file) -> bool |
| 15 | +# Checks that key is exist or not. |
| 16 | +# Args: |
| 17 | +# key (str) > takes key of db. |
| 18 | +# file (str) > takes file path. |
| 19 | +_db.iskey(){ |
12 | 20 | grep "${1}: " "${2}" &>/dev/null; |
13 | 21 | } |
14 | 22 |
|
15 | | -# db.read(key,file) ~ str |
16 | | -# Reads value of given key in db file. |
17 | | -# |
| 23 | +# db.read(key,file) -> str |
| 24 | +# Give you data of key of yaml db. |
18 | 25 | # Args: |
19 | | -# - key (str): takes key of db. |
20 | | -# - file (str): takes file path. |
| 26 | +# key (str) > takes key of db. |
| 27 | +# file (str) > takes file path. |
21 | 28 | db.read(){ |
22 | 29 | # checking args given or not. |
23 | 30 | [[ ${#} -eq 2 ]] || |
24 | 31 | { echo "error: 'missing args'" && return 1; }; |
25 | | - _db.isKeyExist "${1}" "${2}" || |
| 32 | + _db.iskey "${1}" "${2}" || |
26 | 33 | { echo "error: ${1}: 'key not exists'" && return 1; }; |
27 | 34 | local dbValue="$(grep "${1}: " "${2}")"; |
28 | 35 | echo "${dbValue}" | awk -F': ' '{sub(/^[^:]*: /, ""); print}'; |
29 | 36 | } |
30 | 37 |
|
31 | 38 | # db.create(key,value,file) |
32 | | -# Creates data key and value to db. |
33 | | -# |
| 39 | +# Adds data key and value to db. |
34 | 40 | # Args: |
35 | | -# - key (str): takes key of db. |
36 | | -# - value (str): takes value of key. |
37 | | -# - file (str): takes file path. |
| 41 | +# key (str) > takes key of db. |
| 42 | +# value (str) > takes value of key. |
| 43 | +# file (str) > takes file path. |
38 | 44 | db.create(){ |
39 | 45 | # checking args given or not. |
40 | 46 | [[ ${#} -eq 3 ]] || |
41 | 47 | { echo "error: 'missing args'" && return 1; }; |
42 | | - _db.isKeyExist "${1}" "${3}" && |
| 48 | + _db.iskey "${1}" "${3}" && |
43 | 49 | { echo "error: ${1}: 'key already exists'" && return 1; }; |
44 | 50 | echo -ne "\n${1}: ${2}" >> "${3}"; |
45 | 51 | } |
46 | 52 |
|
47 | 53 | # db.update(key,value,file) |
48 | | -# Update data of key in db file. |
49 | | -# |
| 54 | +# Update data of key in db file. |
50 | 55 | # Args: |
51 | | -# - key (str): takes key of db. |
52 | | -# - value (str): takes update value of key. |
53 | | -# - file (str): takes file path. |
| 56 | +# key (str) > takes key of db. |
| 57 | +# value (str) > takes update value of key. |
| 58 | +# file (str) > takes file path. |
54 | 59 | db.update(){ |
55 | 60 | # checking args given or not. |
56 | 61 | [[ ${#} -eq 3 ]] || |
57 | 62 | { echo "error: 'missing args'" && return 1; }; |
58 | | - _db.isKeyExist "${1}" "${3}" || |
| 63 | + _db.iskey "${1}" "${3}" || |
59 | 64 | { echo "error: ${1}: 'key not exists'" && return 1; }; |
| 65 | + # taking key arg. |
60 | 66 | local dbKey="${1}"; |
61 | | - local dbUpdateValue="${2}"; |
| 67 | + # taking update value of given key. |
| 68 | + local dbUpdatedValue="${2}"; |
| 69 | + # taking db file path. |
62 | 70 | local dbFile="${3}"; |
63 | | - local dbCurrentValue="$(db.read "${dbKey}" "${dbFile}")"; |
64 | | - local dbCurrentPair="${dbKey}: ${dbCurrentValue}"; # concated to create pair. |
65 | | - local dbCurrentPairPos="$(grep -n "${dbCurrentPair}" "${dbFile}" | cut -d: -f1 | head -n 1;)"; |
66 | | - local dbUpdatedPair="${dbCurrentPair/${dbCurrentValue}/"${dbUpdateValue}"}"; |
67 | | - sed -i "${dbCurrentPairPos}c\\${dbUpdatedPair}" "${dbFile}"; # replaced full pair. |
| 71 | + # getting old value of given key. |
| 72 | + local dbValue="$(db.read "${dbKey}" "${dbFile}")"; |
| 73 | + # concating key and old value. |
| 74 | + local dbKeyValuePair="${dbKey}: ${dbValue}"; |
| 75 | + # getting position of concated line in file. |
| 76 | + local dbKeyValuePairPos="$(grep -n "${dbKeyValuePair}" "${dbFile}" | cut -d: -f1 | head -n 1;)"; |
| 77 | + # replacing value from old to new. |
| 78 | + local dbUpdatedKeyValuePair="${dbKeyValuePair/${dbValue}/"${dbUpdatedValue}"}"; |
| 79 | + # placing updated value to position. |
| 80 | + sed -i "${dbKeyValuePairPos}c\\${dbUpdatedKeyValuePair}" "${dbFile}"; |
68 | 81 | } |
69 | 82 |
|
70 | 83 | # db.delete(key,file) |
71 | | -# Delete key and value of db file. |
72 | | -# |
| 84 | +# Delete key and value of db file. |
73 | 85 | # Args: |
74 | | -# - key (str): takes key of db. |
75 | | -# - file (str): takes file path. |
| 86 | +# key (str) > takes key of db. |
| 87 | +# file (str) > takes file path. |
76 | 88 | db.delete(){ |
77 | 89 | # checking args given or not. |
78 | 90 | [[ ${#} -eq 2 ]] || |
79 | 91 | { echo "error: 'missing args'" && return 1; }; |
80 | | - _db.isKeyExist "${1}" "${2}" || |
| 92 | + _db.iskey "${1}" "${2}" || |
81 | 93 | { echo "error: ${1}: 'key not exists'" && return 1; }; |
82 | 94 | local dbKey="${1}: "; |
83 | 95 | local dbFile="${2}"; |
|
0 commit comments