Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
.env.*
!.env.example
.DS_Store
# Temp files left by an interrupted secret write, e.g. ..env.lab-tmp-123
.*.lab-tmp-*
# Temp files left by an interrupted secret write, e.g. ..env.jdg-cli-tmp-123
.*.jdg-cli-tmp-*
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "jdg-cli"
version = "0.1.0"
edition = "2024"
description = "LAB (Lazy Accounting Buddy): agent-friendly accounting reconciliation CLI and MCP server"
description = "jdg-cli: agent-friendly invoice reconciliation CLI and MCP server (KSeF, Gmail, SaldeoSMART)"
license = "MIT"

[dependencies]
Expand Down
134 changes: 71 additions & 63 deletions README.md

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions mcp/jdg-cli-mcp.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"mcpServers": {
"jdg-cli-mcp": {
"command": "jdg-cli",
"args": [
"--db",
"/path/to/jdg-cli.sqlite",
"mcp"
],
"cwd": "/path/to/jdg-cli"
}
}
}
13 changes: 0 additions & 13 deletions mcp/lab-mcp.example.json

This file was deleted.

58 changes: 27 additions & 31 deletions scripts/install-launchd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
LABEL="${LAB_LAUNCHD_LABEL:-com.rafalw.lab.automation}"
LABEL="${JDG_LAUNCHD_LABEL:-com.rafalw.jdg-cli.automation}"
PLIST="$HOME/Library/LaunchAgents/$LABEL.plist"
# LAB_BIN and LAB_YEAR go into the plist only when set explicitly; otherwise
# lab-automation.sh resolves the binary and the year(s) on every run.
LAB_BIN_OVERRIDE="${LAB_BIN:-}"
YEAR_OVERRIDE="${LAB_YEAR:-}"
INTERVAL_SECONDS="${LAB_INTERVAL_SECONDS:-14400}" # every 4h by default
LOG_DIR="${LAB_LOG_DIR:-$HOME/Library/Logs/lab}"
SCRIPT="$ROOT/scripts/lab-automation.sh"
# JDG_BIN and JDG_YEAR go into the plist only when set explicitly; otherwise
# jdg-cli-automation.sh resolves the binary and the year(s) on every run.
JDG_BIN_OVERRIDE="${JDG_BIN:-}"
YEAR_OVERRIDE="${JDG_YEAR:-}"
INTERVAL_SECONDS="${JDG_INTERVAL_SECONDS:-14400}" # every 4h by default
LOG_DIR="${JDG_LOG_DIR:-$HOME/Library/Logs/jdg-cli}"
SCRIPT="$ROOT/scripts/jdg-cli-automation.sh"
# launchd starts agents with PATH=/usr/bin:/bin:/usr/sbin:/sbin; the binary
# needs node/npm for the Saldeo re-login and Homebrew tools (poppler).
BASE_PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

