|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/BurntSushi/toml" |
| 9 | +) |
| 10 | + |
| 11 | +func printConfFile(config *Config) { |
| 12 | + // Loop the 'Order' list. |
| 13 | + for _, section := range config.Order { |
| 14 | + // If Order contains a missing section, panic. |
| 15 | + if config.Sections[section] == nil { |
| 16 | + panic(section + ": in order, but missing from sections. This is a bug in conf-builder.yml.") |
| 17 | + } |
| 18 | + |
| 19 | + if config.Defs[section] != nil { |
| 20 | + fmt.Print(config.Sections[section].makeDefinedSection(config.Defs[section], config.DefOrder[section], false)) |
| 21 | + } else { |
| 22 | + fmt.Print(config.Sections[section].makeSection(section, false, false)) |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Not all sections have defs, and it may be nil. Defs only work on 'list' sections. |
| 28 | +func (h *Header) makeSection(name section, showHeader, showValue bool) string { |
| 29 | + var buf bytes.Buffer |
| 30 | + |
| 31 | + // Print section header text. |
| 32 | + if h.Text != "" { |
| 33 | + buf.WriteString(h.Text) |
| 34 | + } |
| 35 | + |
| 36 | + comment := "#" |
| 37 | + if showHeader { |
| 38 | + // this only happens when a defined section has a comment override on the repeating headers. |
| 39 | + comment = "" |
| 40 | + } |
| 41 | + |
| 42 | + if !h.NoHeader { // Print the [section] or [[section]] header. |
| 43 | + if h.Kind == list { // list sections are commented by default. |
| 44 | + buf.WriteString(comment + "[[" + string(name) + "]]" + "\n") // list sections use double-brackets. |
| 45 | + } else { |
| 46 | + buf.WriteString("[" + string(name) + "]" + "\n") // non-list sections use single brackets. |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + for _, param := range h.Params { |
| 51 | + // Print an empty newline for each param if the section has no header and the param has a description. |
| 52 | + if h.NoHeader && param.Desc != "" { |
| 53 | + buf.WriteString("\n") |
| 54 | + } |
| 55 | + |
| 56 | + // Add ## to the beginning of each line in the description. |
| 57 | + // Uses the newline \n character to figure out where each line begins. |
| 58 | + if param.Desc != "" { |
| 59 | + buf.WriteString("## " + strings.ReplaceAll(strings.TrimSpace(param.Desc), "\n", "\n## ") + "\n") |
| 60 | + } |
| 61 | + |
| 62 | + switch { |
| 63 | + default: |
| 64 | + fallthrough |
| 65 | + case showValue: |
| 66 | + buf.WriteString(fmt.Sprintf("%s = %s\n", param.Name, param.Value())) |
| 67 | + case param.Example != nil: |
| 68 | + // If example is not empty, use that commented out, otherwise use the default. |
| 69 | + fallthrough |
| 70 | + case h.Kind == list: |
| 71 | + // If the 'kind' is a 'list', we comment all the parameters. |
| 72 | + buf.WriteString(fmt.Sprintf("#%s = %s\n", param.Name, param.Value())) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Each section needs a newline at the end. |
| 77 | + buf.WriteString("\n") |
| 78 | + |
| 79 | + return buf.String() |
| 80 | +} |
| 81 | + |
| 82 | +func (p *Param) Value() string { |
| 83 | + // If example is not empty, use that commented out, otherwise use the default. |
| 84 | + out, _ := toml.Marshal(p.Default) |
| 85 | + if p.Example != nil { |
| 86 | + out, _ = toml.Marshal(p.Example) |
| 87 | + } |
| 88 | + |
| 89 | + // The toml marshaller uses only regular quotes " which kinda suck, so replace them with single quotes ' on file paths. |
| 90 | + if strings.Contains(p.Name, "path") || strings.HasSuffix(p.Name, "file") || p.Name == "command" { |
| 91 | + return string(bytes.ReplaceAll(out, []byte{'"'}, []byte("'"))) |
| 92 | + } |
| 93 | + |
| 94 | + return string(out) |
| 95 | +} |
| 96 | + |
| 97 | +// makeDefinedSection duplicates sections from overrides, and prints it once for each override. |
| 98 | +func (h *Header) makeDefinedSection(defs Defs, order []section, showValue bool) string { |
| 99 | + var buf bytes.Buffer |
| 100 | + |
| 101 | + for _, section := range order { |
| 102 | + newHeader := createDefinedSection(defs[section], h) |
| 103 | + // Make a brand new section and pass it back in. |
| 104 | + // Only defined sections can comment the header. |
| 105 | + buf.WriteString(newHeader.makeSection(section, !defs[section].Comment, showValue)) |
| 106 | + } |
| 107 | + |
| 108 | + return buf.String() |
| 109 | +} |
0 commit comments