github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/action/show.go (about)

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