Print & Search Yaml struct in go
- import in your project
- example of printing yaml struct
- example of searching keys in yaml struct
- example of subtract some struct in yaml struct
import (
"github.com/HaesungSeo/yamlconv"
)
---
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
lets go and play with playground
package main
import (
"fmt"
"strings"
"github.com/HaesungSeo/yamlconv"
"gopkg.in/yaml.v2"
)
func main() {
buf := []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",
}
data := yaml.MapSlice{}
yaml.Unmarshal([]byte(strings.Join(buf, "\n")), &data)
yamlconv.Print(data, " ")
fmt.Printf("\n")
}
$ ./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]
$
lets go and play with playground
---
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
search the keys "sriov[0].ip" in above yaml sample
expected result: "10.10.0.101"
package main
import (
"fmt"
"strings"
"github.com/HaesungSeo/yamlconv"
"gopkg.in/yaml.v2"
)
func main() {
buf := []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",
}
data := yaml.MapSlice{}
yaml.Unmarshal([]byte(strings.Join(buf, "\n")), &data)
ret, _ := yamlconv.Search(data, []string{"sriov", "[0]", "ip"})
yamlconv.Print(ret, " ")
fmt.Printf("\n")
}
$ ./search
Str[10.10.0.101]
$
lets go and play with playground
---
sriov:
- network: resource01 # network name
interface: net1
ip: 10.10.0.101 # network ip
- network: resource02 # network name
interface: net2
ip: 20.10.0.101 # network ip
gpu:
drivers: video,compute,utility
service:
type:
NodePort: 30080
#cloud-config
password: centos
chpasswd: { expire: False }
ssh_pwauth: True
remove the struct under the keys "sriov[1]", "sriov[0].network" and "password" in above yaml sample
lets go and play with playground
package main
import (
"fmt"
"strings"
"github.com/HaesungSeo/yamlconv"
"gopkg.in/yaml.v2"
)
func main() {
buf := []string{
"sriov:",
" - network: resource01 # network name",
" interface: net1",
" ip: 10.10.0.101 # network ip",
" - network: resource02 # network name",
" interface: net2",
" ip: 20.10.0.101 # network ip",
"gpu:",
" drivers: video,compute,utility",
"service:",
" type:",
" NodePort: 30080",
"#cloud-config",
"password: centos",
"chpasswd: { expire: False }",
"ssh_pwauth: True",
}
data := yaml.MapSlice{}
yaml.Unmarshal([]byte(strings.Join(buf, "\n")), &data)
ret, err := yamlconv.Subtract(data, []string{"sriov", "[0]"})
if err != nil {
panic(err.Error())
}
ret, err = yamlconv.Subtract(ret, []string{"sriov", "[0]", "network"})
if err != nil {
panic(err.Error())
}
ret, err = yamlconv.Subtract(ret, []string{"password"})
if err != nil {
panic(err.Error())
}
yamlconv.Print(ret, " ")
fmt.Printf("\n")
}
$ ./subtract
M[sriov]
A[0/1]
M[interface] Str{net2}
M[ip] Str{20.10.0.101}
M[gpu]
M[drivers] Str{video,compute,utility}
M[service]
M[type]
M[NodePort] Int{30080}
M[chpasswd]
M[expire] Bool{false}
M[ssh_pwauth] Bool{true}
$