From d8276ba9d64ef82d2c9bcfed34dd21b29ffe62ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Wed, 30 Jul 2025 21:11:40 +0200 Subject: [PATCH] feat: move common and notifications to libs folder --- install.sh | 17 +++++----- libs/common.sh | 75 +++++++++++++++++++++++++++++++++++++++++++ libs/notifications.sh | 28 ++++++++++++++++ 3 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 libs/common.sh create mode 100644 libs/notifications.sh diff --git a/install.sh b/install.sh index 331c0f1..63aa78e 100644 --- a/install.sh +++ b/install.sh @@ -1,6 +1,6 @@ #!/bin/bash -source ./scripts/common.sh +source ./libs/common.sh mkdir -p $ETC_DIR rm -f $LOG @@ -75,16 +75,17 @@ info_print "\n\ Installing scripts to $SCRIPT_FILE \n\ --------------------------------------------------" -- false -for scripts in scripts/*.sh; do - filename=$(basename "$scripts") - info_print "Moving $filename to $SCRIPT_FILE" +for scripts in libs/*.sh scripts/*.sh; do + info_print "Moving $scripts to $SCRIPT_FILE" + output="$SCRIPT_FILE/$scripts" - install $argument "$scripts" "$SCRIPT_FILE/$filename" -Dm755 \ - && { info_print "$SCRIPT_FILE/$filename installed." 6; } \ - || { info_print "$SCRIPT_FILE/$filename failed." 3; ISERROR=true; } + install $argument "$scripts" $output -Dm755 \ + && { info_print "$output installed." 6; } \ + || { info_print "$output failed." 3; ISERROR=true; } done -for element in $SCRIPT_FILE/*.sh; do +touch $ENV_FILE +for element in $SCRIPT_FILE/*/*.sh; do bash "$element" --install done diff --git a/libs/common.sh b/libs/common.sh new file mode 100644 index 0000000..b8a092f --- /dev/null +++ b/libs/common.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +FILENAME="serverconfig" + +ETC_DIR="/etc/$FILENAME" +ENV_FILE="$ETC_DIR/.env" +LOG="/var/log/$FILENAME.log" +SCRIPT_FILE="/usr/local/bin" + +INFO="\e[34mINFO\e[0m" +SUCCESS="\e[32mSUCCESS\e[0m" +WARN="\e[33mWARN\e[0m" +ERROR="\e[31mERROR\e[0m" +DEBUG="\e[35mDEBUG\e[0m" +ACTION="\e[36mACTION\e[0m" + +DATETIME_FORMAT="%d-%m-%Y %H:%M:%S" + +info_print() { + local message="$1" + local level="${2:-1}" + local write_log="${3:-true}" + + case $level in + 1|--) local level=$INFO;; + 2) local level=$WARN;; + 3) local level=$ERROR;; + 4) local level=$DEBUG;; + 5) local level=$ACTION;; + 6) local level=$SUCCESS;; + *);; + esac + + local output="[$(date +"$DATETIME_FORMAT")] - $level: $message" + + if [ "$write_log" = true ]; then echo -e "$output" | tee -a "$LOG" + else echo -e "$output" + fi +} + +create_env_variable() { + local key="$1" + local value="$2" + local file="${3:-$ENV_FILE}" + + if [[ -z "$value" ]]; then + if grep -q "^$key=*" "$file" 2>/dev/null; then + info_print "$key not updated." + return + else + info_print "$key not set (empty input)." + return + fi + fi + + if grep -q "^$key=" "$file" 2>/dev/null; then + if $AUTO_CONFIRM; then + yn="y" + else + read -p "$key already set, overwrite? (y/N): " yn + fi + case "$yn" in + [Yy]*) + sed -i "s/^$key=.*/$key=$value/" "$file" + info_print "$key updated." + ;; + *) + info_print "$key not changed." + ;; + esac + else + echo "$key=$value" >> "$file" + info_print "$key \e[32mset\e[0m." + fi +} \ No newline at end of file diff --git a/libs/notifications.sh b/libs/notifications.sh new file mode 100644 index 0000000..f56b51a --- /dev/null +++ b/libs/notifications.sh @@ -0,0 +1,28 @@ +source /usr/local/bin/libs/common.sh +source /etc/serverconfig/.env + +INSTALLED=$1 + +if [[ "--install" == $INSTALLED ]]; then + info_print "\n\ +==================================================\n\ + notifications Installation\n\ +--------------------------------------------------" + + ENV_LIST=("TELEGRAM_CHAT_ID" "TELEGRAM_TOKEN") + + for env in "${ENV_LIST[@]}"; do + read -p "Enter value for $env: " value + create_env_variable "$env" "$value" + done + + exit 0 +fi + +send_notification() { + local message="$1" + curl -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" \ + -d "chat_id=$TELEGRAM_CHAT_ID" \ + -d "text=$message" \ + -d "parse_mode=HTML" +} \ No newline at end of file