-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (48 loc) · 906 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"log"
//"time"
//"sync"
//"regexp"
"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)
}
var pattern string
fmt.Scanln(&pattern)
p, err := yamlpath.NewPath(pattern) //e.g., "$..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 {
fmt.Println(i.Value)
}
}