#!/usr/bin/env bash # --- # @file_name: jade.sh # @version: 1.3.1 # @description: Lazy script for modifying docker files # @author: Jamie Albert (empty_produce) # @author_contact: # @license: GNU Affero General Public License v3.0 (Included in LICENSE) # Copyright (C) 2025, Jamie Albert # --- set -euo pipefail # --- # shellcheck disable=1091 # --- setup() { . /usr/local/share/dao/libs/libs_dao.sh . /usr/local/share/dao/config/dao.conf } # --- # @return_code: [3] Download failed. # --- download_file() { scp -q "${JADE_REMOTE_HOST}:${JADE_REMOTE_PATH}" "$JADE_LOCAL_PATH" || dao::error 3 'Download failed' dao::info "Downloaded: $JADE_LOCAL_PATH" } # --- # @return_code: [4] Local file missing. # @return_code: [5] Failed to create remote backup. # @return_code: [6] Upload failed. # --- upload_file() { [[ -f "$JADE_LOCAL_PATH" ]] || dao::error 4 "Local file missing: $JADE_LOCAL_PATH" ssh -q "${JADE_REMOTE_HOST}" "[[ -f '${JADE_REMOTE_PATH}' ]] && cp -f '${JADE_REMOTE_PATH}' '${JADE_REMOTE_PATH}.bak'" || dao::error 5 'Failed to create remote backup' scp -q "$JADE_LOCAL_PATH" "${JADE_REMOTE_HOST}:${JADE_REMOTE_PATH}" || dao::error 6 'Upload failed' dao::info "Uploaded: $JADE_LOCAL_PATH" dao::info "Remote file backed up to: ${JADE_REMOTE_PATH}.bak" } # --- # @return_code: [7] JADE_EDITOR command not found. # @return_code: [3] Download failed (inherited from download_file). # --- edit_file() { download_file if ! command -v "$JADE_EDITOR" >/dev/null; then dao::error 7 "JADE_EDITOR not found: '$JADE_EDITOR'" fi "$JADE_EDITOR" "$JADE_LOCAL_PATH" } # --- # @return_code: [6] Upload failed (inherited from upload_file). # @return_code: [8] Remote docker compose up failed. # --- upload_compose() { upload_file ssh -q "${JADE_REMOTE_HOST}" \ "cd '$(dirname "$JADE_REMOTE_PATH")' && exec docker compose up -d --remove-orphans" \ || dao::error 8 'Remote docker compose up failed' dao::info 'Remote docker compose up -d completed' } # --- # @return_code: [9] Remote docker compose down failed. # @return_code: [6] Upload failed (inherited from upload_file). # @return_code: [11] Remote docker compose up failed during restart. # --- upload_restart() { ssh -q "${JADE_REMOTE_HOST}" \ "cd '$(dirname "$JADE_REMOTE_PATH")' && exec docker compose down" \ || dao::error 9 'Remote docker compose down failed' upload_file ssh -q "${JADE_REMOTE_HOST}" \ "cd '$(dirname "$JADE_REMOTE_PATH")' && exec docker compose up -d --remove-orphans" \ || dao::error 11 'Remote docker compose up failed during restart' dao::info 'Remote docker compose restart completed' } # --- # @return_code: [10] Unknown command-line option. # @return_code: [12] Required tool 'scp' not found. # @return_code: [13] Required tool 'ssh' not found. # @return_code: [14] Unexpected execution mode. # @return_code: [N] Errors from called functions (e.g., download_file, upload_file, etc.). # --- main() { setup declare mode='' while [[ $# -gt 0 ]]; do case "$1" in -d) mode='download' ; shift ;; -u) mode='upload' ; shift ;; -uc) mode='up' ; shift ;; -ur) mode='restart' ; shift ;; *) dao::error 10 "Unknown option: $1 (use -d, -u, -uc, -ur)" ;; esac done command -v scp >/dev/null || dao::error 12 "'scp' not found" command -v ssh >/dev/null || dao::error 13 "'ssh' not found" case "$mode" in download) download_file ;; upload) upload_file ;; up) upload_compose ;; restart) upload_restart ;; '') edit_file ;; *) dao::error 14 "Unexpected mode: $mode" ;; esac } main "$@"