github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/cmd/helm/printer.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 "encoding/json" 21 "fmt" 22 "io" 23 "text/template" 24 "time" 25 26 "github.com/ghodss/yaml" 27 "github.com/gosuri/uitable" 28 "github.com/spf13/cobra" 29 30 "k8s.io/helm/pkg/chartutil" 31 "k8s.io/helm/pkg/proto/hapi/release" 32 "k8s.io/helm/pkg/timeconv" 33 ) 34 35 type outputFormat string 36 37 const ( 38 outputFlag = "output" 39 40 outputTable outputFormat = "table" 41 outputJSON outputFormat = "json" 42 outputYAML outputFormat = "yaml" 43 ) 44 45 var printReleaseTemplate = `REVISION: {{.Release.Version}} 46 RELEASED: {{.ReleaseDate}} 47 CHART: {{.Release.Chart.Metadata.Name}}-{{.Release.Chart.Metadata.Version}} 48 USER-SUPPLIED VALUES: 49 {{.Release.Config.Raw}} 50 COMPUTED VALUES: 51 {{.ComputedValues}} 52 HOOKS: 53 {{- range .Release.Hooks }} 54 --- 55 # {{.Name}} 56 {{.Manifest}} 57 {{- end }} 58 MANIFEST: 59 {{.Release.Manifest}} 60 ` 61 62 func printRelease(out io.Writer, rel *release.Release) error { 63 if rel == nil { 64 return nil 65 } 66 67 cfg, err := chartutil.CoalesceValues(rel.Chart, rel.Config) 68 if err != nil { 69 return err 70 } 71 cfgStr, err := cfg.YAML() 72 if err != nil { 73 return err 74 } 75 76 data := map[string]interface{}{ 77 "Release": rel, 78 "ComputedValues": cfgStr, 79 "ReleaseDate": timeconv.Format(rel.Info.LastDeployed, time.ANSIC), 80 } 81 return tpl(printReleaseTemplate, data, out) 82 } 83 84 func tpl(t string, vals interface{}, out io.Writer) error { 85 tt, err := template.New("_").Parse(t) 86 if err != nil { 87 return err 88 } 89 return tt.Execute(out, vals) 90 } 91 92 func debug(format string, args ...interface{}) { 93 if settings.Debug { 94 format = fmt.Sprintf("[debug] %s\n", format) 95 fmt.Printf(format, args...) 96 } 97 } 98 99 // bindOutputFlag will add the output flag to the given command and bind the 100 // value to the given string pointer 101 func bindOutputFlag(cmd *cobra.Command, varRef *string) { 102 cmd.Flags().StringVarP(varRef, outputFlag, "o", string(outputTable), fmt.Sprintf("Prints the output in the specified format. Allowed values: %s, %s, %s", outputTable, outputJSON, outputYAML)) 103 } 104 105 type outputWriter interface { 106 WriteTable(out io.Writer) error 107 WriteJSON(out io.Writer) error 108 WriteYAML(out io.Writer) error 109 } 110 111 func write(out io.Writer, ow outputWriter, format outputFormat) error { 112 switch format { 113 case outputTable: 114 return ow.WriteTable(out) 115 case outputJSON: 116 return ow.WriteJSON(out) 117 case outputYAML: 118 return ow.WriteYAML(out) 119 } 120 return fmt.Errorf("unsupported format %s", format) 121 } 122 123 // encodeJSON is a helper function to decorate any error message with a bit more 124 // context and avoid writing the same code over and over for printers 125 func encodeJSON(out io.Writer, obj interface{}) error { 126 enc := json.NewEncoder(out) 127 err := enc.Encode(obj) 128 if err != nil { 129 return fmt.Errorf("unable to write JSON output: %s", err) 130 } 131 return nil 132 } 133 134 // encodeYAML is a helper function to decorate any error message with a bit more 135 // context and avoid writing the same code over and over for printers 136 func encodeYAML(out io.Writer, obj interface{}) error { 137 raw, err := yaml.Marshal(obj) 138 if err != nil { 139 return fmt.Errorf("unable to write YAML output: %s", err) 140 } 141 // Append a newline, as with a JSON encoder 142 raw = append(raw, []byte("\n")...) 143 _, err = out.Write(raw) 144 if err != nil { 145 return fmt.Errorf("unable to write YAML output: %s", err) 146 } 147 return nil 148 } 149 150 // encodeTable is a helper function to decorate any error message with a bit 151 // more context and avoid writing the same code over and over for printers 152 func encodeTable(out io.Writer, table *uitable.Table) error { 153 raw := table.Bytes() 154 raw = append(raw, []byte("\n")...) 155 _, err := out.Write(raw) 156 if err != nil { 157 return fmt.Errorf("unable to write table output: %s", err) 158 } 159 return nil 160 }