output

package
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyJQ

func ApplyJQ(w io.Writer, payload any, expr string) error

ApplyJQ applies a jq expression to a JSON-serialisable payload and writes the results to w. Each result value is written as a separate line of JSON.

The expression is compiled once and evaluated against the normalised payload. Multiple result values (e.g. from `.[]`) are each written as indented JSON followed by a newline.

func ResolveFields

func ResolveFields(cmd *cobra.Command) string

ResolveFields extracts the --fields flag value from the command. It ensures that we do not mistakenly grab a business parameter also named "fields" by matching the flag's usage string against the global root definition.

func ResolveJQ

func ResolveJQ(cmd *cobra.Command) string

ResolveJQ extracts the --jq flag value from the command. It ensures that we only grab the global output filter, not a similarly named business parameter.

func SanitizeForTerminal

func SanitizeForTerminal(text string) string

SanitizeForTerminal strips ANSI escape sequences, control characters, and dangerous Unicode from text before it is printed to a terminal. Delegates to the validate package which provides the canonical implementation.

func SelectFields

func SelectFields(payload any, fields []string) any

SelectFields filters a JSON-serialisable payload to include only the specified field names, following the gh CLI convention:

  1. If the payload is an array of objects, filter each element.
  2. If the payload is an object containing a "data list" (a nested array under a well-known key like "value", "items", "data", "records", etc.), filter each element of that list.
  3. Otherwise, filter top-level keys.

This approach lets callers write `--fields title,memberCount` and get the right result regardless of the response envelope structure. Field names are matched case-insensitively.

func Write

func Write(w io.Writer, format Format, payload any) error

func WriteCommandPayload

func WriteCommandPayload(cmd *cobra.Command, payload any, fallback Format) error

func WriteFiltered

func WriteFiltered(w io.Writer, format Format, payload any, fields, jq string) error

WriteFiltered applies field selection and/or jq filtering before writing the payload. If jq is non-empty, the jq result is written directly (bypassing format). If fields is non-empty, the payload is filtered to those fields before normal output.

func WriteJSON

func WriteJSON(w io.Writer, payload any) error

WriteJSON marshals payload as indented JSON and writes it to w.

Types

type Format

type Format string
const (
	FormatJSON   Format = "json"
	FormatTable  Format = "table"
	FormatRaw    Format = "raw"
	FormatPretty Format = "pretty"
)

func ResolveFormat

func ResolveFormat(cmd *cobra.Command, fallback Format) Format

Jump to

Keyboard shortcuts

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