yamlops

package
v0.3.45 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SearchInMap

func SearchInMap(node *yaml.Node, values map[string]string) bool

SearchInMap look for provided key=values inside a yaml map

func SearchNodes

func SearchNodes(root *yaml.Node, rootPath string, matchers ...NodeMatchFunc) map[string]*yaml.Node

SearchNodes walks the node tree adding the node to the result if matchers all return true.

func UpdateMap

func UpdateMap(doc []byte, pathSpec, selectorFilter string, selectors, values map[string]string) ([]byte, error)

UpdateMap finds maps in a YAML doc that match a path and/or contain a specific set of fields. It then updates the maps' fields with new values.

Values are replaced without disturbing any other content (as far as is possible) although the original style (inline, flow, folded, etc.) of value nodes being replaced may not be retained. To achieve this, the func works close to the YAML AST imposing some limitations:

* New values must be strings. * Matching maps must already define the values being replaced. * Existing map values must be strings.

It should be possible to remove some of these limitations as needed.

Examples:

Update a Chart's values.yaml to set a specific image's reference:

UpdateMap(doc, ".images.postgresql", "", nil, map[string]string{
    "registry": "docker.io",
    "repository": "bitnami/postgresql",
    "tag": "11.6.0-debian-10-r5",
})

Update a Chart's values.yaml to replace any uses of a general purpose image:

UpdateMap(doc, "", "",map[string]string{
    "repository": "bitnami/minideb",
}, map[string]string{
    "registry": "custom.images.org",
    "repository": "custom-general-purpose",
    "tag": "1.2.3",
})

Update a Chart's dependencies to rewrite a chart registry:

UpdateMap(doc, "", ".dependencies", map[string]string{
    "repository": "https://charts.bitnami.com/bitnami",
}, map[string]string{
    "repository": "custom.charts.org",
})

Types

type NodeMatchFunc

type NodeMatchFunc func(node *yaml.Node, path string) bool

NodeMatchFunc is a func that takes a node and path and returns a bool to indicate if the node matches some test.

func MapNodeContains

func MapNodeContains(values map[string]string) NodeMatchFunc

MapNodeContains returns a NodeMatchFunc that tests if a map node contains all the given field values.

func NodeHasPath

func NodeHasPath(spec string) NodeMatchFunc

NodeHasPath returns a NodeMatchFunc that tests if the node's path matches the query spec.

func SelectorMatchFilter

func SelectorMatchFilter(filter string, values map[string]string) NodeMatchFunc

SelectorMatchFilter returns a NodeMatchFunc that tests if a map node inside a prefix contains all the given field values.

type NodeScanner

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

NodeScanner performs a depth-first traversal of a yaml node tree, yielding each node with its jq-like path in the document.

func NewNodeScanner

func NewNodeScanner(root *yaml.Node, path string) *NodeScanner

NewNodeScanner returns a NodeScanner to traverse the yaml node graph starting at the root node and path.

func (*NodeScanner) Current

func (ns *NodeScanner) Current() (*yaml.Node, string)

Current returns the scanner's current node and path.

func (*NodeScanner) Next

func (ns *NodeScanner) Next() bool

Next moves the scanner to the next value node.

Jump to

Keyboard shortcuts

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