feature(cmd): `helm dep list` with transitive dependencies

Signed-off-by: Yonatan Kahana <yonatankahana.il@gmail.com>
pull/8179/head
Yonatan Kahana 5 years ago
parent e29ed7a108
commit ceb05ac478

@ -100,7 +100,7 @@ func newDependencyCmd(out io.Writer) *cobra.Command {
func newDependencyListCmd(out io.Writer) *cobra.Command {
client := action.NewDependency()
var transitive *bool
cmd := &cobra.Command{
Use: "list CHART",
Aliases: []string{"ls"},
@ -112,8 +112,11 @@ func newDependencyListCmd(out io.Writer) *cobra.Command {
if len(args) > 0 {
chartpath = filepath.Clean(args[0])
}
return client.List(chartpath, out)
return client.List(chartpath, out, *transitive)
},
}
transitive = cmd.Flags().Bool("transitive", false, "show transitive dependencies (dependencies of dependencies)")
return cmd
}

@ -44,7 +44,7 @@ func NewDependency() *Dependency {
}
// List executes 'helm dependency list'.
func (d *Dependency) List(chartpath string, out io.Writer) error {
func (d *Dependency) List(chartpath string, out io.Writer, transitive bool) error {
c, err := loader.Load(chartpath)
if err != nil {
return err
@ -55,7 +55,17 @@ func (d *Dependency) List(chartpath string, out io.Writer) error {
return nil
}
d.printDependencies(chartpath, out, c)
table := uitable.New()
table.MaxColWidth = 80
table.AddRow("NAME", "VERSION", "REPOSITORY", "STATUS")
d.printDependencies(table, chartpath, c)
if transitive {
d.printTransitiveDependencies(table, c)
}
fmt.Fprintln(out, table)
fmt.Fprintln(out)
d.printMissing(chartpath, out, c.Metadata.Dependencies)
return nil
@ -138,14 +148,23 @@ func (d *Dependency) dependencyStatus(chartpath string, dep *chart.Dependency, p
}
// printDependencies prints all of the dependencies in the yaml file.
func (d *Dependency) printDependencies(chartpath string, out io.Writer, c *chart.Chart) {
table := uitable.New()
table.MaxColWidth = 80
table.AddRow("NAME", "VERSION", "REPOSITORY", "STATUS")
func (d *Dependency) printDependencies(table *uitable.Table, chartpath string, c *chart.Chart) {
for _, row := range c.Metadata.Dependencies {
table.AddRow(row.Name, row.Version, row.Repository, d.dependencyStatus(chartpath, row, c))
}
fmt.Fprintln(out, table)
}
// printTransitiveDependencies prints all the transitive dependencies in a given chart.
func (d *Dependency) printTransitiveDependencies(table *uitable.Table, c *chart.Chart) {
for _, sc := range c.Dependencies() {
if sc.Lock != nil {
for _, td := range sc.Lock.Dependencies {
table.AddRow(td.Name, td.Version, td.Repository, "transitive")
}
}
d.printTransitiveDependencies(table, sc)
}
}
// printMissing prints warnings about charts that are present on disk, but are

@ -27,6 +27,7 @@ func TestList(t *testing.T) {
for _, tcase := range []struct {
chart string
golden string
transitive bool
}{
{
chart: "testdata/charts/chart-with-compressed-dependencies",
@ -48,9 +49,14 @@ func TestList(t *testing.T) {
chart: "testdata/charts/chart-missing-deps",
golden: "output/missing-deps.txt",
},
{
chart: "testdata/charts/transitive-dependencies",
golden: "output/transitive-dependencies.txt",
transitive: true,
},
} {
buf := bytes.Buffer{}
if err := NewDependency().List(tcase.chart, &buf); err != nil {
if err := NewDependency().List(tcase.chart, &buf, tcase.transitive); err != nil {
t.Fatal(err)
}
test.AssertGoldenBytes(t, buf.Bytes(), tcase.golden)

@ -0,0 +1,9 @@
apiVersion: v1
appVersion: "1.0"
description: A Helm chart
name: my-chart
version: 0.1.0
dependencies:
- name: wordpress
version: 5.0.2
repository: https://kubernetes-charts.storage.googleapis.com

@ -0,0 +1,6 @@
dependencies:
- name: wordpress
repository: https://kubernetes-charts.storage.googleapis.com
version: 5.0.2
digest: sha256:47874170647e0698d5fc4cb23bbc97e3e2bf80d841cffa0a2fb2688aa4a35372
generated: "2020-05-21T22:52:53.59605223+03:00"

@ -0,0 +1,4 @@
NAME VERSION REPOSITORY STATUS
wordpress 5.0.2 https://kubernetes-charts.storage.googleapis.com ok
mariadb 5.2.5 https://kubernetes-charts.storage.googleapis.com/ transitive
Loading…
Cancel
Save