...

Source file src/helm.sh/helm/v3/pkg/action/show.go

Documentation: helm.sh/helm/v3/pkg/action

     1  /*
     2  Copyright The Helm Authors.
     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 action
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	"k8s.io/cli-runtime/pkg/printers"
    26  	"sigs.k8s.io/yaml"
    27  
    28  	"helm.sh/helm/v3/pkg/chart"
    29  	"helm.sh/helm/v3/pkg/chart/loader"
    30  	"helm.sh/helm/v3/pkg/chartutil"
    31  	"helm.sh/helm/v3/pkg/registry"
    32  )
    33  
    34  // ShowOutputFormat is the format of the output of `helm show`
    35  type ShowOutputFormat string
    36  
    37  const (
    38  	// ShowAll is the format which shows all the information of a chart
    39  	ShowAll ShowOutputFormat = "all"
    40  	// ShowChart is the format which only shows the chart's definition
    41  	ShowChart ShowOutputFormat = "chart"
    42  	// ShowValues is the format which only shows the chart's values
    43  	ShowValues ShowOutputFormat = "values"
    44  	// ShowReadme is the format which only shows the chart's README
    45  	ShowReadme ShowOutputFormat = "readme"
    46  	// ShowCRDs is the format which only shows the chart's CRDs
    47  	ShowCRDs ShowOutputFormat = "crds"
    48  )
    49  
    50  var readmeFileNames = []string{"readme.md", "readme.txt", "readme"}
    51  
    52  func (o ShowOutputFormat) String() string {
    53  	return string(o)
    54  }
    55  
    56  // Show is the action for checking a given release's information.
    57  //
    58  // It provides the implementation of 'helm show' and its respective subcommands.
    59  type Show struct {
    60  	ChartPathOptions
    61  	Devel            bool
    62  	OutputFormat     ShowOutputFormat
    63  	JSONPathTemplate string
    64  	chart            *chart.Chart // for testing
    65  }
    66  
    67  // NewShow creates a new Show object with the given configuration.
    68  // Deprecated: Use NewShowWithConfig
    69  // TODO Helm 4: Fold NewShowWithConfig back into NewShow
    70  func NewShow(output ShowOutputFormat) *Show {
    71  	return &Show{
    72  		OutputFormat: output,
    73  	}
    74  }
    75  
    76  // NewShowWithConfig creates a new Show object with the given configuration.
    77  func NewShowWithConfig(output ShowOutputFormat, cfg *Configuration) *Show {
    78  	sh := &Show{
    79  		OutputFormat: output,
    80  	}
    81  	sh.ChartPathOptions.registryClient = cfg.RegistryClient
    82  
    83  	return sh
    84  }
    85  
    86  // SetRegistryClient sets the registry client to use when pulling a chart from a registry.
    87  func (s *Show) SetRegistryClient(client *registry.Client) {
    88  	s.ChartPathOptions.registryClient = client
    89  }
    90  
    91  // Run executes 'helm show' against the given release.
    92  func (s *Show) Run(chartpath string) (string, error) {
    93  	if s.chart == nil {
    94  		chrt, err := loader.Load(chartpath)
    95  		if err != nil {
    96  			return "", err
    97  		}
    98  		s.chart = chrt
    99  	}
   100  	cf, err := yaml.Marshal(s.chart.Metadata)
   101  	if err != nil {
   102  		return "", err
   103  	}
   104  
   105  	var out strings.Builder
   106  	if s.OutputFormat == ShowChart || s.OutputFormat == ShowAll {
   107  		fmt.Fprintf(&out, "%s\n", cf)
   108  	}
   109  
   110  	if (s.OutputFormat == ShowValues || s.OutputFormat == ShowAll) && s.chart.Values != nil {
   111  		if s.OutputFormat == ShowAll {
   112  			fmt.Fprintln(&out, "---")
   113  		}
   114  		if s.JSONPathTemplate != "" {
   115  			printer, err := printers.NewJSONPathPrinter(s.JSONPathTemplate)
   116  			if err != nil {
   117  				return "", errors.Wrapf(err, "error parsing jsonpath %s", s.JSONPathTemplate)
   118  			}
   119  			printer.Execute(&out, s.chart.Values)
   120  		} else {
   121  			for _, f := range s.chart.Raw {
   122  				if f.Name == chartutil.ValuesfileName {
   123  					fmt.Fprintln(&out, string(f.Data))
   124  				}
   125  			}
   126  		}
   127  	}
   128  
   129  	if s.OutputFormat == ShowReadme || s.OutputFormat == ShowAll {
   130  		readme := findReadme(s.chart.Files)
   131  		if readme != nil {
   132  			if s.OutputFormat == ShowAll {
   133  				fmt.Fprintln(&out, "---")
   134  			}
   135  			fmt.Fprintf(&out, "%s\n", readme.Data)
   136  		}
   137  	}
   138  
   139  	if s.OutputFormat == ShowCRDs || s.OutputFormat == ShowAll {
   140  		crds := s.chart.CRDObjects()
   141  		if len(crds) > 0 {
   142  			if s.OutputFormat == ShowAll && !bytes.HasPrefix(crds[0].File.Data, []byte("---")) {
   143  				fmt.Fprintln(&out, "---")
   144  			}
   145  			for _, crd := range crds {
   146  				fmt.Fprintf(&out, "%s\n", string(crd.File.Data))
   147  			}
   148  		}
   149  	}
   150  	return out.String(), nil
   151  }
   152  
   153  func findReadme(files []*chart.File) (file *chart.File) {
   154  	for _, file := range files {
   155  		for _, n := range readmeFileNames {
   156  			if file == nil {
   157  				continue
   158  			}
   159  			if strings.EqualFold(file.Name, n) {
   160  				return file
   161  			}
   162  		}
   163  	}
   164  	return nil
   165  }
   166  

View as plain text