diff options
Diffstat (limited to 'firefly_cli')
| -rw-r--r-- | firefly_cli/__init__.py | 2 | ||||
| -rw-r--r-- | firefly_cli/commands/account.py | 13 | ||||
| -rw-r--r-- | firefly_cli/commands/transaction.py | 13 | ||||
| -rw-r--r-- | firefly_cli/output.py | 21 |
4 files changed, 46 insertions, 3 deletions
diff --git a/firefly_cli/__init__.py b/firefly_cli/__init__.py index 4b170ef..27e214f 100644 --- a/firefly_cli/__init__.py +++ b/firefly_cli/__init__.py @@ -2,4 +2,4 @@ # Copyright (C) 2026 Danilo M. <danix@danix.xyz> # Licensed under the GNU General Public License v2.0 only. -__version__ = "0.3.5" +__version__ = "0.3.6" diff --git a/firefly_cli/commands/account.py b/firefly_cli/commands/account.py index d78f8fc..81c2e1d 100644 --- a/firefly_cli/commands/account.py +++ b/firefly_cli/commands/account.py @@ -23,6 +23,8 @@ def _create_args(p): p.add_argument("--opening-balance", dest="opening_balance", default=None, help="initial balance (asset accounts); dated today") p.add_argument("--currency", default=None, help="currency code, e.g. EUR") + p.add_argument("--if-not-exists", dest="if_not_exists", action="store_true", + help="if an account with this name exists, return it (existed:true) instead of erroring") @registry.command("account create", help="create an asset, expense, or revenue account", args=_create_args) def cmd_create(args, ctx): @@ -30,6 +32,17 @@ def cmd_create(args, ctx): raise FireflyError( f'Unsupported account type "{args.type}". ' f'Use one of: {", ".join(_CREATE_TYPES)}.') + if getattr(args, "if_not_exists", False): + # Resolve by name to detect existence; avoids parsing Firefly's 422 + # "name already in use" error string. Missing = ResolutionError -> create. + from firefly_cli.errors import ResolutionError + try: + existing = ctx.resolver.account(args.name) + except ResolutionError: + existing = None + if existing is not None: + output.emit({**existing, "existed": True}, human=ctx.human) + return 0 body = {"name": args.name, "type": args.type} if args.type == "asset": body["account_role"] = "defaultAsset" diff --git a/firefly_cli/commands/transaction.py b/firefly_cli/commands/transaction.py index df1ffc4..714d2d4 100644 --- a/firefly_cli/commands/transaction.py +++ b/firefly_cli/commands/transaction.py @@ -139,6 +139,8 @@ def _list_args(p): p.add_argument("--limit", type=int, default=20) p.add_argument("--all", action="store_true", help="fetch every page (ignores --limit truncation)") + p.add_argument("--flat", action="store_true", + help="one flat object per split; drop the transactions[] nesting (JSON only)") @registry.command("tx list", help="list recent transactions (newest first)", args=_list_args) def cmd_list(args, ctx): @@ -162,7 +164,7 @@ def cmd_list(args, ctx): if page >= (pg.get("total_pages") or 1): break page += 1 - output.emit(rows, human=ctx.human) + output.emit(_maybe_flat(rows, args, ctx), human=ctx.human) return 0 resp = ctx.client.request("GET", path, params=params) @@ -171,9 +173,16 @@ def cmd_list(args, ctx): if total is not None and count is not None and count < total: import sys print(f"showing {count} of {total} (use --all for all)", file=sys.stderr) - output.emit(output.unwrap(resp), human=ctx.human) + output.emit(_maybe_flat(output.unwrap(resp), args, ctx), human=ctx.human) return 0 +# --flat is a JSON-only convenience; --human already explodes splits into a +# table, so leave its nested rows alone. +def _maybe_flat(rows, args, ctx): + if getattr(args, "flat", False) and not ctx.human: + return output.flatten_tx(rows) + return rows + def _id_arg(p): p.add_argument("id") diff --git a/firefly_cli/output.py b/firefly_cli/output.py index 166e1bf..6c6a61c 100644 --- a/firefly_cli/output.py +++ b/firefly_cli/output.py @@ -62,6 +62,27 @@ def _tx_rows(rows): def _is_tx(rows): return bool(rows) and isinstance(rows[0], dict) and "transactions" in rows[0] +def flatten_tx(rows): + """Explode unwrapped tx journals into one flat object per split. + + Each journal is {id, transactions: [split, ...], ...}. We emit one object + per split: the split's raw Firefly fields (source_name, amount, etc.) with + the journal id merged in and the `transactions` list dropped. Single-split + journals (the common case) become one clean object. Rows without a + `transactions` list pass through unchanged. + """ + out = [] + for r in rows: + splits = r.get("transactions") + if not isinstance(splits, list): + out.append(r) + continue + for s in splits: + flat = dict(s) + flat["id"] = r.get("id") + out.append(flat) + return out + # Per-resource column whitelists for --human. Firefly returns ~50 fields per # row; only a handful are worth a table. A row is matched by a signature key. # (signature, columns) -- first match wins; unmatched rows use a generic table. |
