-
Notifications
You must be signed in to change notification settings - Fork 725
refactor(cli): move CLI head dispatch into src/cli/root.ts #1444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+265
−55
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * CLI head: version/help early exits, `ocx ready` pre-parse, and the bounded | ||
| * Codex-shim auto-restore preflight, in that order (Phase 1 of the CLI | ||
| * deepening — moved out of src/cli/index.ts). | ||
| * | ||
| * `parseCliHead` is pure (no I/O, no process access) so the ordering and the | ||
| * single-parse contract are unit-testable without a subprocess. `runCli` owns | ||
| * the exit paths and the shim preflight, then returns the dispatchable head | ||
| * for the command switch in src/cli/index.ts. | ||
| */ | ||
| import { hasHelpFlag, printSubcommandUsage, printUsage, printVersion } from "./help"; | ||
| import { parseReadyArgs, type ReadyArgs } from "./ready"; | ||
| import { maybeAutoRestoreCodexShim } from "./codex-shim-autorestore"; | ||
|
|
||
| export interface CliHead { | ||
| kind: "version" | "help" | "ready" | "command"; | ||
| command: string | undefined; | ||
| args: string[]; | ||
| /** For kind "help": the subcommand whose usage should print, if any. */ | ||
| helpTarget?: string; | ||
| /** Present only for `ready`; undefined when the ready args failed to parse. */ | ||
| readyArgs?: ReadyArgs; | ||
| } | ||
|
|
||
| export function parseCliHead(argv: string[]): CliHead { | ||
| const command = argv[0]; | ||
| const args = argv; | ||
| if (command === "--version" || command === "-v" || command === "version") { | ||
| return { kind: "version", command, args }; | ||
| } | ||
| if (command === undefined || command === "help" || command === "--help" || command === "-h") { | ||
| // `ocx help <sub>` carries the subcommand; bare/flag help prints the full usage. | ||
| return { | ||
| kind: "help", | ||
| command, | ||
| args, | ||
| ...(command === "help" && args[1] ? { helpTarget: args[1] } : {}), | ||
| }; | ||
| } | ||
| if (command !== "help" && hasHelpFlag(args.slice(1))) { | ||
| // `ocx <cmd> --help|-h|help` prints that command's usage, not the full list. | ||
| return { kind: "help", command, args, helpTarget: command }; | ||
| } | ||
| // P1: pre-parse `ocx ready` and reject invalid arguments with exit 64 BEFORE | ||
| // maybeAutoRestoreCodexShim (or any discovery/probe/filesystem-capable global | ||
| // preflight) runs. `ready --help` / `help ready` already exited above, so this | ||
| // only sees ready args without a help flag. Valid args are stashed so the | ||
| // switch dispatch can call runReady without a second parse. | ||
| if (command === "ready") { | ||
| const parsed = parseReadyArgs(args.slice(1)); | ||
| if (!parsed.ok) return { kind: "ready", command, args, readyArgs: undefined }; | ||
| return { kind: "ready", command, args, readyArgs: parsed.args }; | ||
| } | ||
| return { kind: "command", command, args }; | ||
| } | ||
|
|
||
| export async function runCli(argv: string[]): Promise<CliHead> { | ||
| const head = parseCliHead(argv); | ||
| switch (head.kind) { | ||
| case "version": | ||
| printVersion(); | ||
| process.exit(0); | ||
| case "help": { | ||
| if (head.helpTarget) printSubcommandUsage(head.helpTarget); | ||
| else printUsage(); | ||
| process.exit(0); | ||
| } | ||
| case "ready": { | ||
| // Fail-closed impossible-state guard: parseCliHead already ran the | ||
| // pre-parse before any shim/preflight side effect, so reaching here | ||
| // without readyArgs means dispatch diverged. Refuse with code 64 and | ||
| // perform NO I/O (no discovery/probe). | ||
| if (!head.readyArgs) { | ||
| console.error("Usage: ocx ready [--json] [--wait [--timeout <seconds>]]"); | ||
| console.error(" --timeout requires --wait; <seconds> must be a positive integer (1..300)."); | ||
| console.error(" Default wait timeout is 45 seconds."); | ||
| process.exit(64); | ||
| } | ||
| maybeAutoRestoreCodexShim(head.command, head.args); | ||
| return head; | ||
| } | ||
| case "command": | ||
| maybeAutoRestoreCodexShim(head.command, head.args); | ||
| return head; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { parseCliHead } from "../src/cli/root"; | ||
| import { DEFAULT_READY_WAIT_TIMEOUT_SECONDS } from "../src/cli/ready"; | ||
|
|
||
| describe("parseCliHead (pure CLI head, Phase 1)", () => { | ||
| test("version flags exit as version", () => { | ||
| expect(parseCliHead(["--version"])).toEqual({ kind: "version", command: "--version", args: ["--version"] }); | ||
| expect(parseCliHead(["-v"])).toEqual({ kind: "version", command: "-v", args: ["-v"] }); | ||
| expect(parseCliHead(["version"])).toEqual({ kind: "version", command: "version", args: ["version"] }); | ||
| }); | ||
|
|
||
| test("bare help forms exit as help", () => { | ||
| expect(parseCliHead([])).toEqual({ kind: "help", command: undefined, args: [] }); | ||
| expect(parseCliHead(["help"])).toEqual({ kind: "help", command: "help", args: ["help"] }); | ||
| expect(parseCliHead(["--help"])).toEqual({ kind: "help", command: "--help", args: ["--help"] }); | ||
| expect(parseCliHead(["-h"])).toEqual({ kind: "help", command: "-h", args: ["-h"] }); | ||
| }); | ||
|
|
||
| test("help with a subcommand carries the subcommand", () => { | ||
| expect(parseCliHead(["help", "service"])).toEqual({ | ||
| kind: "help", | ||
| command: "help", | ||
| args: ["help", "service"], | ||
| helpTarget: "service", | ||
| }); | ||
| }); | ||
|
|
||
| test("help with an unknown subcommand still carries the helpTarget", () => { | ||
| expect(parseCliHead(["help", "nosuch"])).toEqual({ | ||
| kind: "help", | ||
| command: "help", | ||
| args: ["help", "nosuch"], | ||
| helpTarget: "nosuch", | ||
| }); | ||
| }); | ||
|
|
||
| test("help flag after position 0 is a help exit for that command", () => { | ||
| expect(parseCliHead(["sync", "--help"])).toEqual({ | ||
| kind: "help", | ||
| command: "sync", | ||
| args: ["sync", "--help"], | ||
| helpTarget: "sync", | ||
| }); | ||
| expect(parseCliHead(["sync", "help"])).toEqual({ | ||
| kind: "help", | ||
| command: "sync", | ||
| args: ["sync", "help"], | ||
| helpTarget: "sync", | ||
| }); | ||
| expect(parseCliHead(["provider", "-h"])).toEqual({ | ||
| kind: "help", | ||
| command: "provider", | ||
| args: ["provider", "-h"], | ||
| helpTarget: "provider", | ||
| }); | ||
| }); | ||
|
|
||
| test("helpTarget routes the subcommand or command whose usage should print", () => { | ||
| expect(parseCliHead(["help", "service"])).toEqual({ | ||
| kind: "help", | ||
| command: "help", | ||
| args: ["help", "service"], | ||
| helpTarget: "service", | ||
| }); | ||
| expect(parseCliHead(["sync", "--help"])).toEqual({ | ||
| kind: "help", | ||
| command: "sync", | ||
| args: ["sync", "--help"], | ||
| helpTarget: "sync", | ||
| }); | ||
| expect(parseCliHead(["provider", "-h"])).toEqual({ | ||
| kind: "help", | ||
| command: "provider", | ||
| args: ["provider", "-h"], | ||
| helpTarget: "provider", | ||
| }); | ||
| expect(parseCliHead(["ready", "--help"])).toEqual({ | ||
| kind: "help", | ||
| command: "ready", | ||
| args: ["ready", "--help"], | ||
| helpTarget: "ready", | ||
| }); | ||
| }); | ||
|
|
||
| test("valid ready args are pre-parsed and stashed", () => { | ||
| expect(parseCliHead(["ready"])).toEqual({ | ||
| kind: "ready", | ||
| command: "ready", | ||
| args: ["ready"], | ||
| readyArgs: { json: false, wait: false, timeoutSeconds: DEFAULT_READY_WAIT_TIMEOUT_SECONDS }, | ||
| }); | ||
| expect(parseCliHead(["ready", "--json", "--wait", "--timeout", "120"])).toEqual({ | ||
| kind: "ready", | ||
| command: "ready", | ||
| args: ["ready", "--json", "--wait", "--timeout", "120"], | ||
| readyArgs: { json: true, wait: true, timeoutSeconds: 120 }, | ||
| }); | ||
| }); | ||
|
|
||
| test("invalid ready args fail closed with no readyArgs", () => { | ||
| expect(parseCliHead(["ready", "--timeout", "5"])).toEqual({ | ||
| kind: "ready", | ||
| command: "ready", | ||
| args: ["ready", "--timeout", "5"], | ||
| readyArgs: undefined, | ||
| }); | ||
| expect(parseCliHead(["ready", "--nope"])).toEqual({ | ||
| kind: "ready", | ||
| command: "ready", | ||
| args: ["ready", "--nope"], | ||
| readyArgs: undefined, | ||
| }); | ||
| expect(parseCliHead(["ready", "--wait", "--timeout", "abc"])).toEqual({ | ||
| kind: "ready", | ||
| command: "ready", | ||
| args: ["ready", "--wait", "--timeout", "abc"], | ||
| readyArgs: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| test("ordinary commands dispatch as command", () => { | ||
| expect(parseCliHead(["status"])).toEqual({ kind: "command", command: "status", args: ["status"] }); | ||
| expect(parseCliHead(["start", "--port", "8080"])).toEqual({ | ||
| kind: "command", | ||
| command: "start", | ||
| args: ["start", "--port", "8080"], | ||
| }); | ||
| expect(parseCliHead(["sync"])).toEqual({ kind: "command", command: "sync", args: ["sync"] }); | ||
| expect(parseCliHead(["provider", "list"])).toEqual({ | ||
| kind: "command", | ||
| command: "provider", | ||
| args: ["provider", "list"], | ||
| }); | ||
| expect(parseCliHead([""])).toEqual({ kind: "command", command: "", args: [""] }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.