refactor(cli): split command execution out of run.rs
@varshaprasad96 is already working on this.
Since Jul 17, 2026.
- Dominant language
- Rust
- Stars
- 8.7k
- Forks
- 1.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 253
Description
Problem Statement
crates/openshell-cli/src/run.rs has become the central implementation file for most CLI command execution, rendering helpers, parsing helpers, and inline tests. That makes unrelated CLI changes collide in the same file and makes review harder because command behavior, shared utilities, and tests are interleaved.
Current measurements from the feat/gateway-status-driver-info branch:
run.rsis 10,103 lines out of 21,091 total lines incrates/openshell-cli/src, about 48% of the CLI source.- The next largest CLI files are
main.rsat 4,942 lines andssh.rsat 2,356 lines. run.rscontains about 199 function definitions, including 60pub async fncommand-style entry points and 13pub fnentry points.- The inline test module starts around line 8,058 and contains about 2,046 lines with 95 test functions.
- In the last six months,
run.rswas touched by 88 commits, with roughly 12,827 insertions and 2,724 deletions. main.rswas touched by 71 commits in the same period, and 59 commits touched bothmain.rsandrun.rs, which suggests frequent parser/dispatch/execution coupling.
This matters because CLI work is a high-churn area. A single large execution file increases merge conflicts, makes small behavior changes look broad, and makes command-specific tests harder to find.
Proposed Design
Split CLI command execution incrementally by command group while preserving behavior.
Suggested target structure:
crates/openshell-cli/src/
main.rs # clap parser, global setup, dispatch
run.rs # temporary compatibility layer or shared entrypoint during migration
commands/
mod.rs
gateway.rs
sandbox.rs
provider.rs
service.rs
policy.rs
settings.rs
Start with the gateway command group because it is actively changing and has a natural boundary around gateway registration, selection, status, info, auth, and inference. Move gateway-specific tests with the gateway module. Keep shared utilities, output formatting, TLS/auth setup, and gRPC client construction in existing shared modules or extract them only when multiple command groups need them.
The refactor should be mechanical and behavior-preserving:
- Move command execution functions by command group.
- Move command-specific renderers and JSON helpers with their command group.
- Move tests close to the command implementation they cover.
- Avoid introducing a command trait or dynamic registry unless a later design requires plugin-style CLI commands.
- Keep public CLI behavior unchanged except for any already-planned command changes in separate commits.
Definition of done:
- Gateway command execution is moved out of
run.rs. - Gateway command tests move with the implementation.
- Shared helpers remain accessible without circular dependencies.
- No user-facing CLI behavior changes are introduced by the refactor.
- Focused CLI tests and
cargo fmt --checkpass.
Alternatives Considered
Keep run.rs as-is:
- Lowest immediate risk.
- Does not address the high churn and conflict surface.
Split into one file per individual command:
- Gives maximum isolation.
- Likely too granular at first; command groups are a better intermediate shape for OpenShell's current CLI size.
Introduce a command trait/registry:
- Useful for plugin-like dynamic commands.
- More abstraction than needed for a static clap CLI and would make a mechanical refactor harder to review.
Agent Investigation
- Reviewed
crates/openshell-cli/src/run.rs,main.rs, and surrounding CLI modules. - Counted CLI source lines and found
run.rsis about 48% ofcrates/openshell-cli/src. - Counted function/test density in
run.rs: about 199 function definitions and 95 test functions. - Reviewed six-month git churn for
run.rsandmain.rs; found 88 commits touchingrun.rs, 71 touchingmain.rs, and 59 touching both. - Searched open issues for
run.rs CLI refactor split commands; no duplicates were found.
Checklist
- I've reviewed existing issues and the architecture docs
- This is a design proposal, not a "please build this" request
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.