|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "text/template" |
| 11 | + |
| 12 | + "gopkg.in/yaml.v2" |
| 13 | +) |
| 14 | + |
| 15 | +// Reads templated documents and does templating based on the inValues, dumps to stdout |
| 16 | +func executeTemplate(inValues io.Reader, templates ...string) error { |
| 17 | + tpl, err := template.ParseFiles(templates...) |
| 18 | + if err != nil { |
| 19 | + return fmt.Errorf("error parsing template(s): %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + buf := bytes.NewBuffer(nil) |
| 23 | + _, err = io.Copy(buf, inValues) |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("failed to read values: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + var values map[string]interface{} |
| 29 | + err = yaml.Unmarshal(buf.Bytes(), &values) |
| 30 | + if err != nil { |
| 31 | + return fmt.Errorf("failed to parse values: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Add the .Values to the values that are read, to make it more helm-like |
| 35 | + topvalues := map[string]interface{}{ |
| 36 | + "Values": values, |
| 37 | + } |
| 38 | + err = tpl.Execute(os.Stdout, topvalues) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("failed to execute template: %v", err) |
| 41 | + } |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func main() { |
| 46 | + valuesFile := flag.String("values", "", "Path to values YAML file (required)") |
| 47 | + flag.Parse() |
| 48 | + |
| 49 | + if *valuesFile == "" { |
| 50 | + log.Println("Error: --values flag is required") |
| 51 | + flag.Usage() |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + |
| 55 | + valuesReader, err := os.Open(*valuesFile) |
| 56 | + if err != nil { |
| 57 | + log.Printf("Failed to open values file: %v\n", err) |
| 58 | + os.Exit(1) |
| 59 | + } |
| 60 | + defer valuesReader.Close() |
| 61 | + |
| 62 | + err = executeTemplate(valuesReader, flag.Args()...) |
| 63 | + if err != nil { |
| 64 | + log.Println(err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | +} |
0 commit comments