-
Notifications
You must be signed in to change notification settings - Fork 61
/
list-chart-dependencies.go
executable file
·84 lines (73 loc) · 1.92 KB
/
list-chart-dependencies.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
///usr/bin/go run "$0" "$@"; exit $?
//
// This program parses the dependencies in all Chart.yaml and requirements.yaml
// into a stream of JSON documents, for further analysis with jq(1). Run in the
// repo root without any arguments.
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
func main() {
must(filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsRegular() && filepath.Base(path) == "Chart.yaml" {
buf, err := ioutil.ReadFile(path)
must(err)
chart := chartData{Path: filepath.Dir(path)}
must(yaml.Unmarshal(buf, &chart))
if len(chart.Dependencies) > 0 {
for _, dep := range chart.Dependencies {
dependency{chart, dep}.SerializeTo(os.Stdout)
}
} else {
buf, err := ioutil.ReadFile(filepath.Join(chart.Path, "requirements.yaml"))
if os.IsNotExist(err) {
return nil
}
must(err)
var reqs requirementsData
must(yaml.Unmarshal(buf, &reqs))
for _, dep := range reqs.Dependencies {
dependency{chart, dep}.SerializeTo(os.Stdout)
}
}
}
return nil
}))
}
type chartData struct {
Path string `yaml:"-" json:"path"`
Name string `yaml:"name" json:"name"`
Version string `yaml:"version" json:"version"`
Dependencies []depData `yaml:"dependencies" json:"-"`
}
type requirementsData struct {
Dependencies []depData `yaml:"dependencies"`
}
type depData struct {
Name string `json:"name" yaml:"name"`
Repository string `json:"repository" yaml:"repository"`
Version string `json:"version" yaml:"version"`
}
type dependency struct {
Parent chartData `json:"parent"`
Dependency depData `json:"dependency"`
}
func (d dependency) SerializeTo(w io.Writer) {
buf, err := json.Marshal(d)
must(err)
fmt.Fprintln(w, string(buf))
}
func must(err error) {
if err != nil {
panic(err.Error())
}
}