From 86c40e03f8dc54ee7cb6e4a3fe3e7d2ccf1a71ac Mon Sep 17 00:00:00 2001 From: guezoloic Date: Mon, 8 Sep 2025 18:28:17 +0000 Subject: [PATCH] feat: add isUpdated and cron error statement - isUpdated (create_env_variable common.sh) to not propose overwrite field - if cron does not exist, print error (log too) --- libs/common.sh | 37 ++++++++++++++++++++++--------------- scripts/aws-backup.sh | 9 +++++++-- scripts/disk-monitor.sh | 2 +- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/libs/common.sh b/libs/common.sh index b34a59c..bf704c3 100644 --- a/libs/common.sh +++ b/libs/common.sh @@ -50,8 +50,13 @@ create_env_variable() { local key="$1" local value="$2" local file="${3:-$ENV_FILE}" + local isUpdated="${4:-true}" - if [[ -z "$value" ]]; then + if [[ "$file" == '--' ]]; then + file=$ENV_FILE + fi + + if [[ -z "$value" && "$isUpdated" == true ]]; then if grep -q "^$key=*" "$file" 2>/dev/null; then info_print "$key not updated." $WARN_FLAG return @@ -61,21 +66,23 @@ create_env_variable() { fi fi - if grep -Eq "^${key}=" "$file" 2>/dev/null; then - read -p "$key already set, overwrite? (y/N): " yn + if [[ "$isUpdated" == true ]]; then + if grep -Eq "^${key}=" "$file" 2>/dev/null; then + read -p "$key already set, overwrite? (y/N): " yn - case "$yn" in - [Yy]*) - sed -i "s/^$key=.*/$key=$value/" "$file" - info_print "$key updated." $SUCCESS_FLAG - ;; - *) - info_print "$key not changed." $WARN_FLAG - ;; - esac - else - echo "$key=$value" >> "$file" - info_print "$key set." $SUCCESS_FLAG + case "$yn" in + [Yy]*) + sed -i "s/^$key=.*/$key=$value/" "$file" + info_print "$key updated." $SUCCESS_FLAG + ;; + *) + info_print "$key not changed." $WARN_FLAG + ;; + esac + else + echo "$key=$value" >> "$file" + info_print "$key set." $SUCCESS_FLAG + fi fi } diff --git a/scripts/aws-backup.sh b/scripts/aws-backup.sh index 86ce467..d537f46 100644 --- a/scripts/aws-backup.sh +++ b/scripts/aws-backup.sh @@ -22,8 +22,8 @@ if [[ "--install" == $INSTALLED ]]; then info_print "AWS configuration." aws configure - create_env_variable AWS_ACCESS_KEY_ID $(aws configure get aws_access_key_id) - create_env_variable AWS_SECRET_ACCESS_KEY $(aws configure get aws_secret_access_key) + create_env_variable AWS_ACCESS_KEY_ID $(aws configure get aws_access_key_id) -- false + create_env_variable AWS_SECRET_ACCESS_KEY $(aws configure get aws_secret_access_key) -- false touch "$BACKUP" info_print "$BACKUP created." @@ -35,6 +35,11 @@ if [[ "--install" == $INSTALLED ]]; then done info_print "You can add more names later by editing $BACKUP." + if ! command -v crontab >/dev/null 2>&1; then + info_print "Error: crontab not found." $ERROR_FLAG + exit 1 + fi + CRON_JOB="0 0 * * * $SCRIPT_FILE/scripts/aws-backup.sh" crontab -l | grep -F "$CRON_JOB" > /dev/null 2>&1 if ! crontab -l | grep -Fq "$CRON_JOB"; then diff --git a/scripts/disk-monitor.sh b/scripts/disk-monitor.sh index 727f177..3d7ea84 100644 --- a/scripts/disk-monitor.sh +++ b/scripts/disk-monitor.sh @@ -10,7 +10,7 @@ if [[ "--install" == $INSTALLED ]]; then disk-monitor Installation\n\ --------------------------------------------------" if ! command -v crontab >/dev/null 2>&1; then - info_print "Cron job added." $ERROR_FLAG + info_print "Error: crontab not found." $ERROR_FLAG exit 1 fi