github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/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 23 "github.com/spf13/cobra" 24 25 "github.com/codefresh-io/kcfi/pkg/helm-internal/completion" 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, Charts.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 Charts.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 func newShowCmd(out io.Writer) *cobra.Command { 55 client := action.NewShow(action.ShowAll) 56 57 showCommand := &cobra.Command{ 58 Use: "show", 59 Short: "show information of a chart", 60 Aliases: []string{"inspect"}, 61 Long: showDesc, 62 Args: require.NoArgs, 63 } 64 65 // Function providing dynamic auto-completion 66 validArgsFunc := func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) { 67 if len(args) != 0 { 68 return nil, completion.BashCompDirectiveNoFileComp 69 } 70 return compListCharts(toComplete, true) 71 } 72 73 all := &cobra.Command{ 74 Use: "all [CHART]", 75 Short: "shows all information of the chart", 76 Long: showAllDesc, 77 Args: require.ExactArgs(1), 78 RunE: func(cmd *cobra.Command, args []string) error { 79 client.OutputFormat = action.ShowAll 80 output, err := runShow(args, client) 81 if err != nil { 82 return err 83 } 84 fmt.Fprint(out, output) 85 return nil 86 }, 87 } 88 89 valuesSubCmd := &cobra.Command{ 90 Use: "values [CHART]", 91 Short: "shows the chart's values", 92 Long: showValuesDesc, 93 Args: require.ExactArgs(1), 94 RunE: func(cmd *cobra.Command, args []string) error { 95 client.OutputFormat = action.ShowValues 96 output, err := runShow(args, client) 97 if err != nil { 98 return err 99 } 100 fmt.Fprint(out, output) 101 return nil 102 }, 103 } 104 105 chartSubCmd := &cobra.Command{ 106 Use: "chart [CHART]", 107 Short: "shows the chart's definition", 108 Long: showChartDesc, 109 Args: require.ExactArgs(1), 110 RunE: func(cmd *cobra.Command, args []string) error { 111 client.OutputFormat = action.ShowChart 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 readmeSubCmd := &cobra.Command{ 122 Use: "readme [CHART]", 123 Short: "shows the chart's README", 124 Long: readmeChartDesc, 125 Args: require.ExactArgs(1), 126 RunE: func(cmd *cobra.Command, args []string) error { 127 client.OutputFormat = action.ShowReadme 128 output, err := runShow(args, client) 129 if err != nil { 130 return err 131 } 132 fmt.Fprint(out, output) 133 return nil 134 }, 135 } 136 137 cmds := []*cobra.Command{all, readmeSubCmd, valuesSubCmd, chartSubCmd} 138 for _, subCmd := range cmds { 139 addShowFlags(showCommand, subCmd, client) 140 141 // Register the completion function for each subcommand 142 completion.RegisterValidArgsFunc(subCmd, validArgsFunc) 143 } 144 145 return showCommand 146 } 147 148 func addShowFlags(showCmd *cobra.Command, subCmd *cobra.Command, client *action.Show) { 149 f := subCmd.Flags() 150 151 f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored") 152 addChartPathOptionsFlags(f, &client.ChartPathOptions) 153 showCmd.AddCommand(subCmd) 154 } 155 156 func runShow(args []string, client *action.Show) (string, error) { 157 debug("Original chart version: %q", client.Version) 158 if client.Version == "" && client.Devel { 159 debug("setting version to >0.0.0-0") 160 client.Version = ">0.0.0-0" 161 } 162 163 cp, err := client.ChartPathOptions.LocateChart(args[0], settings) 164 if err != nil { 165 return "", err 166 } 167 return client.Run(cp) 168 }