github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/inspect.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/ghodss/yaml"
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/chartutil"
    27  )
    28  
    29  const inspectDesc = `
    30  This command inspects a chart and displays information. It takes a chart reference
    31  ('stable/drupal'), a full path to a directory or packaged chart, or a URL.
    32  
    33  Inspect prints the contents of the Chart.yaml file and the values.yaml file.
    34  `
    35  
    36  const inspectValuesDesc = `
    37  This command inspects a chart (directory, file, or URL) and displays the contents
    38  of the values.yaml file
    39  `
    40  
    41  const inspectChartDesc = `
    42  This command inspects a chart (directory, file, or URL) and displays the contents
    43  of the Charts.yaml file
    44  `
    45  
    46  type inspectCmd struct {
    47  	chartpath string
    48  	output    string
    49  	verify    bool
    50  	keyring   string
    51  	out       io.Writer
    52  	version   string
    53  }
    54  
    55  const (
    56  	chartOnly  = "chart"
    57  	valuesOnly = "values"
    58  	both       = "both"
    59  )
    60  
    61  func newInspectCmd(out io.Writer) *cobra.Command {
    62  	insp := &inspectCmd{
    63  		out:    out,
    64  		output: both,
    65  	}
    66  
    67  	inspectCommand := &cobra.Command{
    68  		Use:   "inspect [CHART]",
    69  		Short: "inspect a chart",
    70  		Long:  inspectDesc,
    71  		RunE: func(cmd *cobra.Command, args []string) error {
    72  			if err := checkArgsLength(len(args), "chart name"); err != nil {
    73  				return err
    74  			}
    75  			cp, err := locateChartPath(args[0], insp.version, insp.verify, insp.keyring)
    76  			if err != nil {
    77  				return err
    78  			}
    79  			insp.chartpath = cp
    80  			return insp.run()
    81  		},
    82  	}
    83  
    84  	valuesSubCmd := &cobra.Command{
    85  		Use:   "values [CHART]",
    86  		Short: "shows inspect values",
    87  		Long:  inspectValuesDesc,
    88  		RunE: func(cmd *cobra.Command, args []string) error {
    89  			insp.output = valuesOnly
    90  			if err := checkArgsLength(len(args), "chart name"); err != nil {
    91  				return err
    92  			}
    93  			cp, err := locateChartPath(args[0], insp.version, insp.verify, insp.keyring)
    94  			if err != nil {
    95  				return err
    96  			}
    97  			insp.chartpath = cp
    98  			return insp.run()
    99  		},
   100  	}
   101  
   102  	chartSubCmd := &cobra.Command{
   103  		Use:   "chart [CHART]",
   104  		Short: "shows inspect chart",
   105  		Long:  inspectChartDesc,
   106  		RunE: func(cmd *cobra.Command, args []string) error {
   107  			insp.output = chartOnly
   108  			if err := checkArgsLength(len(args), "chart name"); err != nil {
   109  				return err
   110  			}
   111  			cp, err := locateChartPath(args[0], insp.version, insp.verify, insp.keyring)
   112  			if err != nil {
   113  				return err
   114  			}
   115  			insp.chartpath = cp
   116  			return insp.run()
   117  		},
   118  	}
   119  
   120  	vflag := "verify"
   121  	vdesc := "verify the provenance data for this chart"
   122  	inspectCommand.Flags().BoolVar(&insp.verify, vflag, false, vdesc)
   123  	valuesSubCmd.Flags().BoolVar(&insp.verify, vflag, false, vdesc)
   124  	chartSubCmd.Flags().BoolVar(&insp.verify, vflag, false, vdesc)
   125  
   126  	kflag := "keyring"
   127  	kdesc := "path to the keyring containing public verification keys"
   128  	kdefault := defaultKeyring()
   129  	inspectCommand.Flags().StringVar(&insp.keyring, kflag, kdefault, kdesc)
   130  	valuesSubCmd.Flags().StringVar(&insp.keyring, kflag, kdefault, kdesc)
   131  	chartSubCmd.Flags().StringVar(&insp.keyring, kflag, kdefault, kdesc)
   132  
   133  	verflag := "version"
   134  	verdesc := "version of the chart. By default, the newest chart is shown"
   135  	inspectCommand.Flags().StringVar(&insp.version, verflag, "", verdesc)
   136  	valuesSubCmd.Flags().StringVar(&insp.version, verflag, "", verdesc)
   137  	chartSubCmd.Flags().StringVar(&insp.version, verflag, "", verdesc)
   138  
   139  	inspectCommand.AddCommand(valuesSubCmd)
   140  	inspectCommand.AddCommand(chartSubCmd)
   141  
   142  	return inspectCommand
   143  }
   144  
   145  func (i *inspectCmd) run() error {
   146  	chrt, err := chartutil.Load(i.chartpath)
   147  	if err != nil {
   148  		return err
   149  	}
   150  	cf, err := yaml.Marshal(chrt.Metadata)
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	if i.output == chartOnly || i.output == both {
   156  		fmt.Fprintln(i.out, string(cf))
   157  	}
   158  
   159  	if (i.output == valuesOnly || i.output == both) && chrt.Values != nil {
   160  		if i.output == both {
   161  			fmt.Fprintln(i.out, "---")
   162  		}
   163  		fmt.Fprintln(i.out, chrt.Values.Raw)
   164  	}
   165  
   166  	return nil
   167  }