mirror of
https://github.com/guezoloic/serverconfig.git
synced 2026-03-28 18:03:49 +00:00
feat: rework entire repo structure
This commit is contained in:
70
utils.sh
Normal file
70
utils.sh
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/bin/bash
|
||||
|
||||
# UTILITY FUNCTIONS & CONFIGURATION
|
||||
# This script acts as a library (utils.sh) for other monitoring scripts.
|
||||
# It handles logging, environment variables, and Telegram notifications.
|
||||
|
||||
PROJECT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
readonly PROJECT_DIR 2>/dev/null
|
||||
|
||||
readonly PROJECT_NAME="serverconfig"
|
||||
|
||||
readonly REQ=("curl" "docker")
|
||||
|
||||
readonly ENV_FILE="${PROJECT_DIR}/.env"
|
||||
|
||||
readonly GREEN='\033[0;32m'
|
||||
readonly RED='\033[0;31m'
|
||||
readonly YELLOW='\033[1;33m'
|
||||
readonly BLUE='\033[0;34m'
|
||||
readonly NC='\033[0m'
|
||||
|
||||
DATETIME_FORMAT="%d-%m-%Y %H:%M:%S"
|
||||
|
||||
function log() {
|
||||
local type="${1}"
|
||||
local color="${2}"
|
||||
local message="${3}"
|
||||
echo -e "${color}[$(date +"$DATETIME_FORMAT")] [${type}]${NC} ${message}"
|
||||
}
|
||||
|
||||
function log_info() { log "INFO" "$BLUE" "$1"; }
|
||||
function log_success() { log "OK " "$GREEN" "$1"; }
|
||||
function log_error() { log "ERR " "$RED" "$1" >&2; }
|
||||
function log_warn() { log "WARN " "$YELLOW" "$1" >&2; }
|
||||
|
||||
# USING TELEGRAM (may change later)
|
||||
function 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"
|
||||
}
|
||||
|
||||
function env_variable() {
|
||||
source $ENV_FILE
|
||||
|
||||
local key="$1"
|
||||
local value="$2"
|
||||
|
||||
if [[ -z "$value" ]]; then
|
||||
log_warn "$key not set."
|
||||
fi
|
||||
|
||||
if grep -Eq "^${key}=" "$ENV_FILE" 2>/dev/null; then
|
||||
read -p "$key already set, overwrite? (y/N): " yn
|
||||
case "$yn" in
|
||||
[yY]*)
|
||||
sed -i "s/^$key=.*/$key=$value/" "$ENV_FILE"
|
||||
log_success "$key updated."
|
||||
;;
|
||||
*)
|
||||
log_info "$key not changed."
|
||||
;;
|
||||
esac
|
||||
else
|
||||
echo "$key=$value" >> "$ENV_FILE"
|
||||
log_success "$key created."
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user