SaasKit.Task.Helpers (saas_kit v3.0.0)

Copy Markdown

Shared helpers for SaaS Kit Mix tasks.

Centralizes the human/JSON output split. Every read-only task parses --json via parse_opts/2, flips into JSON mode with enter_json_mode/1, and emits its final payload via emit/3 or fail!/4.

JSON documents always include a stable schema_version and ok flag so agents can rely on the shape. Stable error codes used across tasks:

  • not_configuredboilerplate_token missing from config
  • api_unreachable — API call failed (timeout, 5xx, network)
  • feature_not_found — requested feature slug does not exist

Summary

Functions

Emits the final result.

When --json is set, silence Mix.shell().info/1 so stdout stays pure JSON. Returns opts unchanged for pipelining.

Emits a structured failure and halts with exit_code.

Prints human-readable text, but only when not in --json mode.

Parses task args with --json layered on top of task-specific switches.

Current JSON schema version. Exposed for tests and docs.

Functions

emit(payload, opts, human_fn)

Emits the final result.

In --json mode: merges schema_version and ok: true into the payload and prints it as one JSON line to stdout.

Otherwise: invokes human_fn with the raw payload to pretty-print.

enter_json_mode(opts)

When --json is set, silence Mix.shell().info/1 so stdout stays pure JSON. Returns opts unchanged for pipelining.

fail!(code, message, opts, exit_code \\ 1)

Emits a structured failure and halts with exit_code.

In --json mode: {"schema_version":1,"ok":false,"error":{"code":...,"message":...}} Otherwise: Mix.shell().error(message).

human(msg, opts)

Prints human-readable text, but only when not in --json mode.

parse_opts(args, extra_switches \\ [])

Parses task args with --json layered on top of task-specific switches.

schema_version()

Current JSON schema version. Exposed for tests and docs.