yamledit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package yamledit finds a kind/name YAML document in a policy repo (any document of a multi-document file) and edits it by byte splice, leaving comments, formatting and every other document intact.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(data []byte, kind policy.Kind, name string) (*yaml.Node, error)

Parse returns the document declaring kind/name in data (which may hold several documents), or nil when none does.

func Quote

func Quote(v string, style yaml.Style) string

Quote renders v as an inline YAML scalar, keeping style (single/double quotes) when possible and quoting whenever a plain scalar would change meaning.

func Scalar

func Scalar(m *yaml.Node, key string) (k, v *yaml.Node)

Scalar returns the key and value nodes for key in mapping m, or nils.

Types

type Doc

type Doc struct {
	Path    string // path as walked (dir joined with the file)
	Rel     string // slash-separated path relative to dir
	Data    []byte
	Mapping *yaml.Node
}

Doc is one YAML document of a given kind/name found in a policy repo. Data is the whole file; node positions (Line/Column) are file-absolute.

func Find

func Find(dir string, kind policy.Kind, name string) (*Doc, error)

Find locates the document with the given kind and name under dir.

func (*Doc) AppendSeq

func (d *Doc) AppendSeq(path []string, item any) ([]byte, error)

AppendSeq appends item to the sequence at path (mapping keys from the document root), creating missing keys, by splicing lines: everything outside the edited sequence keeps its bytes. A block sequence gets a new "- item" after its last item; a flow sequence ([a, b]) is rewritten as a block sequence.

func (*Doc) Get

func (d *Doc) Get(path ...string) *yaml.Node

Get returns the node at path (mapping keys from the document root), or nil.

func (*Doc) Save

func (d *Doc) Save(data []byte) error

Save writes data over the document's file atomically, keeping its permissions.

func (*Doc) SetField

func (d *Doc) SetField(key, val string) ([]byte, error)

SetField sets a top-level scalar key by splicing bytes at the value's source span, so comments, blank lines, formatting and other documents are untouched. val is the raw string; it is quoted as needed (an existing quote style is kept).

func (*Doc) SetPath

func (d *Doc) SetPath(path []string, raw string) ([]byte, error)

SetPath sets the inline scalar at path to raw, spliced in verbatim like SetRaw (raw must already be valid YAML: quote strings with Quote). Path elements are mapping keys, or "name=<v>" to pick the sequence item whose name is v. Only a missing top-level key is inserted (after name); deeper keys must exist.

func (*Doc) SetRaw

func (d *Doc) SetRaw(v *yaml.Node, raw string) ([]byte, error)

SetRaw replaces the plain inline scalar v (a node of d.Mapping, at any depth) with raw, spliced byte for byte like SetField, but unquoted: for numbers and booleans.

Jump to

Keyboard shortcuts

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