...
1
16
17 package action
18
19 import (
20 "bytes"
21 "fmt"
22 "strings"
23
24 "github.com/pkg/errors"
25 "k8s.io/cli-runtime/pkg/printers"
26 "sigs.k8s.io/yaml"
27
28 "helm.sh/helm/v3/pkg/chart"
29 "helm.sh/helm/v3/pkg/chart/loader"
30 "helm.sh/helm/v3/pkg/chartutil"
31 "helm.sh/helm/v3/pkg/registry"
32 )
33
34
35 type ShowOutputFormat string
36
37 const (
38
39 ShowAll ShowOutputFormat = "all"
40
41 ShowChart ShowOutputFormat = "chart"
42
43 ShowValues ShowOutputFormat = "values"
44
45 ShowReadme ShowOutputFormat = "readme"
46
47 ShowCRDs ShowOutputFormat = "crds"
48 )
49
50 var readmeFileNames = []string{"readme.md", "readme.txt", "readme"}
51
52 func (o ShowOutputFormat) String() string {
53 return string(o)
54 }
55
56
57
58
59 type Show struct {
60 ChartPathOptions
61 Devel bool
62 OutputFormat ShowOutputFormat
63 JSONPathTemplate string
64 chart *chart.Chart
65 }
66
67
68
69
70 func NewShow(output ShowOutputFormat) *Show {
71 return &Show{
72 OutputFormat: output,
73 }
74 }
75
76
77 func NewShowWithConfig(output ShowOutputFormat, cfg *Configuration) *Show {
78 sh := &Show{
79 OutputFormat: output,
80 }
81 sh.ChartPathOptions.registryClient = cfg.RegistryClient
82
83 return sh
84 }
85
86
87 func (s *Show) SetRegistryClient(client *registry.Client) {
88 s.ChartPathOptions.registryClient = client
89 }
90
91
92 func (s *Show) Run(chartpath string) (string, error) {
93 if s.chart == nil {
94 chrt, err := loader.Load(chartpath)
95 if err != nil {
96 return "", err
97 }
98 s.chart = chrt
99 }
100 cf, err := yaml.Marshal(s.chart.Metadata)
101 if err != nil {
102 return "", err
103 }
104
105 var out strings.Builder
106 if s.OutputFormat == ShowChart || s.OutputFormat == ShowAll {
107 fmt.Fprintf(&out, "%s\n", cf)
108 }
109
110 if (s.OutputFormat == ShowValues || s.OutputFormat == ShowAll) && s.chart.Values != nil {
111 if s.OutputFormat == ShowAll {
112 fmt.Fprintln(&out, "---")
113 }
114 if s.JSONPathTemplate != "" {
115 printer, err := printers.NewJSONPathPrinter(s.JSONPathTemplate)
116 if err != nil {
117 return "", errors.Wrapf(err, "error parsing jsonpath %s", s.JSONPathTemplate)
118 }
119 printer.Execute(&out, s.chart.Values)
120 } else {
121 for _, f := range s.chart.Raw {
122 if f.Name == chartutil.ValuesfileName {
123 fmt.Fprintln(&out, string(f.Data))
124 }
125 }
126 }
127 }
128
129 if s.OutputFormat == ShowReadme || s.OutputFormat == ShowAll {
130 readme := findReadme(s.chart.Files)
131 if readme != nil {
132 if s.OutputFormat == ShowAll {
133 fmt.Fprintln(&out, "---")
134 }
135 fmt.Fprintf(&out, "%s\n", readme.Data)
136 }
137 }
138
139 if s.OutputFormat == ShowCRDs || s.OutputFormat == ShowAll {
140 crds := s.chart.CRDObjects()
141 if len(crds) > 0 {
142 if s.OutputFormat == ShowAll && !bytes.HasPrefix(crds[0].File.Data, []byte("---")) {
143 fmt.Fprintln(&out, "---")
144 }
145 for _, crd := range crds {
146 fmt.Fprintf(&out, "%s\n", string(crd.File.Data))
147 }
148 }
149 }
150 return out.String(), nil
151 }
152
153 func findReadme(files []*chart.File) (file *chart.File) {
154 for _, file := range files {
155 for _, n := range readmeFileNames {
156 if file == nil {
157 continue
158 }
159 if strings.EqualFold(file.Name, n) {
160 return file
161 }
162 }
163 }
164 return nil
165 }
166
View as plain text