Skip to content

walking skeleton: Cobra root, video list command, test harness (PRINFRA-142) - #3

Merged
somanshreddy merged 4 commits into
mainfrom
03-30-m0_video_list
Mar 31, 2026
Merged

somanshreddy merged 4 commits into
mainfrom
03-30-m0_video_list

Conversation

@somanshreddy

@somanshreddy somanshreddy commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Wires everything from PRs #1 and #2 into a working CLI. After this, heygen video list works end-to-end against the real HeyGen API.

What happens when you run heygen video list --limit 5:

  1. main.go creates a JSON formatter before anything else — so even early failures (bad auth, bad flags) get structured JSON error output, never plain text.
  2. Cobra parses the command and flags. Unknown commands and bad flags are caught and returned as usage errors (exit 2).
  3. The root command's startup hook runs: creates a config provider, resolves the API key, and builds an HTTP client.
  4. The video list command validates --limit is 1-100, builds a RequestSpec, and hands it to the executor.
  5. The executor makes the HTTP call and returns raw JSON. The formatter pretty-prints it to stdout.
  6. If anything fails at any step, the error flows back to main.go where it's classified (auth → exit 3, usage → exit 2, everything else → exit 1) and rendered as a JSON envelope to stderr.

Test harness: runCommand() in the test helper mirrors this exact flow — fresh Cobra tree, buffer-backed formatter, same error classification. Tests assert on exit codes and the JSON envelope shape, not just whether an error occurred.

Testing

10 command tests using httptest mocks:

  • Success with valid JSON response
  • Missing auth (exit 3 with hint)
  • API 401 and 400 responses (correct exit codes, request ID preserved)
  • Flags passed correctly as query params
  • Limit validation (exit 2 for 0, -5, 999)
  • Unknown flag and unknown command (exit 2)
  • Server error (exit 1)

Manual smoke test: HEYGEN_API_KEY=<key> ./bin/heygen video list --limit 2 returns real video JSON.

Linear: PRINFRA-142

somanshreddy commented Mar 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

@somanshreddy
somanshreddy marked this pull request as ready for review March 30, 2026 21:00
@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from efe76e6 to 9bde38a Compare March 30, 2026 21:05
@somanshreddy somanshreddy changed the title [M0] walking skeleton: heygen video list E2E walking skeleton: Cobra root, video list command, test harness Mar 30, 2026
Comment thread cmd/heygen/main.go
// Bootstrap formatter created before the Cobra tree so it's available
// for errors from PersistentPreRunE (auth failures, config loading).
formatter := output.DefaultJSONFormatter()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Formatter is created here (before the Cobra tree) so it's available for errors from PersistentPreRunE — e.g. auth failures. Without this, early errors would have no formatter to render through.

Comment thread cmd/heygen/root.go
// 1. Create config provider (BaseURL only — auth is Resolver's job)
provider := &config.EnvProvider{}
ctx.config = provider

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

config.Provider is on the execution path (not just defined): PersistentPreRunE creates EnvProvider and passes provider.BaseURL() to the client constructor. When FileProvider replaces this, the wiring stays the same.

