YamlMeta

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

YamlMeta

Print & Search Yaml struct in go

import in your project

import (
    "github.com/HaesungSeo/YamlMeta"
)

example of printing yaml struct

yaml example

---
sriov:
  - network: resource01 # network name
    interface: net1
    ip: 10.10.0.101     # network ip
gpu:
 drivers: video,compute,utility

service:
  type:
    NodePort: 30080

#cloud-config
password: centos
chpasswd: { expire: False }
ssh_pwauth: True

sample code

package main

import (
	"fmt"
	"strings"

	"github.com/HaesungSeo/YamlMeta"
	"gopkg.in/yaml.v2"
)

func main() {
	filebuf := []string{
		"sriov:",
		"  - network: resource01 # network name",
		"    interface: net1",
		"    ip: 10.10.0.101     # network ip",
		"gpu:",
		" drivers: video,compute,utility",
		"service:",
		"  type:",
		"    NodePort: 30080",
		"#cloud-config",
		"password: centos",
		"chpasswd: { expire: False }",
		"ssh_pwauth: True",
	}
	meta := yaml.MapSlice{}
	yaml.Unmarshal([]byte(strings.Join(filebuf, "\n")), &meta)
	YamlMeta.Print(meta, "")
	fmt.Printf("\n")
}

run

$ ./print

M[sriov]
  A[0/1]
    M[network] Str[resource01]
    M[interface] Str[net1]
    M[ip] Str[10.10.0.101]
M[gpu]
  M[drivers] Str[video,compute,utility]
M[service]
  M[type]
    M[NodePort] Int[30080]
M[password] Str[centos]
M[chpasswd]
  M[expire] Bool[false]
M[ssh_pwauth] Bool[true]
$

example of searching keys in yaml struct

yaml example

---
sriov:
  - network: resource01 # network name
    interface: net1
    ip: 10.10.0.101     # network ip
gpu:
 drivers: video,compute,utility

service:
  type:
    NodePort: 30080

#cloud-config
password: centos
chpasswd: { expire: False }
ssh_pwauth: True

sample code

search the keys "sriov[0].ip" in above yaml sample
expected result: "10.10.0.101"

package main

import (
	"fmt"
	"strings"

	"github.com/HaesungSeo/YamlMeta"
	"gopkg.in/yaml.v2"
)

func main() {
	filebuf := []string{
		"sriov:",
		"  - network: resource01 # network name",
		"    interface: net1",
		"    ip: 10.10.0.101     # network ip",
		"gpu:",
		" drivers: video,compute,utility",
		"service:",
		"  type:",
		"    NodePort: 30080",
		"#cloud-config",
		"password: centos",
		"chpasswd: { expire: False }",
		"ssh_pwauth: True",
	}
	meta := yaml.MapSlice{}
	yaml.Unmarshal([]byte(strings.Join(filebuf, "\n")), &meta)
	ret, _ := YamlMeta.Search(meta, []string{"sriov", "[0]", "ip"})
	YamlMeta.Print(ret, "")
	fmt.Printf("\n")
}

run

$ ./search
 Str[10.10.0.101]
$

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Json

func Json(meta interface{}) string

func Print

func Print(meta interface{}, tab string) error
func Search(meta interface{}, keys []string) (interface{}, error)

kyes: 'status' 'phase' status conditions [3]

func Sprint

func Sprint(in string, meta interface{}, tab string) (out string, errOut error)

Types

type InvalidIndexError

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

func (*InvalidIndexError) Error

func (m *InvalidIndexError) Error() string

type NotFoundError

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

func (*NotFoundError) Error

func (m *NotFoundError) Error() string

type SearchKeyTooLongError

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

func (*SearchKeyTooLongError) Error

func (m *SearchKeyTooLongError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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