github.com/uhthomas/helm@v3.0.0-beta.3+incompatible/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  	"fmt"
    21  	"strings"
    22  
    23  	"sigs.k8s.io/yaml"
    24  
    25  	"helm.sh/helm/pkg/chart"
    26  	"helm.sh/helm/pkg/chart/loader"
    27  )
    28  
    29  type ShowOutputFormat string
    30  
    31  const (
    32  	ShowAll    ShowOutputFormat = "all"
    33  	ShowChart  ShowOutputFormat = "chart"
    34  	ShowValues ShowOutputFormat = "values"
    35  	ShowReadme ShowOutputFormat = "readme"
    36  )
    37  
    38  var readmeFileNames = []string{"readme.md", "readme.txt", "readme"}
    39  
    40  func (o ShowOutputFormat) String() string {
    41  	return string(o)
    42  }
    43  
    44  // Show is the action for checking a given release's information.
    45  //
    46  // It provides the implementation of 'helm show' and its respective subcommands.
    47  type Show struct {
    48  	OutputFormat ShowOutputFormat
    49  	ChartPathOptions
    50  }
    51  
    52  // NewShow creates a new Show object with the given configuration.
    53  func NewShow(output ShowOutputFormat) *Show {
    54  	return &Show{
    55  		OutputFormat: output,
    56  	}
    57  }
    58  
    59  // Run executes 'helm show' against the given release.
    60  func (s *Show) Run(chartpath string) (string, error) {
    61  	var out strings.Builder
    62  	chrt, err := loader.Load(chartpath)
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	cf, err := yaml.Marshal(chrt.Metadata)
    67  	if err != nil {
    68  		return "", err
    69  	}
    70  
    71  	if s.OutputFormat == ShowChart || s.OutputFormat == ShowAll {
    72  		fmt.Fprintf(&out, "%s\n", cf)
    73  	}
    74  
    75  	if (s.OutputFormat == ShowValues || s.OutputFormat == ShowAll) && chrt.Values != nil {
    76  		if s.OutputFormat == ShowAll {
    77  			fmt.Fprintln(&out, "---")
    78  		}
    79  		b, err := yaml.Marshal(chrt.Values)
    80  		if err != nil {
    81  			return "", err
    82  		}
    83  		fmt.Fprintf(&out, "%s\n", b)
    84  	}
    85  
    86  	if s.OutputFormat == ShowReadme || s.OutputFormat == ShowAll {
    87  		if s.OutputFormat == ShowAll {
    88  			fmt.Fprintln(&out, "---")
    89  		}
    90  		readme := findReadme(chrt.Files)
    91  		if readme == nil {
    92  			return out.String(), nil
    93  		}
    94  		fmt.Fprintf(&out, "%s\n", readme.Data)
    95  	}
    96  	return out.String(), nil
    97  }
    98  
    99  func findReadme(files []*chart.File) (file *chart.File) {
   100  	for _, file := range files {
   101  		for _, n := range readmeFileNames {
   102  			if strings.EqualFold(file.Name, n) {
   103  				return file
   104  			}
   105  		}
   106  	}
   107  	return nil
   108  }