cmdutil

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cmdutil provides the generic Build function for constructing SDK-backed CLI subcommands with consistent flags and protocol enforcement.

Every namespace subcommand follows the same contract:

  • Optional --json flag accepts a raw JSON payload
  • Optional --output flag (json | ndjson | text)
  • Optional --dry-run flag prints the request without sending it (never requires auth)
  • camelCase input keys are transparently normalized to snake_case where the SDK uses snake_case JSON tags (e.g. agentId → agent_id)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build added in v0.7.0

func Build[Req any](spec Spec[Req]) *cobra.Command

Build constructs a *cobra.Command from spec, enforcing:

  1. --json required check (before anything else, when JSONRequired is true)
  2. Transparent camelCase → snake_case key normalization on --json input
  3. --dry-run check before SDK init (never requires auth)
  4. SDK init only when actually sending
  5. Consistent --json / --output / --dry-run flag registration

Types

type Spec added in v0.7.0

type Spec[Req any] struct {
	Use   string
	Short string
	Long  string

	// JSONRequired causes an error when --json is absent.
	// Set false for list/read commands where the request body is optional.
	JSONRequired bool

	// ErrTransform optionally remaps errors returned by json.Unmarshal before
	// they are surfaced to the user. Use it to replace Go type names in SDK
	// enum validation errors with human-readable messages. The transform must
	// return an error (never nil); return fmt.Errorf("invalid --json: %w", err)
	// as the fallback to preserve the default behavior.
	ErrTransform func(err error) error

	// Run calls the SDK method and returns the response payload to serialize.
	// The payload must be the inner component field — NOT the operation wrapper
	// (e.g. return resp.ListCollectionsResponse, not resp).
	// Return (nil, nil) for operations with no response body (delete, etc.).
	Run func(ctx context.Context, sdk *glean.Glean, req Req) (any, error)
}

Spec describes a single SDK-backed subcommand.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL