diff --git a/tasks/module_4/task-2/data/users.db b/tasks/module_4/task-2/data/users.db new file mode 100644 index 0000000..d9ad375 --- /dev/null +++ b/tasks/module_4/task-2/data/users.db @@ -0,0 +1,4 @@ +baer,baer +temp,temp +asd,qweq +qweqweqwe,qweqweqwe diff --git a/tasks/module_4/task-2/scripts/db.sh b/tasks/module_4/task-2/scripts/db.sh new file mode 100755 index 0000000..40b6ef3 --- /dev/null +++ b/tasks/module_4/task-2/scripts/db.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +source "./helpers/help_db.sh" +source "./helpers/add_user_db.sh" +source "./helpers/backup_db.sh" +source "./helpers/restore_db.sh" +source "./helpers/list_users_db.sh" +source "./helpers/find_user_db.sh" + +DB_FILE_PATH="../data/users.db" +USERNAME_OR_INVERSE_FLAG=$2; + +case $1 in + add) add "$DB_FILE_PATH" ;; + backup) backup "$DB_FILE_PATH" ;; + restore) restore "$DB_FILE_PATH" ;; + find) find "$DB_FILE_PATH" "$USERNAME_OR_INVERSE_FLAG" ;; + list) list "$DB_FILE_PATH" "$USERNAME_OR_INVERSE_FLAG" ;; + help | '' | *) help ;; +esac \ No newline at end of file diff --git a/tasks/module_4/task-2/scripts/helpers/add_user_db.sh b/tasks/module_4/task-2/scripts/helpers/add_user_db.sh new file mode 100755 index 0000000..6013fdf --- /dev/null +++ b/tasks/module_4/task-2/scripts/helpers/add_user_db.sh @@ -0,0 +1,25 @@ +# add_user_db.sh + +add() { + local _DB_FILE_PATH="$1" + while true; do + read -p "Type the username (latin letters only) of a new entity: " _username + if [[ "$_username" =~ ^[a-zA-Z]+$ ]]; then + break + else + echo "Incorrect input, latin letters only" + exit 1 + fi + done + while true; do + read -p "Type a role (latin letters only): " _role + if [[ "$_role" =~ ^[a-zA-Z]+$ ]]; then + break + else + echo "Incorrect iput, litin letters only" + exit 1 + fi + done + echo "$_username,$_role" >> "$_DB_FILE_PATH" + echo "Data saved to $_DB_FILE_PATH" +} \ No newline at end of file diff --git a/tasks/module_4/task-2/scripts/helpers/backup_db.sh b/tasks/module_4/task-2/scripts/helpers/backup_db.sh new file mode 100644 index 0000000..086137e --- /dev/null +++ b/tasks/module_4/task-2/scripts/helpers/backup_db.sh @@ -0,0 +1,9 @@ +# backup_db.sh + +backup() { + local DB_FILE_PATH="$1" + local current_date=$(date +'%Y-%m-%d') + local backup_file="../data/$current_date-users.db.backup" + cp "$DB_FILE_PATH" "$backup_file" + echo "Backup created as $backup_file" +} \ No newline at end of file diff --git a/tasks/module_4/task-2/scripts/helpers/find_user_db.sh b/tasks/module_4/task-2/scripts/helpers/find_user_db.sh new file mode 100644 index 0000000..b724137 --- /dev/null +++ b/tasks/module_4/task-2/scripts/helpers/find_user_db.sh @@ -0,0 +1,22 @@ +#find_user_db.sh + +function find { + local DB_FILE_PATH="$1" + if [ -n "$2" ]; then + local USERNAME_TO_FIND="$2" + if [ -f "$DB_FILE_PATH" ]; then + found_entries=$(awk -v username="$USERNAME_TO_FIND" -F',' '$1 == username' "$DB_FILE_PATH") + if [ -n "$found_entries" ]; then + echo "$found_entries" + else + echo "User not found." + fi + else + echo "Database file not found." + exit 1 + fi + else + echo "Usage: db.sh find " + exit 1 + fi +} \ No newline at end of file diff --git a/tasks/module_4/task-2/scripts/helpers/help_db.sh b/tasks/module_4/task-2/scripts/helpers/help_db.sh new file mode 100644 index 0000000..7532c1a --- /dev/null +++ b/tasks/module_4/task-2/scripts/helpers/help_db.sh @@ -0,0 +1,32 @@ +function help { + echo "Manages users in db. It accepts a single parameter with a command name." + echo + echo "Syntax: db.sh [command]" + echo + echo "List of available commands:" + echo + echo "add Adds a new line to the users.db. Script must prompt user to type a + username of new entity. After entering username, user must be prompted to + type a role." + echo "backup Creates a new file, named" $filePath".backup which is a copy of + current" $fileName + echo "restore Restores the database from a previously created backup. If a backup file + with the current date exists, it will be used to overwrite the current + database. If no backup file is found, the script will inform you that no + backup file is available for restoration." + echo "find Prompts user to type a username, then prints username and role if such + exists in users.db. If there is no user with selected username, script must print: + “User not found”. If there is more than one user with such username, print all + found entries." + echo "list Prints the contents of users.db in the format: N. username, role + where N is the line number of an actual record. By default, it lists the records + in ascending order. You can use the additional optional parameter --inverse to + reverse the order and list the records from bottom to top. For example, running + the command "\$ db.sh list --inverse" will return the result as follows: + + \`$ db.sh list --inverse\` + + 10. John, admin + 9. Valerie, user + 8. Ghost, guest" +} \ No newline at end of file diff --git a/tasks/module_4/task-2/scripts/helpers/list_users_db.sh b/tasks/module_4/task-2/scripts/helpers/list_users_db.sh new file mode 100644 index 0000000..053a3c9 --- /dev/null +++ b/tasks/module_4/task-2/scripts/helpers/list_users_db.sh @@ -0,0 +1,11 @@ +#list_users_db.sh + +function list () { + local _DB_FILE_PATH="$1" + local _INVERSE_FLAG="$2" + if [ "$_INVERSE_FLAG" == "--inverse" ]; then + awk -F',' '{data[NR] = $1 ", " $2} END {for (i = NR; i >= 1; --i) print i ". " data[i]}' "$_DB_FILE_PATH" + else + awk '{print NR ". " $1 ", " $2}' FS=',' "$_DB_FILE_PATH" + fi +} \ No newline at end of file diff --git a/tasks/module_4/task-2/scripts/helpers/restore_db.sh b/tasks/module_4/task-2/scripts/helpers/restore_db.sh new file mode 100644 index 0000000..728fb0b --- /dev/null +++ b/tasks/module_4/task-2/scripts/helpers/restore_db.sh @@ -0,0 +1,13 @@ +# restore_db.sh + +restore() { + local _DB_FILE_PATH="$1" + current_date=$(date +'%Y-%m-%d') + backup_file="../data/$current_date-users.db.backup" + if [ -f "$backup_file" ]; then + cp "$backup_file" "$_DB_FILE_PATH" + echo "Restored from $backup_file" + else + echo "No backup file found for restoration." + fi +} \ No newline at end of file