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]
$