github.com/defensepoint-snyk-test/helm-new@v0.0.0-20211130153739-c57ea64d6603/cmd/helm/inspect.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 "github.com/ghodss/yaml" 22 "github.com/golang/protobuf/ptypes/any" 23 "github.com/spf13/cobra" 24 "io" 25 "strings" 26 27 "k8s.io/helm/pkg/chartutil" 28 ) 29 30 const inspectDesc = ` 31 This command inspects a chart and displays information. It takes a chart reference 32 ('stable/drupal'), a full path to a directory or packaged chart, or a URL. 33 34 Inspect prints the contents of the Chart.yaml file and the values.yaml file. 35 ` 36 37 const inspectValuesDesc = ` 38 This command inspects a chart (directory, file, or URL) and displays the contents 39 of the values.yaml file 40 ` 41 42 const inspectChartDesc = ` 43 This command inspects a chart (directory, file, or URL) and displays the contents 44 of the Charts.yaml file 45 ` 46 47 const readmeChartDesc = ` 48 This command inspects a chart (directory, file, or URL) and displays the contents 49 of the README file 50 ` 51 52 type inspectCmd struct { 53 chartpath string 54 output string 55 verify bool 56 keyring string 57 out io.Writer 58 version string 59 repoURL string 60 username string 61 password string 62 63 certFile string 64 keyFile string 65 caFile string 66 } 67 68 const ( 69 chartOnly = "chart" 70 valuesOnly = "values" 71 readmeOnly = "readme" 72 all = "all" 73 ) 74 75 var readmeFileNames = []string{"readme.md", "readme.txt", "readme"} 76 77 func newInspectCmd(out io.Writer) *cobra.Command { 78 insp := &inspectCmd{ 79 out: out, 80 output: all, 81 } 82 83 inspectCommand := &cobra.Command{ 84 Use: "inspect [CHART]", 85 Short: "inspect a chart", 86 Long: inspectDesc, 87 RunE: func(cmd *cobra.Command, args []string) error { 88 if err := checkArgsLength(len(args), "chart name"); err != nil { 89 return err 90 } 91 cp, err := locateChartPath(insp.repoURL, insp.username, insp.password, args[0], insp.version, insp.verify, insp.keyring, 92 insp.certFile, insp.keyFile, insp.caFile) 93 if err != nil { 94 return err 95 } 96 insp.chartpath = cp 97 return insp.run() 98 }, 99 } 100 101 valuesSubCmd := &cobra.Command{ 102 Use: "values [CHART]", 103 Short: "shows inspect values", 104 Long: inspectValuesDesc, 105 RunE: func(cmd *cobra.Command, args []string) error { 106 insp.output = valuesOnly 107 if err := checkArgsLength(len(args), "chart name"); err != nil { 108 return err 109 } 110 cp, err := locateChartPath(insp.repoURL, insp.username, insp.password, args[0], insp.version, insp.verify, insp.keyring, 111 insp.certFile, insp.keyFile, insp.caFile) 112 if err != nil { 113 return err 114 } 115 insp.chartpath = cp 116 return insp.run() 117 }, 118 } 119 120 chartSubCmd := &cobra.Command{ 121 Use: "chart [CHART]", 122 Short: "shows inspect chart", 123 Long: inspectChartDesc, 124 RunE: func(cmd *cobra.Command, args []string) error { 125 insp.output = chartOnly 126 if err := checkArgsLength(len(args), "chart name"); err != nil { 127 return err 128 } 129 cp, err := locateChartPath(insp.repoURL, insp.username, insp.password, args[0], insp.version, insp.verify, insp.keyring, 130 insp.certFile, insp.keyFile, insp.caFile) 131 if err != nil { 132 return err 133 } 134 insp.chartpath = cp 135 return insp.run() 136 }, 137 } 138 139 readmeSubCmd := &cobra.Command{ 140 Use: "readme [CHART]", 141 Short: "shows inspect readme", 142 Long: readmeChartDesc, 143 RunE: func(cmd *cobra.Command, args []string) error { 144 insp.output = readmeOnly 145 if err := checkArgsLength(len(args), "chart name"); err != nil { 146 return err 147 } 148 cp, err := locateChartPath(insp.repoURL, insp.username, insp.password, args[0], insp.version, insp.verify, insp.keyring, 149 insp.certFile, insp.keyFile, insp.caFile) 150 if err != nil { 151 return err 152 } 153 insp.chartpath = cp 154 return insp.run() 155 }, 156 } 157 158 cmds := []*cobra.Command{inspectCommand, readmeSubCmd, valuesSubCmd, chartSubCmd} 159 vflag := "verify" 160 vdesc := "verify the provenance data for this chart" 161 for _, subCmd := range cmds { 162 subCmd.Flags().BoolVar(&insp.verify, vflag, false, vdesc) 163 } 164 165 kflag := "keyring" 166 kdesc := "path to the keyring containing public verification keys" 167 kdefault := defaultKeyring() 168 for _, subCmd := range cmds { 169 subCmd.Flags().StringVar(&insp.keyring, kflag, kdefault, kdesc) 170 } 171 172 verflag := "version" 173 verdesc := "version of the chart. By default, the newest chart is shown" 174 for _, subCmd := range cmds { 175 subCmd.Flags().StringVar(&insp.version, verflag, "", verdesc) 176 } 177 178 repoURL := "repo" 179 repoURLdesc := "chart repository url where to locate the requested chart" 180 for _, subCmd := range cmds { 181 subCmd.Flags().StringVar(&insp.repoURL, repoURL, "", repoURLdesc) 182 } 183 184 username := "username" 185 usernamedesc := "chart repository username where to locate the requested chart" 186 inspectCommand.Flags().StringVar(&insp.username, username, "", usernamedesc) 187 valuesSubCmd.Flags().StringVar(&insp.username, username, "", usernamedesc) 188 chartSubCmd.Flags().StringVar(&insp.username, username, "", usernamedesc) 189 190 password := "password" 191 passworddesc := "chart repository password where to locate the requested chart" 192 inspectCommand.Flags().StringVar(&insp.password, password, "", passworddesc) 193 valuesSubCmd.Flags().StringVar(&insp.password, password, "", passworddesc) 194 chartSubCmd.Flags().StringVar(&insp.password, password, "", passworddesc) 195 196 certFile := "cert-file" 197 certFiledesc := "verify certificates of HTTPS-enabled servers using this CA bundle" 198 for _, subCmd := range cmds { 199 subCmd.Flags().StringVar(&insp.certFile, certFile, "", certFiledesc) 200 } 201 202 keyFile := "key-file" 203 keyFiledesc := "identify HTTPS client using this SSL key file" 204 for _, subCmd := range cmds { 205 subCmd.Flags().StringVar(&insp.keyFile, keyFile, "", keyFiledesc) 206 } 207 208 caFile := "ca-file" 209 caFiledesc := "chart repository url where to locate the requested chart" 210 for _, subCmd := range cmds { 211 subCmd.Flags().StringVar(&insp.caFile, caFile, "", caFiledesc) 212 } 213 214 for _, subCmd := range cmds[1:] { 215 inspectCommand.AddCommand(subCmd) 216 } 217 218 return inspectCommand 219 } 220 221 func (i *inspectCmd) run() error { 222 chrt, err := chartutil.Load(i.chartpath) 223 if err != nil { 224 return err 225 } 226 cf, err := yaml.Marshal(chrt.Metadata) 227 if err != nil { 228 return err 229 } 230 231 if i.output == chartOnly || i.output == all { 232 fmt.Fprintln(i.out, string(cf)) 233 } 234 235 if (i.output == valuesOnly || i.output == all) && chrt.Values != nil { 236 if i.output == all { 237 fmt.Fprintln(i.out, "---") 238 } 239 fmt.Fprintln(i.out, chrt.Values.Raw) 240 } 241 242 if i.output == readmeOnly || i.output == all { 243 if i.output == all { 244 fmt.Fprintln(i.out, "---") 245 } 246 readme := findReadme(chrt.Files) 247 if readme == nil { 248 return nil 249 } 250 fmt.Fprintln(i.out, string(readme.Value)) 251 } 252 return nil 253 } 254 255 func findReadme(files []*any.Any) (file *any.Any) { 256 for _, file := range files { 257 if containsString(readmeFileNames, strings.ToLower(file.TypeUrl), nil) { 258 return file 259 } 260 } 261 return nil 262 } 263 264 // containsString checks if a given slice of strings contains the provided string. 265 // If a modifier func is provided, it is called with the slice item before the comparison. 266 func containsString(slice []string, s string, modifier func(s string) string) bool { 267 for _, item := range slice { 268 if item == s { 269 return true 270 } 271 if modifier != nil && modifier(item) == s { 272 return true 273 } 274 } 275 return false 276 }