output

package
v6.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package output formats documents and gates colour on stdout being a terminal, as the TypeScript CLI did: its output is routinely parsed by GitOps pipelines.

Index

Constants

This section is empty.

Variables

JQ is the --jq expression, applied to whatever JSON a command prints. Set once from the root command's flags.

Functions

func ApplyJQ

func ApplyJQ(w io.Writer, jsonText string, expression string) error

ApplyJQ filters JSON through the --jq expression and writes each result on its own line: strings raw, everything else as indented JSON, as `gh --jq` does. jq itself is not needed, the expression runs in-process.

func Bold

func Bold(s string) string

func ColorsEnabled

func ColorsEnabled() bool

ColorsEnabled reports whether output is coloured: only on a terminal, unless NO_COLOR or FORCE_COLOR say otherwise.

func Green

func Green(s string) string

func IsJSON

func IsJSON(content []byte) bool

IsJSON reports whether content is a JSON document, as JSON.parse would accept it. A leading byte order mark, which Windows editors write, is ignored.

func Live

func Live() bool

Live reports whether output can be redrawn in place, as `execution watch` does.

func ParseList

func ParseList(content []byte) ([]any, error)

ParseList reads a JSON array, or JSON values one after another as newline-delimited JSON sends them. The platform streams the audit log that way, although its spec says array, so both are read.

func ParseValue

func ParseValue(content []byte) (any, error)

ParseValue reads any JSON or YAML value, such as a placeholders file that is a list.

func PathSegment

func PathSegment(id string) string

PathSegment reduces an id from the platform to one path segment, so that one containing "../" cannot write outside the chosen directory; Base alone leaves "..".

func Red

func Red(s string) string

func WithFieldFirst

func WithFieldFirst(content []byte, doc *Document, field, value string) ([]byte, bool)

WithFieldFirst returns YAML content with `field: value` added as its first key, and the rest of the file, comments and anchors included, kept byte for byte. It reports false when a line in front would not do that, as for a flow mapping `{...}` or a file that already holds the field, empty; the caller then renders the document instead.

Types

type Datatype

type Datatype string
const (
	JSON Datatype = "json"
	YAML Datatype = "yaml"
)

func ResolveDatatype

func ResolveDatatype(explicit, file string) (Datatype, error)

ResolveDatatype: an explicit type first, then the file's extension, then YAML.

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document is a JSON or YAML object as the TypeScript CLI held it: a JavaScript value with JavaScript's key order and number semantics. Files written from it are kept in Git, so they have to come out exactly as they did before.

func NewDocument

func NewDocument(m *jsyaml.Map) *Document

func ParseDocument

func ParseDocument(content []byte) (*Document, error)

ParseDocument reads JSON or YAML; JSON is valid YAML, so one parser handles both. Anchors, aliases and merge keys (`<<:`) are resolved, as js-yaml's load did.

func (*Document) Delete

func (d *Document) Delete(key string)

func (*Document) Get

func (d *Document) Get(key string) (string, bool)

func (*Document) MarshalJSON

func (d *Document) MarshalJSON() ([]byte, error)

MarshalJSON sends a document as a request body, as JSON.stringify did.

func (*Document) Render

func (d *Document) Render(datatype Datatype) ([]byte, error)

Render as `get` printed it: JSON.stringify(value, undefined, 2), or js-yaml's dump.

func (*Document) RenderFile

func (d *Document) RenderFile(datatype Datatype) []byte

RenderFile as the TypeScript CLI wrote files: compact JSON without a newline, or YAML.

func (*Document) SetFirst

func (d *Document) SetFirst(key, value string)

func (*Document) Value

func (d *Document) Value() *jsyaml.Map

Jump to

Keyboard shortcuts

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