From c5cc02a611834e6f5dcf39df988a615b1f1e7954 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Wed, 20 Jul 2016 16:29:24 -0600 Subject: [PATCH] feat(helm): add 'helm inspect' This basic version of helm inspect displays the Chart.yaml and values.yaml files on STDOUT. Closes #967 --- cmd/helm/helm.go | 1 + cmd/helm/inspect.go | 81 +++++++++++++++++++ cmd/helm/inspect_test.go | 63 +++++++++++++++ .../testdata/testcharts/alpine/Chart.yaml | 6 +- 4 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 cmd/helm/inspect.go create mode 100644 cmd/helm/inspect_test.go diff --git a/cmd/helm/helm.go b/cmd/helm/helm.go index 3a089ced9..a7be03373 100644 --- a/cmd/helm/helm.go +++ b/cmd/helm/helm.go @@ -90,6 +90,7 @@ func newRootCmd(out io.Writer) *cobra.Command { newStatusCmd(nil, out), newInstallCmd(nil, out), newDeleteCmd(nil, out), + newInspectCmd(nil, out), ) return cmd } diff --git a/cmd/helm/inspect.go b/cmd/helm/inspect.go new file mode 100644 index 000000000..b3276fc41 --- /dev/null +++ b/cmd/helm/inspect.go @@ -0,0 +1,81 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + "io" + + "github.com/ghodss/yaml" + "github.com/spf13/cobra" + + "k8s.io/helm/pkg/chartutil" + "k8s.io/helm/pkg/helm" +) + +const inspectDesc = ` +This command inspects a chart (directory, file, or URL) and displays information. + +Inspect prints the contents of the Chart.yaml file and the values.yaml file. +` + +type inspectCmd struct { + chartpath string + + out io.Writer + client helm.Interface +} + +func newInspectCmd(c helm.Interface, out io.Writer) *cobra.Command { + insp := &inspectCmd{ + client: c, + out: out, + } + + cc := &cobra.Command{ + Use: "inspect [CHART]", + Short: "inspect a chart", + Long: installDesc, + RunE: func(cmd *cobra.Command, args []string) error { + if err := checkArgsLength(1, len(args), "chart name"); err != nil { + return err + } + cp, err := locateChartPath(args[0]) + if err != nil { + return err + } + insp.chartpath = cp + return insp.run() + }, + } + return cc +} + +func (i *inspectCmd) run() error { + chrt, err := chartutil.Load(i.chartpath) + if err != nil { + return err + } + cf, err := yaml.Marshal(chrt.Metadata) + if err != nil { + return err + } + fmt.Fprintln(i.out, string(cf)) + fmt.Fprintln(i.out, "---") + fmt.Fprintln(i.out, chrt.Values.Raw) + return nil +} diff --git a/cmd/helm/inspect_test.go b/cmd/helm/inspect_test.go new file mode 100644 index 000000000..815e13813 --- /dev/null +++ b/cmd/helm/inspect_test.go @@ -0,0 +1,63 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "bytes" + "io/ioutil" + "strings" + "testing" +) + +func TestInspect(t *testing.T) { + b := bytes.NewBuffer(nil) + + insp := &inspectCmd{ + chartpath: "testdata/testcharts/alpine", + out: b, + } + insp.run() + + // Load the data from the textfixture directly. + cdata, err := ioutil.ReadFile("testdata/testcharts/alpine/Chart.yaml") + if err != nil { + t.Fatal(err) + } + data, err := ioutil.ReadFile("testdata/testcharts/alpine/values.yaml") + if err != nil { + t.Fatal(err) + } + + parts := strings.SplitN(b.String(), "---", 2) + if len(parts) != 2 { + t.Fatalf("Expected 2 parts, got %d", len(parts)) + } + + expect := []string{ + strings.TrimSpace(string(cdata)), + strings.TrimSpace(string(data)), + } + + // Problem: ghodss/yaml doesn't marshal into struct order. To solve, we + // have to carefully craft the Chart.yaml to match. + for i, got := range parts { + got = strings.TrimSpace(got) + if got != expect[i] { + t.Errorf("Expected\n%q\nGot\n%q\n", expect[i], got) + } + } +} diff --git a/cmd/helm/testdata/testcharts/alpine/Chart.yaml b/cmd/helm/testdata/testcharts/alpine/Chart.yaml index bb2602087..6fbb27f18 100644 --- a/cmd/helm/testdata/testcharts/alpine/Chart.yaml +++ b/cmd/helm/testdata/testcharts/alpine/Chart.yaml @@ -1,6 +1,6 @@ -name: alpine description: Deploy a basic Alpine Linux pod -version: 0.1.0 home: https://k8s.io/helm +name: alpine sources: - - https://github.com/kubernetes/helm +- https://github.com/kubernetes/helm +version: 0.1.0