Comment thread cmd/heygen/video_list.go
Long: "List videos in your HeyGen account with optional filtering.",
RunE: func(cmd *cobra.Command, args []string) error {
// Validate --limit if explicitly provided
if cmd.Flags().Changed("limit") {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Using cmd.Flags().Changed() instead of checking limit > 0 — this correctly distinguishes 'not provided' (don't send param, let API use its default) from 'explicitly set to some value'. The codegen pipeline will use this same pattern for all optional flags.

formatter := output.NewJSONFormatter(&stdout, &stderr)

// Set env vars for this test
t.Setenv("HEYGEN_API_KEY", apiKey)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This mirrors the production error-rendering path from main(): errors are rendered through formatter.Error() into the stderr buffer. This ensures stderr content in tests matches what users actually see — tests can assert on the exact JSON envelope shape.

@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from 9bde38a to d49db0e Compare March 30, 2026 21:29
@somanshreddy
somanshreddy force-pushed the 03-30-m0_video_list branch 2 times, most recently from 8ce9be0 to c1f876d Compare March 30, 2026 22:10
@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from d49db0e to 00a632c Compare March 30, 2026 22:10
@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from 00a632c to 3cb6ba2 Compare March 30, 2026 22:24
Comment thread cmd/heygen/video.go
Comment on lines +5 to +10
func newVideoCmd(ctx *cmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "video",
Short: "Manage videos",
}
cmd.AddCommand(newVideoListCmd(ctx))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we need help/examples for everything

Comment thread cmd/heygen/video_list.go
Comment on lines +17 to +20
cmd := &cobra.Command{
Use: "list",
Short: "List videos",
Long: "List videos in your HeyGen account with optional filtering.",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we need good help/example messages for everything this is critical IMO

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is handled in the Design - https://www.notion.so/heygen/Design-331449792c69804993e0eb736b024ca2?source=copy_link#045c03f0f92444e782f37343c7f39b18

We will need to manually generate these examples (via an LLM followed by a human review).

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

left a comment we need good descriptions, examples etc! in the long case

I think kubectl has some formatting/helpers here

we could create our own wrapper around cobra cmd as well to help enforce this for each commnad and format it well!

somanshreddy commented Mar 31, 2026 •

Copy link
Copy Markdown
Collaborator Author

@jrusso1020 Help examples are covered in the Design doc.

Good idea to add an enforcement layer to that. I will add that!

@somanshreddy
somanshreddy force-pushed the 03-30-m0_client_executor branch from 3cb6ba2 to 5f62f7e Compare March 31, 2026 05:44
@somanshreddy somanshreddy changed the title walking skeleton: Cobra root, video list command, test harness walking skeleton: Cobra root, video list command, test harness (PRINFRA-142) Mar 31, 2026
@linear

linear Bot commented Mar 31, 2026

Copy link
Copy Markdown
PRINFRA-142 CLI M0: Walking skeleton — `video list` E2E + test harness

Prove the full architecture spine works end-to-end with one hand-written command and establish the test harness.

Scope

  • Hand-written video list command as proof of concept
    • Defines a CommandSpec manually (before codegen exists)
    • Wires into Cobra via the framework
    • Calls GET /v3/videos → JSON output on stdout
  • Test harness (patterns for all future tests):
    • httptest.Server mock pattern for command tests
    • Cobra test runner (execute command, capture stdout/stderr, check exit code)
    • Golden file comparison framework for codegen (M1)
  • Tests:
    • video list happy path (mock returns videos JSON)
    • video list error case (mock returns 401 → CLIError → exit 3)

Why this matters

This is the validation gate for M0. If heygen video list works against the real API with HEYGEN_API_KEY, the architecture spine is proven. M1 and M2 can safely proceed in parallel.

Acceptance Criteria

  • HEYGEN_API_KEY=xxx heygen video list returns real video data as JSON on stdout
  • heygen video list with no key → exit 3 with clear error
  • Test harness patterns are documented and reusable
  • All M0 tests pass in CI

somanshreddy commented Mar 31, 2026 •

Copy link
Copy Markdown
Collaborator Author

Merge activity

  • Mar 31, 5:53 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Mar 31, 5:57 AM UTC: Graphite rebased this pull request as part of a merge.
  • Mar 31, 5:59 AM UTC: @somanshreddy merged this pull request with Graphite.

@somanshreddy
somanshreddy changed the base branch from 03-30-m0_client_executor to graphite-base/3 March 31, 2026 05:54
@somanshreddy
somanshreddy changed the base branch from graphite-base/3 to main March 31, 2026 05:56
somanshreddy and others added 4 commits March 31, 2026 05:57
Cobra root command with PersistentPreRunE that wires config.Provider,
auth.Resolver, and client together on the execution path:

- main.go: bootstrap JSONFormatter before Cobra tree, all errors
  (including PersistentPreRunE failures) rendered as JSON envelopes
- root.go: cmdContext with client, formatter, config; PersistentPreRunE
  creates EnvProvider → reads BaseURL() → creates Client
- video list: --limit, --token, --folder-id flags, builds canonical
  RequestSpec with []QueryParam, returns raw JSON via formatter.Data()

Test harness mirrors production error-rendering path:
- setupTestServer: httptest mock with handler map
- runCommand: fresh Cobra tree per test, renders errors through
  formatter into stderr buffer, extracts exit code from CLIError
- Tests: success, auth missing (exit 3), API 401 (exit 3), API 400
  (exit 1), flags as query params, server error (exit 1)

Verified: heygen video list --limit 2 returns real video JSON from API.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Three fixes from review:

1. Usage errors → exit code 2: SetFlagErrorFunc wraps Cobra flag
   parse errors as CLIError{ExitUsage}. Unknown flags, bad types
   now correctly return exit 2 with JSON envelope.

2. --limit validation: reject values outside 1-100 with ExitUsage
   before hitting the API. --limit -5 and --limit 999 are caught
   locally.

3. Remove APIKey() from config.Provider interface: credentials are
   exclusively auth.Resolver's responsibility. Provider only owns
   BaseURL(). Eliminates dual credential paths that could drift
   when M2 adds keyring/file auth.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Linear's GitHub integration auto-links PRs to issues when the issue
ID appears in the PR title (e.g., PRINFRA-123).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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