#!/usr/bin/env bash set -euo pipefail PERSON="$1" HOST="alps:3222" PROTOCOL="http" REPO="babbarc/workspaces" BRANCH="master" LOG_FILE="/tmp/.gitops-router-${PERSON}.log" log() { local level="${1^^}" # convert to uppercase shift echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $*" | tee -a "$LOG_FILE" } log info "Received SSH_ORIGINAL_COMMAND: $SSH_ORIGINAL_COMMAND" # Ensure the variable is set if [[ -z "${SSH_ORIGINAL_COMMAND:-}" ]]; then log error "No SSH_ORIGINAL_COMMAND provided." exit 1 fi geturl() { echo "$PROTOCOL://$HOST/$REPO/$1/branch/$BRANCH/$2" } function run() { "$HOME"/.local/bin/"$1" } function update() { type=${4:-raw} fname=$(basename "$1") output_path="$HOME/$2/$fname" url=$(geturl "$type" "$1") [ -f "$output_path" ] && chmod 700 "$output_path" curl -fsSL "$url" -o "$output_path" && log info "Downloaded $url to $output_path" chmod "$3" "$output_path" } clean_images() { # Get list of image IDs with tag (dangling images) dangling_images=$(podman images -f "dangling=true" -q) if [ -z "$dangling_images" ]; then echo "✅ No dangling images to remove." else echo "⚠️ Removing dangling images..." echo "$dangling_images" | xargs podman rmi echo "🧹 Done!" fi } # Strip arguments and parse command read -r command args <<<"$SSH_ORIGINAL_COMMAND" # Define command routing case "$command" in build) case "$args" in base) run build-base.sh ;; workspace) run build-workspace.sh ;; *) log error "Invalid arguments for build command: $args" ;; esac ;; update) case "$args" in workspace) update build-workspace.sh .local/bin 500 ;; base) update build-base.sh .local/bin 500 ;; access) update access.yml . 400 ;; ssh_router) update ssh_router.sh .local/bin 500 ;; gitops_router) update gitops_router.sh .local/bin 500 ;; home_tar) update home.tar.gz . 500 media ;; *) log error "Invalid arguments for update command: $args" ;; esac ;; clean) clean_images ;; status) podman images ;; remove) podman rm "$args" ;; *) log error "Unknown command: $command" exit 127 ;; esac