Skip to content

Commit

Permalink
code shorten
Browse files Browse the repository at this point in the history
  • Loading branch information
OurCodeBase committed Jan 19, 2024
1 parent 81600d2 commit 5407b89
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 75 deletions.
100 changes: 37 additions & 63 deletions src/db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,12 @@ _db.iskey(){
# file (str) > takes file path.
db.read(){
# checking args given or not.
if [[ ! ${#} -eq 2 ]]; then
echo "error: 'missing args'";
return 1;
fi
if _db.iskey "${1}" "${2}"; then
local dbValue;
dbValue="$(grep "${1}: " "${2}")";
echo "${dbValue}" | awk -F': ' '{sub(/^[^:]*: /, ""); print}';
return 0;
else
echo "error: ${1}: 'key not exists'";
return 1;
fi
[[ ${#} -eq 2 ]] ||
{ echo "error: 'missing args'" && return 1; };
_db.iskey "${1}" "${2}" ||
{ echo "error: ${1}: 'key not exists'" && return 1; };
local dbValue="$(grep "${1}: " "${2}")";
echo "${dbValue}" | awk -F': ' '{sub(/^[^:]*: /, ""); print}';
}

# db.create(key,value,file)
Expand All @@ -50,17 +43,11 @@ db.read(){
# file (str) > takes file path.
db.create(){
# checking args given or not.
if [[ ! ${#} -eq 3 ]]; then
echo "error: 'missing args'";
return 1;
fi
if _db.iskey "${1}" "${3}"; then
echo "error: ${1}: 'key already exist'";
return 1;
else
echo -ne "\n${1}: ${2}" >> "${3}";
return 0;
fi
[[ ${#} -eq 3 ]] ||
{ echo "error: 'missing args'" && return 1; };
_db.iskey "${1}" "${3}" &&
{ echo "error: ${1}: 'key already exists'" && return 1; };
echo -ne "\n${1}: ${2}" >> "${3}";
}

# db.update(key,value,file)
Expand All @@ -71,33 +58,26 @@ db.create(){
# file (str) > takes file path.
db.update(){
# checking args given or not.
if [[ ! ${#} -eq 3 ]]; then
echo "error: 'missing args'";
return 1;
fi
if _db.iskey "${1}" "${3}"; then
# taking key arg.
local dbKey="${1}";
# taking update value of given key.
local dbUpdatedValue="${2}";
# taking db file path.
local dbFile="${3}";
# getting old value of given key.
local dbValue;
dbValue="$(db.read "${dbKey}" "${dbFile}")";
# concating key and old value.
local dbKeyValuePair="${dbKey}: ${dbValue}";
# getting position of concated line in file.
local dbKeyValuePairPos;
dbKeyValuePairPos="$(grep -n "${dbKeyValuePair}" "${dbFile}" | cut -d: -f1 | head -n 1;)";
# replacing value from old to new.
local dbUpdatedKeyValuePair="${dbKeyValuePair/${dbValue}/"${dbUpdatedValue}"}";
# placing updated value to position.
sed -i "${dbKeyValuePairPos}c\\${dbUpdatedKeyValuePair}" "${dbFile}";
else
echo "error: ${1}: 'key not exists'";
return 1;
fi
[[ ${#} -eq 3 ]] ||
{ echo "error: 'missing args'" && return 1; };
_db.iskey "${1}" "${3}" ||
{ echo "error: ${1}: 'key not exists'" && return 1; };
# taking key arg.
local dbKey="${1}";
# taking update value of given key.
local dbUpdatedValue="${2}";
# taking db file path.
local dbFile="${3}";
# getting old value of given key.
local dbValue="$(db.read "${dbKey}" "${dbFile}")";
# concating key and old value.
local dbKeyValuePair="${dbKey}: ${dbValue}";
# getting position of concated line in file.
local dbKeyValuePairPos="$(grep -n "${dbKeyValuePair}" "${dbFile}" | cut -d: -f1 | head -n 1;)";
# replacing value from old to new.
local dbUpdatedKeyValuePair="${dbKeyValuePair/${dbValue}/"${dbUpdatedValue}"}";
# placing updated value to position.
sed -i "${dbKeyValuePairPos}c\\${dbUpdatedKeyValuePair}" "${dbFile}";
}

# db.delete(key,file)
Expand All @@ -107,18 +87,12 @@ db.update(){
# file (str) > takes file path.
db.delete(){
# checking args given or not.
if [[ ! ${#} -eq 2 ]]; then
echo "error: 'missing args'";
return 1;
fi
[[ ${#} -eq 2 ]] ||
{ echo "error: 'missing args'" && return 1; };
_db.iskey "${1}" "${2}" ||
{ echo "error: ${1}: 'key not exists'" && return 1; };
local dbKey="${1}: ";
local dbFile="${2}";
if _db.iskey "${1}" "${2}"; then
local dbKeyPos;
dbKeyPos="$(grep -n "${dbKey}" "${dbFile}" | cut -d: -f1 | head -n 1;)";
sed -i "${dbKeyPos}d" "${dbFile}";
else
echo "error: ${1}: 'key not exists'";
return 1;
fi
local dbKeyPos="$(grep -n "${dbKey}" "${dbFile}" | cut -d: -f1 | head -n 1;)";
sed -i "${dbKeyPos}d" "${dbFile}";
}
12 changes: 4 additions & 8 deletions src/file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,8 @@ file.append.hori(){
# takes file as arg.
local ARGFile="${3}";
# checking args given or not.
if [[ ! ${#} -eq 3 ]]; then
echo "error: 'missing args'";
return 1;
fi
[[ ${#} -eq 3 ]] ||
{ echo "error: 'missing args'" && return 1; };
# reading line from given position.
local Vir="$(file.readline "${Pos}" "${ARGFile}")";
# concating string to it.
Expand All @@ -220,10 +218,8 @@ file.append.vert(){
# takes file as arg.
local ARGFile="${3}";
# checking args given or not.
if [[ ! ${#} -eq 3 ]]; then
echo "error: 'missing args'";
return 1;
fi
[[ ${#} -eq 3 ]] ||
{ echo "error: 'missing args'" && return 1; };
# reduce one from variable.
local Pos="$(( Pos - 1 ))";
# do operation.
Expand Down
6 changes: 2 additions & 4 deletions src/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ source "${Dir}"/os.sh
# pkg.size(install,package) > Gives you package installed size.
pkg.size(){
# checking args given or not.
if [[ ! ${#} -eq 2 ]]; then
echo "error: 'missing args'";
return 1;
fi
[[ ${#} -eq 2 ]] ||
{ echo "error: 'missing args'" && return 1; };
case "${1}" in
'dnload') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Download-Size:')";;
'install') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Installed-Size:')";;
Expand Down

0 comments on commit 5407b89

Please sign in to comment.