Skip to content

feat(auth): add account list/use Commander attachers#45

Merged
scottlovegrove merged 3 commits into
mainfrom
feat/account-list-use-attachers
May 23, 2026
Merged

feat(auth): add account list/use Commander attachers#45
scottlovegrove merged 3 commits into
mainfrom
feat/account-list-use-attachers

Conversation

@scottlovegrove
Copy link
Copy Markdown
Collaborator

Summary

  • Adds attachAccountListCommand + attachAccountUseCommand under the @doist/cli-core/auth subpath, giving consumers account list / account use <ref> for multi-account credential management.
  • These consume TokenStore.list() / setDefault() — required on the contract since 0.12.0 but with zero consumer inside cli-core until now (the smell the roadmap flagged).
  • Mirrors the existing attachStatusCommand / attachLogoutCommand patterns: optional renderText/renderJson overrides with sensible defaults, hand-rolled json/ndjson/human branching, generics + .js extensions + type (not interface).

Behavior

  • account list--json emits { accounts: [{ account, isDefault, … }], default: <ref|null> } where default is the default entry's account.id; --ndjson streams one per-account object per line (no envelope); human prints <label ?? id> (id:<id>) with a (default) marker. Renderers optional (unlike status) so a CLI gets working output with zero config.
  • account use <ref> — positional <ref>store.setDefault(ref), then ✓ Default account set to <ref> (human) / { ok: true, default: <ref> } (--json) / silent (--ndjson, matching logout). ACCOUNT_NOT_FOUND propagates unchanged. Optional onDefaultSet follow-up hook.

Design notes

  • ndjson for list is true streaming (one object per account); per-account shape is identical to the json accounts[] entries.
  • default is derived from account.id (the contract's stable index key); stores with non-id ref-matching can override via renderJson. Documented in JSDoc.
  • The roadmap's "consolidate attacher context types" cleanup is deferred to a follow-up PR (non-breaking type-alias refactor; better factored once these two shapes exist).

Test plan

  • npm run build (tsc)
  • npm test — 465 pass, incl. 21 new cases in src/auth/account.test.ts
  • npm run check (oxlint + oxfmt) on the changed source

🤖 Generated with Claude Code

Adds attachAccountListCommand + attachAccountUseCommand under the /auth
subpath, giving consumers `account list` / `account use <ref>` for
multi-account management. They consume TokenStore.list() / setDefault(),
which were required since 0.12.0 but had no consumer inside cli-core.

Mirrors the existing attachStatusCommand / attachLogoutCommand patterns:
optional renderText/renderJson overrides with sensible defaults, json
envelope { accounts, default } with ndjson one-object-per-line streaming,
and the logout-style success envelope (ndjson silent) for `use`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@doistbot doistbot left a comment

Choose a reason for hiding this comment

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

Adding the new account list and use commands provides a solid foundation for multi-account credential management. These additions effectively bridge the gap for consumers leveraging the existing TokenStore contract. A few adjustments are needed to polish the implementation, such as updating the README to document the new exports, ensuring the JSON payload contract is consistent and validated across commands, and replacing the buffered NDJSON path with true streaming. Additionally, reusing the existing emitView utility for output formatting and refining the test suite to cover custom overrides while trimming redundant cases will help ensure long-term maintainability.

Share FeedbackReview Logs

Comment thread src/auth/index.ts
Comment thread src/auth/account.ts Outdated
Comment thread src/auth/account.ts Outdated
Comment thread src/auth/account.ts Outdated
Comment thread src/auth/account.ts Outdated
Comment thread src/auth/account.test.ts
Comment thread src/auth/account.test.ts Outdated
- Document the new exports in README (What's in it + Account selection
  usage block) per the AGENTS.md README-maintenance rule.
- account list --ndjson now streams one object per account instead of
  buffering a joined string, matching the documented streaming contract.
- account use --json now emits the resolved default account.id (re-read
  from list() after setDefault) so it round-trips against account list
  --json; the human line still echoes the raw ref.
- Adopt the shared emitView helper for the json/human branches.
- Add an --ndjson + custom renderJson test; replace the redundant
  raw-ref test with one asserting the canonical-vs-requested distinction.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@scottlovegrove
Copy link
Copy Markdown
Collaborator Author

@doistbot /review

Copy link
Copy Markdown
Member

@doistbot doistbot left a comment

Choose a reason for hiding this comment

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

This PR expands the auth module's CLI capabilities by introducing commands to list and switch between multiple accounts. These additions close an important gap in credential management while mirroring the thoughtful design patterns established by existing commands. A few adjustments are needed to align the machine-output precedence and styling with established repository rules, optimize store reads, validate JSON payloads, extract shared flag parsing logic, and streamline the test suite's async assertions and mocking approaches.

Share FeedbackReview Logs

Comment thread src/auth/account.ts Outdated
Comment thread src/auth/account.ts
Comment thread src/auth/account.ts Outdated
Comment thread src/auth/account.ts Outdated
Comment thread src/auth/account.test.ts Outdated
Comment thread src/auth/account.test.ts Outdated
Comment thread src/auth/account.test.ts Outdated
Comment thread src/auth/account.ts
Comment thread src/auth/account.test.ts Outdated
- Machine-output precedence: --json now wins over --ndjson in both
  commands (gate the ndjson cases behind !view.json), matching emitView
  / status / logout.
- account use only re-reads store.list() under --json (where the
  canonical id is needed); human / --ndjson succeed off the original ref
  so a follow-up read error can't fail an already-successful setDefault.
- Validate each per-account renderJson payload before serializing so a
  non-serializable result throws CliError('INVALID_TYPE') in both machine
  modes instead of nulling under --json.
- Extract splitViewFlags(cmd) and reuse it in both actions.
- Tests: rely on buildStore's natural ACCOUNT_NOT_FOUND throw; rewrite
  the onDefaultSet test with a deferred hook to actually prove the
  success line precedes the awaited hook; add json-wins + guard +
  no-reread coverage; drop a restating comment.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@scottlovegrove scottlovegrove merged commit 190770c into main May 23, 2026
4 checks passed
@scottlovegrove scottlovegrove deleted the feat/account-list-use-attachers branch May 23, 2026 08:13
doist-release-bot Bot added a commit that referenced this pull request May 23, 2026
## [0.22.0](v0.21.0...v0.22.0) (2026-05-23)

### Features

* **auth:** add account list/use Commander attachers ([#45](#45)) ([190770c](190770c))
@doist-release-bot
Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.22.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants