yamled

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: BSD-2-Clause Imports: 10 Imported by: 0

README

Fork clone from git@github.com:vmware-archive/go-yaml-edit.git

WARNING: This repository is no longer actively maintained by VMware.

VMware has made the difficult decision to stop driving this project and therefore we will no longer actively respond to issues or pull requests. If you would like to take over maintaining this project independently from VMware, please let us know so we can add a link to your forked project here.

Thank You.

Go Report Card

go-yaml-edit

This project contains a Go package that implements helpers for in-place editing of YAML sources.

Documentation

Read full doc on the Go package documentation page.

Contributing

The go-yaml-edit project team welcomes contributions from the community. Before you start working with go-yaml-edit, please read our Developer Certificate of Origin. All contributions to this repository must be signed as described on that page. Your signature certifies that you wrote the patch or have the right to pass it on as an open-source patch. For more detailed information, refer to CONTRIBUTING.md.

License

go-yaml-edit is available under the BSD-2 license.

Documentation

Overview

Package yamled implements helpers for in-place editing of YAML sources.

The editing is performed by a golang.org/x/text/transform.Transformer implementation configured with one or more editing operations.

Editing operations are defined as string replacements over selections covering YAML nodes in the YAML source.

Selections are constructed from *yaml.Node value that can be obtained by either manually navigating the YAML node tree or by using other packages like those provided by YAML JSONPointer or YAML JSONPath libraries.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Node

func Node(n *yaml.Node) splice.Selection

Node returns a selection that spans over a YAML node.

Types

type Transformer

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

A Transformer implements golang.org/x/text/transform.Transformer and can be used to perform precise in-place edits of yaml nodes in an byte stream.

func T

func T(ops ...splice.Op) *Transformer

T creates a transformer that performs YAML-aware edit operations.

Example

ExampleT shows how to use the transformer to edit a YAML source in place. It also shows how the quoting style is preserved.

src := `apiVersion: v1
kind: Service
metadata:
  name: "foo" # some comment
  namespace: myns
`

var root yaml.Node
if err := yaml.Unmarshal([]byte(src), &root); err != nil {
	log.Fatal(err)
}

// Let's find some nodes in the YAML tree using the YAML JSONPointer library yptr.
nameNode, err := yptr.Find(&root, "/metadata/name")
if err != nil {
	log.Fatal(err)
}
nsNode, err := yptr.Find(&root, "/metadata/namespace")
if err != nil {
	log.Fatal(err)
}

out, _, err := transform.String(yamled.T(
	yamled.Node(nameNode).With("bar"),
	yamled.Node(nsNode).With("otherns"),
), src)
if err != nil {
	log.Fatal(err)
}
fmt.Println(out)
Output:

apiVersion: v1
kind: Service
metadata:
  name: "bar" # some comment
  namespace: otherns

func (*Transformer) Reset

func (t *Transformer) Reset()

Reset implements the golang.org/x/text/transform.Transformer interface.

func (*Transformer) Transform

func (t *Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

Transform implements the golang.org/x/text/transform.Transformer interface.

Directories

Path Synopsis
Package splice allows to perform simple edits on a string, byte buffer or a file.
Package splice allows to perform simple edits on a string, byte buffer or a file.

Jump to

Keyboard shortcuts

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