github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/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 cmd 18 19 import ( 20 "fmt" 21 "io" 22 "log" 23 24 "github.com/spf13/cobra" 25 26 "helm.sh/helm/v3/cmd/helm/require" 27 "helm.sh/helm/v3/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 err := addRegistryClient(client) 88 if err != nil { 89 return err 90 } 91 output, err := runShow(args, client) 92 if err != nil { 93 return err 94 } 95 fmt.Fprint(out, output) 96 return nil 97 }, 98 } 99 100 valuesSubCmd := &cobra.Command{ 101 Use: "values [CHART]", 102 Short: "show the chart's values", 103 Long: showValuesDesc, 104 Args: require.ExactArgs(1), 105 ValidArgsFunction: validArgsFunc, 106 RunE: func(cmd *cobra.Command, args []string) error { 107 client.OutputFormat = action.ShowValues 108 err := addRegistryClient(client) 109 if err != nil { 110 return err 111 } 112 output, err := runShow(args, client) 113 if err != nil { 114 return err 115 } 116 fmt.Fprint(out, output) 117 return nil 118 }, 119 } 120 121 chartSubCmd := &cobra.Command{ 122 Use: "chart [CHART]", 123 Short: "show the chart's definition", 124 Long: showChartDesc, 125 Args: require.ExactArgs(1), 126 ValidArgsFunction: validArgsFunc, 127 RunE: func(cmd *cobra.Command, args []string) error { 128 client.OutputFormat = action.ShowChart 129 err := addRegistryClient(client) 130 if err != nil { 131 return err 132 } 133 output, err := runShow(args, client) 134 if err != nil { 135 return err 136 } 137 fmt.Fprint(out, output) 138 return nil 139 }, 140 } 141 142 readmeSubCmd := &cobra.Command{ 143 Use: "readme [CHART]", 144 Short: "show the chart's README", 145 Long: readmeChartDesc, 146 Args: require.ExactArgs(1), 147 ValidArgsFunction: validArgsFunc, 148 RunE: func(cmd *cobra.Command, args []string) error { 149 client.OutputFormat = action.ShowReadme 150 err := addRegistryClient(client) 151 if err != nil { 152 return err 153 } 154 output, err := runShow(args, client) 155 if err != nil { 156 return err 157 } 158 fmt.Fprint(out, output) 159 return nil 160 }, 161 } 162 163 crdsSubCmd := &cobra.Command{ 164 Use: "crds [CHART]", 165 Short: "show the chart's CRDs", 166 Long: showCRDsDesc, 167 Args: require.ExactArgs(1), 168 ValidArgsFunction: validArgsFunc, 169 RunE: func(cmd *cobra.Command, args []string) error { 170 client.OutputFormat = action.ShowCRDs 171 err := addRegistryClient(client) 172 if err != nil { 173 return err 174 } 175 output, err := runShow(args, client) 176 if err != nil { 177 return err 178 } 179 fmt.Fprint(out, output) 180 return nil 181 }, 182 } 183 184 cmds := []*cobra.Command{all, readmeSubCmd, valuesSubCmd, chartSubCmd, crdsSubCmd} 185 for _, subCmd := range cmds { 186 addShowFlags(subCmd, client) 187 showCommand.AddCommand(subCmd) 188 } 189 190 return showCommand 191 } 192 193 func addShowFlags(subCmd *cobra.Command, client *action.Show) { 194 f := subCmd.Flags() 195 196 f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored") 197 if subCmd.Name() == "values" { 198 f.StringVar(&client.JSONPathTemplate, "jsonpath", "", "supply a JSONPath expression to filter the output") 199 } 200 addChartPathOptionsFlags(f, &client.ChartPathOptions) 201 202 err := subCmd.RegisterFlagCompletionFunc("version", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 203 if len(args) != 1 { 204 return nil, cobra.ShellCompDirectiveNoFileComp 205 } 206 return compVersionFlag(args[0], toComplete) 207 }) 208 209 if err != nil { 210 log.Fatal(err) 211 } 212 } 213 214 func runShow(args []string, client *action.Show) (string, error) { 215 debug("Original chart version: %q", client.Version) 216 if client.Version == "" && client.Devel { 217 debug("setting version to >0.0.0-0") 218 client.Version = ">0.0.0-0" 219 } 220 221 cp, err := client.ChartPathOptions.LocateChart(args[0], settings) 222 if err != nil { 223 return "", err 224 } 225 return client.Run(cp) 226 } 227 228 func addRegistryClient(client *action.Show) error { 229 registryClient, err := newRegistryClient(client.CertFile, client.KeyFile, client.CaFile, client.InsecureSkipTLSverify) 230 if err != nil { 231 return fmt.Errorf("missing registry client: %w", err) 232 } 233 client.SetRegistryClient(registryClient) 234 return nil 235 }