github.com/werf/3p-helm@v2.8.1+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  	repoURL   string
    54  
    55  	certFile string
    56  	keyFile  string
    57  	caFile   string
    58  }
    59  
    60  const (
    61  	chartOnly  = "chart"
    62  	valuesOnly = "values"
    63  	both       = "both"
    64  )
    65  
    66  func newInspectCmd(out io.Writer) *cobra.Command {
    67  	insp := &inspectCmd{
    68  		out:    out,
    69  		output: both,
    70  	}
    71  
    72  	inspectCommand := &cobra.Command{
    73  		Use:   "inspect [CHART]",
    74  		Short: "inspect a chart",
    75  		Long:  inspectDesc,
    76  		RunE: func(cmd *cobra.Command, args []string) error {
    77  			if err := checkArgsLength(len(args), "chart name"); err != nil {
    78  				return err
    79  			}
    80  			cp, err := locateChartPath(insp.repoURL, args[0], insp.version, insp.verify, insp.keyring,
    81  				insp.certFile, insp.keyFile, insp.caFile)
    82  			if err != nil {
    83  				return err
    84  			}
    85  			insp.chartpath = cp
    86  			return insp.run()
    87  		},
    88  	}
    89  
    90  	valuesSubCmd := &cobra.Command{
    91  		Use:   "values [CHART]",
    92  		Short: "shows inspect values",
    93  		Long:  inspectValuesDesc,
    94  		RunE: func(cmd *cobra.Command, args []string) error {
    95  			insp.output = valuesOnly
    96  			if err := checkArgsLength(len(args), "chart name"); err != nil {
    97  				return err
    98  			}
    99  			cp, err := locateChartPath(insp.repoURL, args[0], insp.version, insp.verify, insp.keyring,
   100  				insp.certFile, insp.keyFile, insp.caFile)
   101  			if err != nil {
   102  				return err
   103  			}
   104  			insp.chartpath = cp
   105  			return insp.run()
   106  		},
   107  	}
   108  
   109  	chartSubCmd := &cobra.Command{
   110  		Use:   "chart [CHART]",
   111  		Short: "shows inspect chart",
   112  		Long:  inspectChartDesc,
   113  		RunE: func(cmd *cobra.Command, args []string) error {
   114  			insp.output = chartOnly
   115  			if err := checkArgsLength(len(args), "chart name"); err != nil {
   116  				return err
   117  			}
   118  			cp, err := locateChartPath(insp.repoURL, args[0], insp.version, insp.verify, insp.keyring,
   119  				insp.certFile, insp.keyFile, insp.caFile)
   120  			if err != nil {
   121  				return err
   122  			}
   123  			insp.chartpath = cp
   124  			return insp.run()
   125  		},
   126  	}
   127  
   128  	vflag := "verify"
   129  	vdesc := "verify the provenance data for this chart"
   130  	inspectCommand.Flags().BoolVar(&insp.verify, vflag, false, vdesc)
   131  	valuesSubCmd.Flags().BoolVar(&insp.verify, vflag, false, vdesc)
   132  	chartSubCmd.Flags().BoolVar(&insp.verify, vflag, false, vdesc)
   133  
   134  	kflag := "keyring"
   135  	kdesc := "path to the keyring containing public verification keys"
   136  	kdefault := defaultKeyring()
   137  	inspectCommand.Flags().StringVar(&insp.keyring, kflag, kdefault, kdesc)
   138  	valuesSubCmd.Flags().StringVar(&insp.keyring, kflag, kdefault, kdesc)
   139  	chartSubCmd.Flags().StringVar(&insp.keyring, kflag, kdefault, kdesc)
   140  
   141  	verflag := "version"
   142  	verdesc := "version of the chart. By default, the newest chart is shown"
   143  	inspectCommand.Flags().StringVar(&insp.version, verflag, "", verdesc)
   144  	valuesSubCmd.Flags().StringVar(&insp.version, verflag, "", verdesc)
   145  	chartSubCmd.Flags().StringVar(&insp.version, verflag, "", verdesc)
   146  
   147  	repoURL := "repo"
   148  	repoURLdesc := "chart repository url where to locate the requested chart"
   149  	inspectCommand.Flags().StringVar(&insp.repoURL, repoURL, "", repoURLdesc)
   150  	valuesSubCmd.Flags().StringVar(&insp.repoURL, repoURL, "", repoURLdesc)
   151  	chartSubCmd.Flags().StringVar(&insp.repoURL, repoURL, "", repoURLdesc)
   152  
   153  	certFile := "cert-file"
   154  	certFiledesc := "verify certificates of HTTPS-enabled servers using this CA bundle"
   155  	inspectCommand.Flags().StringVar(&insp.certFile, certFile, "", certFiledesc)
   156  	valuesSubCmd.Flags().StringVar(&insp.certFile, certFile, "", certFiledesc)
   157  	chartSubCmd.Flags().StringVar(&insp.certFile, certFile, "", certFiledesc)
   158  
   159  	keyFile := "key-file"
   160  	keyFiledesc := "identify HTTPS client using this SSL key file"
   161  	inspectCommand.Flags().StringVar(&insp.keyFile, keyFile, "", keyFiledesc)
   162  	valuesSubCmd.Flags().StringVar(&insp.keyFile, keyFile, "", keyFiledesc)
   163  	chartSubCmd.Flags().StringVar(&insp.keyFile, keyFile, "", keyFiledesc)
   164  
   165  	caFile := "ca-file"
   166  	caFiledesc := "chart repository url where to locate the requested chart"
   167  	inspectCommand.Flags().StringVar(&insp.caFile, caFile, "", caFiledesc)
   168  	valuesSubCmd.Flags().StringVar(&insp.caFile, caFile, "", caFiledesc)
   169  	chartSubCmd.Flags().StringVar(&insp.caFile, caFile, "", caFiledesc)
   170  
   171  	inspectCommand.AddCommand(valuesSubCmd)
   172  	inspectCommand.AddCommand(chartSubCmd)
   173  
   174  	return inspectCommand
   175  }
   176  
   177  func (i *inspectCmd) run() error {
   178  	chrt, err := chartutil.Load(i.chartpath)
   179  	if err != nil {
   180  		return err
   181  	}
   182  	cf, err := yaml.Marshal(chrt.Metadata)
   183  	if err != nil {
   184  		return err
   185  	}
   186  
   187  	if i.output == chartOnly || i.output == both {
   188  		fmt.Fprintln(i.out, string(cf))
   189  	}
   190  
   191  	if (i.output == valuesOnly || i.output == both) && chrt.Values != nil {
   192  		if i.output == both {
   193  			fmt.Fprintln(i.out, "---")
   194  		}
   195  		fmt.Fprintln(i.out, chrt.Values.Raw)
   196  	}
   197  
   198  	return nil
   199  }