feat: add ory use workspace - #453
Conversation
The documentation advertises `ory use workspace <id>` for setting the default workspace, and `ory auth` prints a SELECTED WORKSPACE field, but the subcommand was never registered — only `ory use project` existed. The workspace had to be passed via --workspace on every invocation. The config already carried a selected_workspace field and CommandHelper.SelectWorkspace already persisted it, so this is command wiring: the new subcommand mirrors `ory use project`, printing the current default when called without an argument and otherwise resolving the given ID or (partial) name and storing it. Closes #406 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JYzGVwAKQ4ormxRHZg1eDu
…s it `ory use workspace` without an argument is documented as printing the current default, but it called SelectWorkspace unconditionally. With ORY_WORKSPACE (or a workspace API key) set, the resolved value was written to the config file, silently replacing the stored default. Persist only when an id argument is actually given. Also accept the `workspaces` alias, matching `get`/`list`/`create workspace`, and drop the unused selectedWorkspace.String method (cmdx.PrintRow only uses Header/Columns/Interface). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JYzGVwAKQ4ormxRHZg1eDu
📝 WalkthroughWalkthroughAdds the ChangesWorkspace selection
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant CLI
participant WorkspaceCommand
participant CommandHelper
participant Config
participant Output
CLI->>WorkspaceCommand: invoke use workspace [id]
WorkspaceCommand->>CommandHelper: resolve workspace ID
CommandHelper->>Config: read or persist workspace selection
WorkspaceCommand->>Output: print selected workspace
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cmd/cloudx/workspace/use_test.go (1)
53-112: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winCover the documented
workspacesalias.All cases invoke
workspacedirectly, so a regression in the newworkspacesalias would go undetected. Add one alias invocation to the existing output or persistence test.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cmd/cloudx/workspace/use_test.go` around lines 53 - 112, Extend the existing workspace command tests to invoke the documented workspaces alias in at least one output or persistence scenario. Update a suitable case around the existing test subcases so it verifies the alias produces the same expected result as workspace, while preserving the current coverage for the canonical command.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@cmd/cloudx/workspace/use_test.go`:
- Around line 53-112: Extend the existing workspace command tests to invoke the
documented workspaces alias in at least one output or persistence scenario.
Update a suitable case around the existing test subcases so it verifies the
alias produces the same expected result as workspace, while preserving the
current coverage for the canonical command.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: cfe7577a-d0af-4c3c-87a5-22c403e4f5fa
📒 Files selected for processing (5)
cmd/cloudx/client/command_helper.gocmd/cloudx/use.gocmd/cloudx/workspace/output.gocmd/cloudx/workspace/use.gocmd/cloudx/workspace/use_test.go
Closes #406
Problem
The documentation advertises
ory use workspace <id>for setting the default workspace, andory authprints aSELECTED WORKSPACEfield — but the subcommand was never registered.ory useoffered onlyproject, so the workspace had to be passed via--workspaceon every single invocation.Fix
The config already carried a
selected_workspacefield andCommandHelper.SelectWorkspacealready persisted it, so this is mostly wiring. The new subcommand mirrorsory use project: with an argument it resolves the given ID or (partial) name and stores it; without one it reports the current default.One bug found in review and fixed: the no-argument form was calling
SelectWorkspacetoo, so merely reading the current default could silently rewrite it. WithORY_WORKSPACE=B ory use workspace,determineWorkspaceIDprefers the env var over the config, so the "read-only" form would persistBover the stored value. It now only writes when an argument was actually given.Also adds the
workspacesalias, which every other workspace subcommand accepts — without itory use workspacesmatched no subcommand, printed theusehelp and exited 0, so a script relying on the exit code would carry on as if the workspace had been set.Tests
TestUseWorkspaceruns entirely against a config file, so unlike itscmd/cloudxneighbours it needs no Ory Network access — selecting a known workspace ID never hits the API. It covers printing the default, the--format jsonshape the command's own example advertises, persisting a new default, the not-set error, and the read-only regression above (verified to fail against the pre-fix code).The persist test also asserts the access token survives the config rewrite:
SelectWorkspacere-encodes the whole config withO_TRUNC, so a partially populated struct would silently log the user out.Known limitations, deliberately not addressed here
These all apply equally to the existing
ory use projectand live in unchanged shared code, so they belong in a separate change rather than being special-cased for workspaces:determineWorkspaceIDtakes theuuid.FromStringfast path and never contacts the API. Later workspace-scoped commands then fail with an opaque 403/404.ORY_PROJECT_API_KEYexported,ory use workspace <id>fails with "project API key is set but workspace is also set" — the user has to unset an unrelated env var.ORY_WORKSPACEsuppresses the config fallback, so the command reports "no workspace was specified" even with a default stored.selected_projectpointing at a project in the old workspace. The test asserts the project is not cleared, so this is a conscious choice — flagged in case you would rather it were.🤖 Generated with Claude Code
https://claude.ai/code/session_01JYzGVwAKQ4ormxRHZg1eDu
Summary by CodeRabbit
New Features
cloudx use workspace [id]to select and display the active workspace.Tests