Skip to content

sdk: build CLI clients directly from CliContext (from_context)#3805

Open
juan-malbeclabs wants to merge 1 commit into
mainfrom
jo/integrate_context_cli
Open

sdk: build CLI clients directly from CliContext (from_context)#3805
juan-malbeclabs wants to merge 1 commit into
mainfrom
jo/integrate_context_cli

Conversation

@juan-malbeclabs
Copy link
Copy Markdown
Contributor

Summary of Changes

  • Add DZClient::from_context and GeoClient::from_context to the Rust SDK, building clients directly from a resolved RFC-20 CliContext instead of re-reading config.yml and re-applying moniker conversion. The context already carries fully resolved ledger RPC/WS URLs and program IDs, so the context becomes the single source of truth and the binary's previous double-resolution is removed.
  • Preserve keypair precedence exactly (CLI flag > DOUBLEZERO_KEYPAIR > stdin > context keypair path > default): the raw --keypair flag is the highest-precedence source and the context keypair path is only the low-precedence fallback, so the env var still wins.
  • Gate the new constructors and the doublezero-cli-core dependency behind a cli-context cargo feature so non-CLI SDK consumers (controlplane, telemetry, e2e) keep a dependency-light default build.
  • Wire the doublezero binary to from_context, removing the legacy DZClient::new(Option<String>, ...) bridge. DZClient::new / GeoClient::new are unchanged for callers that build no CliContext (e.g. controlplane/doublezero-admin).

Related RFC: rfcs/rfc20-cli-standardization.md

Diff Breakdown

Category Files Lines (+/-) Net
Core logic 3 +85 / -16 +69
Tests 1 +62 / -0 +62
Config/build 2 +6 / -1 +5
Docs 1 +4 / -0 +4
Generated 1 +1 / -0 +1
Total 8 +162 / -17 +145

Small, focused change: two new SDK constructors plus binary wiring, with the bulk of additions being the constructors and their tests.

Key files (click to expand)
  • smartcontract/sdk/rs/src/client.rs — add DZClient::from_context (no config re-read / moniker conversion) plus feature-gated unit tests for resolved-value consumption and keypair env-var precedence
  • smartcontract/sdk/rs/src/geolocation/client.rs — add GeoClient::from_context using the context's ledger RPC URL and geolocation program ID
  • client/doublezero/src/main.rs — construct both clients via from_context, drop the legacy bridge locals
  • smartcontract/sdk/rs/Cargo.toml — optional doublezero-cli-core dep behind a new cli-context feature
  • smartcontract/sdk/rs/src/config.rs — make default_keypair_path pub(crate) for the fallback path

Testing Verification

  • New SDK unit tests (cargo test -p doublezero_sdk --features cli-context): one asserts from_context consumes the context's RPC/WS/program-ID verbatim with no config.yml present, and one asserts DOUBLEZERO_KEYPAIR still wins over a (bogus) context keypair path, guarding the masking hazard.
  • End-to-end against testnet with the rebuilt binary: doublezero location list (exercises DZClient::from_context) and doublezero geolocation probe list (exercises GeoClient::from_context) both returned live data.
  • Verified the default SDK build (no cli-context feature) still compiles, so non-CLI consumers are unaffected.

Build the serviceability and geolocation SDK clients directly from a
resolved RFC-20 CliContext instead of round-tripping its values back
through DZClient::new's config-file re-resolution and moniker conversion.

- Add DZClient::from_context and GeoClient::from_context, gated behind a
  new `cli-context` cargo feature so non-CLI SDK consumers keep a
  dependency-light default build.
- Preserve keypair precedence (CLI flag > DOUBLEZERO_KEYPAIR > stdin >
  context keypair path > default); the context path is only the
  low-precedence fallback so the env var still wins.
- Wire the doublezero binary to from_context, removing the legacy bridge.
Comment thread CHANGELOG.md
- Migrate all eight `tenant` verbs (`create`, `update`, `list`, `get`, `delete`, `administrator add`, `administrator remove`, `update-payment-status`) and all six `permission` verbs (`set`, `suspend`, `resume`, `delete`, `get`, `list`) to the RFC-20 conforming shape on top of the shared CLI helpers. Every verb is now `pub async fn execute(self, ctx: &CliContext, client: &C, out: &mut W) -> eyre::Result<()>`, consumes the helpers (`require!`, `render_collection`, `render_record`, `print_signature`), and tenant verbs that accept a pubkey-or-code identifier (`update`, `delete`, `add-administrator`, `remove-administrator`, `update-payment-status`) route through a new `resolve_tenant_pk` helper in `smartcontract/cli/src/helpers.rs`. The duplicate-code precondition in `tenant create` is preserved, as is the `administrator = "me"` short-circuit. `tenant delete`'s bespoke two-line output ("✓ Tenant 'X' deleted successfully\n Signature: ..."), its cascade-delete progress spinners, and its reference-count polling loop are preserved with manual `writeln!` calls and an explanatory comment. `permission set`'s bespoke two-line aligned output ("Signature: ..." + "Permissions: ...") is preserved the same way. Permission verbs derive the on-chain PDA from `user_payer` rather than going through a pubkey-or-code resolver. `controlplane/doublezero-admin`, the unified `doublezero` binary, and the serviceability dispatcher all forward `&ctx` and await every tenant and permission arm. Behavior is byte-identical: table layout, JSON schema, `Signature:` line shape, and `--json` / `--json-compact` semantics match pre-refactor output exactly; all 18 tenant and 18 permission unit tests pass without assertion changes.
- SDK (Rust)
- Add `DZClient::from_context` and `GeoClient::from_context`, which build clients directly from a resolved RFC-20 `CliContext` instead of re-reading `~/.config/doublezero/cli/config.yml` and re-applying moniker conversion. The context already carries the fully resolved ledger RPC/WS URLs and program IDs, so these constructors consume them verbatim, making the context the single source of truth and removing the double-resolution the binary previously incurred. Keypair precedence is preserved exactly (CLI flag > `DOUBLEZERO_KEYPAIR` > stdin > context keypair path > default): the raw `--keypair` flag is passed as the highest-precedence source and the context keypair path is used only as the low-precedence fallback, so the env var still wins. The new constructors and their `doublezero-cli-core` dependency are gated behind a `cli-context` cargo feature so non-CLI SDK consumers (controlplane, telemetry, e2e) keep a dependency-light default build. `DZClient::new` / `GeoClient::new` are unchanged for callers that do not build a `CliContext` (e.g. `controlplane/doublezero-admin`).
- CLI
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are already CLI and SDK sections. Fold the changelog entries into them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants