#!/usr/bin/env bash # Firefly III Data Importer - trigger FIDI directory auto-import # POSTs to FIDI /autoimport with the container-side import directory path. # FIDI scans that directory for JSON+CSV pairs and imports them all. # # Usage: ./import.sh [--dry-run|-n] set -euo pipefail # --------------------------------------------------------------------------- # Args # --------------------------------------------------------------------------- DRY_RUN=false for arg in "$@"; do case "$arg" in --dry-run|-n) DRY_RUN=true ;; *) echo "Unknown argument: $arg"; exit 1 ;; esac done # --------------------------------------------------------------------------- # Config — override via environment or .env file # --------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" if [[ -f "$SCRIPT_DIR/.env" ]]; then # shellcheck source=/dev/null source "$SCRIPT_DIR/.env" fi FIDI_URL="${FIDI_URL:?Set FIDI_URL in .env or environment (e.g. http://localhost:8080)}" FIDI_SECRET="${FIDI_SECRET:-}" FIDI_ACCESS_TOKEN="${FIDI_ACCESS_TOKEN:-}" # Path to the imports directory as seen by FIDI inside its container FIDI_IMPORT_DIR="${FIDI_IMPORT_DIR:-/imports}" # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' pass() { echo -e "${GREEN}[OK]${NC} $*"; } fail() { echo -e "${RED}[FAIL]${NC} $*"; } info() { echo -e "${YELLOW}[INFO]${NC} $*"; } dry() { echo -e "${CYAN}[DRY]${NC} $*"; } build_url() { local url="${FIDI_URL%/}/autoimport" local qs="directory=${FIDI_IMPORT_DIR}" [[ -n "$FIDI_SECRET" ]] && qs="${qs}&secret=${FIDI_SECRET}" echo "${url}?${qs}" } # --------------------------------------------------------------------------- # Main # --------------------------------------------------------------------------- url="$(build_url)" if $DRY_RUN; then info "Dry-run mode — no requests will be sent" dry "POST ${url//secret=*/secret=***}" [[ -n "$FIDI_ACCESS_TOKEN" ]] && dry "auth → Bearer ***" exit 0 fi info "Triggering FIDI auto-import from ${FIDI_IMPORT_DIR}..." curl_args=(-s -w "\n%{http_code}" -X POST) [[ -n "$FIDI_ACCESS_TOKEN" ]] && curl_args+=(-H "Authorization: Bearer $FIDI_ACCESS_TOKEN") curl_args+=("$url") response=$(curl "${curl_args[@]}") http_code=$(echo "$response" | tail -n1) body=$(echo "$response" | head -n -1) echo "" echo "----------------------------------------" if [[ "$http_code" =~ ^2 ]]; then pass "Import triggered (HTTP $http_code)" echo " Response: $body" echo "----------------------------------------" exit 0 else fail "Import failed (HTTP $http_code)" echo " Response: $body" echo "----------------------------------------" exit 1 fi