# Explicit LAB_BIN wins; else ~/.local/bin/lab, else ~/.local/bin/jdg-cli, else the legacy ~/.local/bin/lab-cli.
resolve_lab_bin() {
if [[ -n "${LAB_BIN_OVERRIDE:-}" ]]; then
printf '%s\n' "$LAB_BIN_OVERRIDE"
elif [[ ! -e "$HOME/.local/bin/lab" && -x "$HOME/.local/bin/jdg-cli" ]]; then
printf '%s\n' "$HOME/.local/bin/jdg-cli"
elif [[ ! -e "$HOME/.local/bin/lab" && -x "$HOME/.local/bin/lab-cli" ]]; then
printf '%s\n' "$HOME/.local/bin/lab-cli"
# Explicit JDG_BIN wins; else ~/.local/bin/jdg-cli.
resolve_jdg_bin() {
if [[ -n "${JDG_BIN_OVERRIDE:-}" ]]; then
printf '%s\n' "$JDG_BIN_OVERRIDE"
else
printf '%s\n' "$HOME/.local/bin/lab"
printf '%s\n' "$HOME/.local/bin/jdg-cli"
fi
}

Expand Down Expand Up @@ -68,19 +64,19 @@ env_entry() {

render_env() {
env_entry PATH "$1"
env_entry LAB_ROOT "$ROOT"
if [[ -n "$LAB_BIN_OVERRIDE" ]]; then
env_entry LAB_BIN "$LAB_BIN_OVERRIDE"
env_entry JDG_ROOT "$ROOT"
if [[ -n "$JDG_BIN_OVERRIDE" ]]; then
env_entry JDG_BIN "$JDG_BIN_OVERRIDE"
fi
if [[ -n "$YEAR_OVERRIDE" ]]; then
env_entry LAB_YEAR "$YEAR_OVERRIDE"
env_entry JDG_YEAR "$YEAR_OVERRIDE"
fi
env_entry LAB_LOG_DIR "$LOG_DIR"
env_entry JDG_LOG_DIR "$LOG_DIR"
}

# Log directory 700 and log files 600: they hold invoice data. launchd creates its
# stdout/stderr files with its own default mode, so they are created here first.
# lab-automation.sh writes only to automation.log; launchd.out.log stays empty in a
# jdg-cli-automation.sh writes only to automation.log; launchd.out.log stays empty in a
# normal run and launchd.err.log keeps whatever the script did not catch (bash errors).
prepare_log_dir() {
local file
Expand Down Expand Up @@ -132,23 +128,23 @@ PLIST
}

main() {
local lab_bin launchd_path
lab_bin="$(resolve_lab_bin)"
local jdg_bin launchd_path
jdg_bin="$(resolve_jdg_bin)"

if [[ ! -x "$SCRIPT" ]]; then
echo "ERROR: automation script is not executable: $SCRIPT" >&2
exit 1
fi
if [[ ! -x "$lab_bin" ]]; then
echo "ERROR: LAB binary is not executable: $lab_bin" >&2
if [[ ! -x "$jdg_bin" ]]; then
echo "ERROR: jdg-cli binary is not executable: $jdg_bin" >&2
exit 1
fi
if [[ -n "$YEAR_OVERRIDE" && ! "$YEAR_OVERRIDE" =~ ^[0-9]{4}$ ]]; then
echo "ERROR: LAB_YEAR must be a 4-digit year, got: $YEAR_OVERRIDE" >&2
echo "ERROR: JDG_YEAR must be a 4-digit year, got: $YEAR_OVERRIDE" >&2
exit 1
fi
if [[ ! "$INTERVAL_SECONDS" =~ ^[0-9]+$ ]]; then
echo "ERROR: LAB_INTERVAL_SECONDS must be a number of seconds, got: $INTERVAL_SECONDS" >&2
echo "ERROR: JDG_INTERVAL_SECONDS must be a number of seconds, got: $INTERVAL_SECONDS" >&2
exit 1
fi
if ! command -v node >/dev/null 2>&1; then
Expand All @@ -173,9 +169,9 @@ main() {

echo "Installed and started: $LABEL"
echo "Plist: $PLIST"
echo "Binary: $lab_bin${LAB_BIN_OVERRIDE:+ (fixed by LAB_BIN)}"
echo "Binary: $jdg_bin${JDG_BIN_OVERRIDE:+ (fixed by JDG_BIN)}"
if [[ -n "$YEAR_OVERRIDE" ]]; then
echo "Year: $YEAR_OVERRIDE (fixed by LAB_YEAR)"
echo "Year: $YEAR_OVERRIDE (fixed by JDG_YEAR)"
else
echo "Year: current year at run time (Jan-Feb: previous year, then current)"
fi
Expand Down
70 changes: 33 additions & 37 deletions scripts/lab-automation.sh → scripts/jdg-cli-automation.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
#!/usr/bin/env bash
set -euo pipefail

ROOT="${LAB_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
ROOT="${JDG_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}" .sh)"
LOG_DIR="${LAB_LOG_DIR:-$HOME/Library/Logs/lab}"
LOCK_DIR="${LAB_LOCK_DIR:-$HOME/Library/Caches/lab/automation.lock}"
LOG_DIR="${JDG_LOG_DIR:-$HOME/Library/Logs/jdg-cli}"
LOCK_DIR="${JDG_LOCK_DIR:-$HOME/Library/Caches/jdg-cli/automation.lock}"
LOG_FILE="$LOG_DIR/automation.log"
# automation.log is rotated at the start of a run once it exceeds LOG_MAX_BYTES;
# LOG_KEEP old copies are kept (automation.log.1 is the newest).
LOG_MAX_BYTES=$((5 * 1024 * 1024))
LOG_KEEP=3
# Error text the binary prints (with LAB_NONINTERACTIVE=1) instead of opening a login window.
# Error text the binary prints (with JDG_NONINTERACTIVE=1) instead of opening a login window.
SESSION_EXPIRED_PHRASE="Saldeo session expired"

# Explicit LAB_BIN wins; else ~/.local/bin/lab, else ~/.local/bin/jdg-cli, else the legacy ~/.local/bin/lab-cli.
resolve_lab_bin() {
if [[ -n "${LAB_BIN:-}" ]]; then
printf '%s\n' "$LAB_BIN"
elif [[ ! -e "$HOME/.local/bin/lab" && -x "$HOME/.local/bin/jdg-cli" ]]; then
printf '%s\n' "$HOME/.local/bin/jdg-cli"
elif [[ ! -e "$HOME/.local/bin/lab" && -x "$HOME/.local/bin/lab-cli" ]]; then
printf '%s\n' "$HOME/.local/bin/lab-cli"
# Explicit JDG_BIN wins; else ~/.local/bin/jdg-cli.
resolve_jdg_bin() {
if [[ -n "${JDG_BIN:-}" ]]; then
printf '%s\n' "$JDG_BIN"
else
printf '%s\n' "$HOME/.local/bin/lab"
printf '%s\n' "$HOME/.local/bin/jdg-cli"
fi
}

Expand Down Expand Up @@ -226,9 +222,9 @@ run_year() {
FAILED_STEP=""

log "Start year=$year"
run_step "sync" "$LAB_BIN" sync --year "$year"
run_step "reconcile" "$LAB_BIN" reconcile --year "$year" --store --raw --output "$LOG_DIR/reconcile-$year.json"
run_step "repair" "$LAB_BIN" repair --year "$year" --confirm --output "$LOG_DIR/repair-$year.json"
run_step "sync" "$JDG_BIN" sync --year "$year"
run_step "reconcile" "$JDG_BIN" reconcile --year "$year" --store --raw --output "$LOG_DIR/reconcile-$year.json"
run_step "repair" "$JDG_BIN" repair --year "$year" --confirm --output "$LOG_DIR/repair-$year.json"
if [[ "$YEAR_STATUS" -eq 0 ]]; then
# Never report an earlier run's upload result as this run's.
if rm -f "$upload_json"; then
Expand All @@ -239,9 +235,9 @@ run_year() {
FAILED_STEP="upload (stale output)"
fi
fi
run_step "upload" "$LAB_BIN" upload --year "$year" --confirm --approve --output "$upload_json"
run_step "sync --saldeo" "$LAB_BIN" sync --saldeo --year "$year"
run_step "final reconcile" "$LAB_BIN" reconcile --year "$year" --store --raw --output "$LOG_DIR/reconcile-after-upload-$year.json"
run_step "upload" "$JDG_BIN" upload --year "$year" --confirm --approve --output "$upload_json"
run_step "sync --saldeo" "$JDG_BIN" sync --saldeo --year "$year"
run_step "final reconcile" "$JDG_BIN" reconcile --year "$year" --store --raw --output "$LOG_DIR/reconcile-after-upload-$year.json"

YEAR_SUMMARY="year=$year"
if [[ "$YEAR_STATUS" -ne 0 ]]; then
Expand All @@ -263,16 +259,16 @@ main() {
prepare_log_dir
mkdir -p "$(dirname "$LOCK_DIR")"
# Unattended: the binary must never open a Saldeo login window. An explicit
# LAB_NONINTERACTIVE in the environment (e.g. 0 for a manual run) wins.
export LAB_NONINTERACTIVE="${LAB_NONINTERACTIVE:-1}"
# JDG_NONINTERACTIVE in the environment (e.g. 0 for a manual run) wins.
export JDG_NONINTERACTIVE="${JDG_NONINTERACTIVE:-1}"

local lock_status=0
acquire_lock || lock_status=$?
if [[ "$lock_status" -eq 1 ]]; then
log "Another LAB automation run is active (pid $LOCK_HOLDER); exiting."
log "Another jdg-cli automation run is active (pid $LOCK_HOLDER); exiting."
exit 0
elif [[ "$lock_status" -ne 0 ]]; then
notify "LAB automation failed" "Cannot remove stale lock $LOCK_DIR"
notify "jdg-cli automation failed" "Cannot remove stale lock $LOCK_DIR"
exit 1
fi
trap release_lock EXIT
Expand All @@ -281,28 +277,28 @@ main() {
trap 'exit 143' TERM
rotate_log

LAB_BIN="$(resolve_lab_bin)"
if [[ ! -x "$LAB_BIN" ]]; then
log "ERROR: LAB binary not executable: $LAB_BIN"
notify "LAB automation failed" "LAB binary not executable: $LAB_BIN"
JDG_BIN="$(resolve_jdg_bin)"
if [[ ! -x "$JDG_BIN" ]]; then
log "ERROR: jdg-cli binary not executable: $JDG_BIN"
notify "jdg-cli automation failed" "jdg-cli binary not executable: $JDG_BIN"
exit 1
fi

local years
if [[ -n "${LAB_YEAR:-}" ]]; then
if [[ ! "$LAB_YEAR" =~ ^[0-9]{4}$ ]]; then
log "ERROR: LAB_YEAR must be a 4-digit year, got: $LAB_YEAR"
notify "LAB automation failed" "Invalid LAB_YEAR: $LAB_YEAR"
if [[ -n "${JDG_YEAR:-}" ]]; then
if [[ ! "$JDG_YEAR" =~ ^[0-9]{4}$ ]]; then
log "ERROR: JDG_YEAR must be a 4-digit year, got: $JDG_YEAR"
notify "jdg-cli automation failed" "Invalid JDG_YEAR: $JDG_YEAR"
exit 1
fi
years="$LAB_YEAR"
years="$JDG_YEAR"
else
years="$(automation_years "$(date +%Y)" "$(date +%m)")"
fi

cd "$ROOT"

log "Start automation years=$years root=$ROOT bin=$LAB_BIN noninteractive=$LAB_NONINTERACTIVE"
log "Start automation years=$years root=$ROOT bin=$JDG_BIN noninteractive=$JDG_NONINTERACTIVE"

local status=0 year summaries=""
for year in $years; do
Expand All @@ -319,12 +315,12 @@ main() {
log "FAILED with status=$status. $summaries"
fi
if [[ "$SAW_SESSION_EXPIRED" -eq 1 ]]; then
log "Saldeo session expired; log in with: lab onboard"
notify "LAB: Saldeo login needed" "Saldeo session expired. Run \`lab onboard\` in Terminal to log in. $summaries"
log "Saldeo session expired; log in with: jdg-cli onboard"
notify "jdg-cli: Saldeo login needed" "Saldeo session expired. Run \`jdg-cli onboard\` in Terminal to log in. $summaries"
elif [[ "$status" -eq 0 ]]; then
notify "LAB automation done" "$summaries"
notify "jdg-cli automation done" "$summaries"
else
notify "LAB automation failed" "$summaries. Check $LOG_FILE"
notify "jdg-cli automation failed" "$summaries. Check $LOG_FILE"
fi

exit "$status"
Expand Down
Loading
Loading