first commit
This commit is contained in:
142
import.sh
Normal file
142
import.sh
Normal file
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env bash
|
||||
# Firefly III Data Importer - batch auto-import
|
||||
# Pairs .json + .csv files by base name and POSTs each to FIDI /autoimport
|
||||
#
|
||||
# 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:-}"
|
||||
IMPORT_DIR="${IMPORT_DIR:-$SCRIPT_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"
|
||||
[[ -n "$FIDI_SECRET" ]] && url="${url}?secret=${FIDI_SECRET}"
|
||||
echo "$url"
|
||||
}
|
||||
|
||||
import_pair() {
|
||||
local json_file="$1"
|
||||
local csv_file="$2"
|
||||
local base
|
||||
base="$(basename "$json_file" .json)"
|
||||
local url
|
||||
url="$(build_url)"
|
||||
|
||||
if $DRY_RUN; then
|
||||
dry "$base"
|
||||
dry " POST $url"
|
||||
dry " csv → $csv_file"
|
||||
dry " json → $json_file"
|
||||
[[ -n "$FIDI_ACCESS_TOKEN" ]] && dry " auth → Bearer ***"
|
||||
return 0
|
||||
fi
|
||||
|
||||
info "Importing: $base"
|
||||
|
||||
# Build auth args as an array to safely handle spaces/special chars
|
||||
local curl_args=(-s -w "\n%{http_code}")
|
||||
[[ -n "$FIDI_ACCESS_TOKEN" ]] && curl_args+=(-H "Authorization: Bearer $FIDI_ACCESS_TOKEN")
|
||||
curl_args+=(
|
||||
-F "csv=@${csv_file};type=text/csv"
|
||||
-F "json=@${json_file};type=application/json"
|
||||
"$url"
|
||||
)
|
||||
|
||||
local response http_code body
|
||||
response=$(curl "${curl_args[@]}")
|
||||
http_code=$(echo "$response" | tail -n1)
|
||||
body=$(echo "$response" | head -n -1)
|
||||
|
||||
if [[ "$http_code" =~ ^2 ]]; then
|
||||
pass "$base (HTTP $http_code)"
|
||||
return 0
|
||||
else
|
||||
fail "$base (HTTP $http_code)"
|
||||
echo " Response: $body"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Main
|
||||
# ---------------------------------------------------------------------------
|
||||
if [[ ! -d "$IMPORT_DIR" ]]; then
|
||||
echo "Import directory not found: $IMPORT_DIR"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
$DRY_RUN && info "Dry-run mode — no requests will be sent"
|
||||
|
||||
mapfile -t json_files < <(find "$IMPORT_DIR" -maxdepth 1 -name '*.json' | sort)
|
||||
|
||||
if [[ ${#json_files[@]} -eq 0 ]]; then
|
||||
info "No .json files found in $IMPORT_DIR"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
success=0
|
||||
skipped=0
|
||||
failed=0
|
||||
|
||||
for json_file in "${json_files[@]}"; do
|
||||
base="$(basename "$json_file" .json)"
|
||||
csv_file="${IMPORT_DIR}/${base}.csv"
|
||||
|
||||
if [[ ! -f "$csv_file" ]]; then
|
||||
info "Skipping $base — no matching .csv found"
|
||||
((skipped++))
|
||||
continue
|
||||
fi
|
||||
|
||||
if import_pair "$json_file" "$csv_file"; then
|
||||
((success++))
|
||||
else
|
||||
((failed++))
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "----------------------------------------"
|
||||
$DRY_RUN && echo " (dry run — nothing was imported)"
|
||||
echo " Done: ${success} succeeded, ${failed} failed, ${skipped} skipped"
|
||||
echo "----------------------------------------"
|
||||
|
||||
[[ "$failed" -eq 0 ]]
|
||||
Reference in New Issue
Block a user