yamlpath

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: Apache-2.0 Imports: 9 Imported by: 61 modules, 54 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package yamlpath provides YAML node searching using path notation.

Example

Example uses a Path to find certain nodes and replace their content. Unlike a global change, it avoids false positives.

package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/sergi/go-diff/diffmatchpatch"
	"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
	"gopkg.in/yaml.v3"
)

func main() {
	y := `---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-deployment
spec:
  template:
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
      - name: nginy
        image: nginy
        ports:
        - containerPort: 81
`
	var n yaml.Node

	err := yaml.Unmarshal([]byte(y), &n)
	if err != nil {
		log.Fatalf("cannot unmarshal data: %v", err)
	}

	p, err := yamlpath.NewPath("$..spec.containers[*].image")
	if err != nil {
		log.Fatalf("cannot create path: %v", err)
	}

	q, err := p.Find(&n)
	if err != nil {
		log.Fatalf("unexpected error: %v", err)
	}

	for _, i := range q {
		i.Value = "example.com/user/" + i.Value
	}

	var buf bytes.Buffer
	e := yaml.NewEncoder(&buf)
	defer e.Close()
	e.SetIndent(2)

	err = e.Encode(&n)
	if err != nil {
		log.Printf("Error: cannot marshal node: %v", err)
		return
	}

	z := `apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-deployment
spec:
  template:
    spec:
      containers:
      - name: nginx
        image: example.com/user/nginx
        ports:
        - containerPort: 80
      - name: nginy
        image: example.com/user/nginy
        ports:
        - containerPort: 81
`
	if buf.String() == z {
		fmt.Printf("success")
	} else {
		dmp := diffmatchpatch.New()
		diffs := dmp.DiffMain(buf.String(), z, false)
		fmt.Println(diffs)
	}

}
Output:
success

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Path

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

Path is a compiled YAML path expression.

func NewPath

func NewPath(path string) (*Path, error)

NewPath constructs a Path from a string expression.

func (*Path) Find

func (p *Path) Find(node *yaml.Node) ([]*yaml.Node, error)

Find applies the Path to a YAML node and returns the addresses of the subnodes which match the Path.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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