github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/cmd/helm/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 main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"log"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"github.com/stefanmcshane/helm/cmd/helm/require"
    27  	"github.com/stefanmcshane/helm/pkg/action"
    28  )
    29  
    30  const showDesc = `
    31  This command consists of multiple subcommands to display information about a chart
    32  `
    33  
    34  const showAllDesc = `
    35  This command inspects a chart (directory, file, or URL) and displays all its content
    36  (values.yaml, Chart.yaml, README)
    37  `
    38  
    39  const showValuesDesc = `
    40  This command inspects a chart (directory, file, or URL) and displays the contents
    41  of the values.yaml file
    42  `
    43  
    44  const showChartDesc = `
    45  This command inspects a chart (directory, file, or URL) and displays the contents
    46  of the Chart.yaml file
    47  `
    48  
    49  const readmeChartDesc = `
    50  This command inspects a chart (directory, file, or URL) and displays the contents
    51  of the README file
    52  `
    53  
    54  const showCRDsDesc = `
    55  This command inspects a chart (directory, file, or URL) and displays the contents
    56  of the CustomResourceDefinition files
    57  `
    58  
    59  func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    60  	client := action.NewShowWithConfig(action.ShowAll, cfg)
    61  
    62  	showCommand := &cobra.Command{
    63  		Use:               "show",
    64  		Short:             "show information of a chart",
    65  		Aliases:           []string{"inspect"},
    66  		Long:              showDesc,
    67  		Args:              require.NoArgs,
    68  		ValidArgsFunction: noCompletions, // Disable file completion
    69  	}
    70  
    71  	// Function providing dynamic auto-completion
    72  	validArgsFunc := func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    73  		if len(args) != 0 {
    74  			return nil, cobra.ShellCompDirectiveNoFileComp
    75  		}
    76  		return compListCharts(toComplete, true)
    77  	}
    78  
    79  	all := &cobra.Command{
    80  		Use:               "all [CHART]",
    81  		Short:             "show all information of the chart",
    82  		Long:              showAllDesc,
    83  		Args:              require.ExactArgs(1),
    84  		ValidArgsFunction: validArgsFunc,
    85  		RunE: func(cmd *cobra.Command, args []string) error {
    86  			client.OutputFormat = action.ShowAll
    87  			output, err := runShow(args, client)
    88  			if err != nil {
    89  				return err
    90  			}
    91  			fmt.Fprint(out, output)
    92  			return nil
    93  		},
    94  	}
    95  
    96  	valuesSubCmd := &cobra.Command{
    97  		Use:               "values [CHART]",
    98  		Short:             "show the chart's values",
    99  		Long:              showValuesDesc,
   100  		Args:              require.ExactArgs(1),
   101  		ValidArgsFunction: validArgsFunc,
   102  		RunE: func(cmd *cobra.Command, args []string) error {
   103  			client.OutputFormat = action.ShowValues
   104  			output, err := runShow(args, client)
   105  			if err != nil {
   106  				return err
   107  			}
   108  			fmt.Fprint(out, output)
   109  			return nil
   110  		},
   111  	}
   112  
   113  	chartSubCmd := &cobra.Command{
   114  		Use:               "chart [CHART]",
   115  		Short:             "show the chart's definition",
   116  		Long:              showChartDesc,
   117  		Args:              require.ExactArgs(1),
   118  		ValidArgsFunction: validArgsFunc,
   119  		RunE: func(cmd *cobra.Command, args []string) error {
   120  			client.OutputFormat = action.ShowChart
   121  			output, err := runShow(args, client)
   122  			if err != nil {
   123  				return err
   124  			}
   125  			fmt.Fprint(out, output)
   126  			return nil
   127  		},
   128  	}
   129  
   130  	readmeSubCmd := &cobra.Command{
   131  		Use:               "readme [CHART]",
   132  		Short:             "show the chart's README",
   133  		Long:              readmeChartDesc,
   134  		Args:              require.ExactArgs(1),
   135  		ValidArgsFunction: validArgsFunc,
   136  		RunE: func(cmd *cobra.Command, args []string) error {
   137  			client.OutputFormat = action.ShowReadme
   138  			output, err := runShow(args, client)
   139  			if err != nil {
   140  				return err
   141  			}
   142  			fmt.Fprint(out, output)
   143  			return nil
   144  		},
   145  	}
   146  
   147  	crdsSubCmd := &cobra.Command{
   148  		Use:               "crds [CHART]",
   149  		Short:             "show the chart's CRDs",
   150  		Long:              showCRDsDesc,
   151  		Args:              require.ExactArgs(1),
   152  		ValidArgsFunction: validArgsFunc,
   153  		RunE: func(cmd *cobra.Command, args []string) error {
   154  			client.OutputFormat = action.ShowCRDs
   155  			output, err := runShow(args, client)
   156  			if err != nil {
   157  				return err
   158  			}
   159  			fmt.Fprint(out, output)
   160  			return nil
   161  		},
   162  	}
   163  
   164  	cmds := []*cobra.Command{all, readmeSubCmd, valuesSubCmd, chartSubCmd, crdsSubCmd}
   165  	for _, subCmd := range cmds {
   166  		addShowFlags(subCmd, client)
   167  		showCommand.AddCommand(subCmd)
   168  	}
   169  
   170  	return showCommand
   171  }
   172  
   173  func addShowFlags(subCmd *cobra.Command, client *action.Show) {
   174  	f := subCmd.Flags()
   175  
   176  	f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored")
   177  	if subCmd.Name() == "values" {
   178  		f.StringVar(&client.JSONPathTemplate, "jsonpath", "", "supply a JSONPath expression to filter the output")
   179  	}
   180  	addChartPathOptionsFlags(f, &client.ChartPathOptions)
   181  
   182  	err := subCmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   183  		if len(args) != 1 {
   184  			return nil, cobra.ShellCompDirectiveNoFileComp
   185  		}
   186  		return compVersionFlag(args[0], toComplete)
   187  	})
   188  
   189  	if err != nil {
   190  		log.Fatal(err)
   191  	}
   192  }
   193  
   194  func runShow(args []string, client *action.Show) (string, error) {
   195  	debug("Original chart version: %q", client.Version)
   196  	if client.Version == "" && client.Devel {
   197  		debug("setting version to >0.0.0-0")
   198  		client.Version = ">0.0.0-0"
   199  	}
   200  
   201  	cp, err := client.ChartPathOptions.LocateChart(args[0], settings)
   202  	if err != nil {
   203  		return "", err
   204  	}
   205  	return client.Run(cp)
   206  }