| 1 |
package flags |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"reflect" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
type IniValue struct { |
| 13 |
Name string |
| 14 |
Value string |
| 15 |
} |
| 16 |
|
| 17 |
type IniSection []IniValue |
| 18 |
type Ini map[string]IniSection |
| 19 |
|
| 20 |
func readFullLine(reader *bufio.Reader) (string, error) { |
| 21 |
var line []byte |
| 22 |
|
| 23 |
for { |
| 24 |
l, more, err := reader.ReadLine() |
| 25 |
|
| 26 |
if err != nil { |
| 27 |
return "", err |
| 28 |
} |
| 29 |
|
| 30 |
if line == nil && !more { |
| 31 |
return string(l), nil |
| 32 |
} |
| 33 |
|
| 34 |
line = append(line, l...) |
| 35 |
|
| 36 |
if !more { |
| 37 |
break |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return string(line), nil |
| 42 |
} |
| 43 |
|
| 44 |
type IniError struct { |
| 45 |
Message string |
| 46 |
File string |
| 47 |
LineNumber uint |
| 48 |
} |
| 49 |
|
| 50 |
func (x *IniError) Error() string { |
| 51 |
return fmt.Sprintf("%s:%d: %s", |
| 52 |
x.File, |
| 53 |
x.LineNumber, |
| 54 |
x.Message) |
| 55 |
} |
| 56 |
|
| 57 |
type IniOptions uint |
| 58 |
|
| 59 |
const ( |
| 60 |
IniNone IniOptions = 0 |
| 61 |
IniIncludeDefaults = 1 << iota |
| 62 |
IniIncludeComments |
| 63 |
IniDefault = IniIncludeComments |
| 64 |
) |
| 65 |
|
| 66 |
func writeIni(parser *Parser, writer io.Writer, options IniOptions) { |
| 67 |
parser.EachGroup(func(i int, group *Group) { |
| 68 |
if i != 0 { |
| 69 |
io.WriteString(writer, "\n") |
| 70 |
} |
| 71 |
|
| 72 |
fmt.Fprintf(writer, "[%s]\n", group.Name) |
| 73 |
|
| 74 |
for _, option := range group.Options { |
| 75 |
if option.isFunc() { |
| 76 |
continue |
| 77 |
} |
| 78 |
|
| 79 |
if len(option.tag.Get("no-ini")) != 0 { |
| 80 |
continue |
| 81 |
} |
| 82 |
|
| 83 |
val := option.Value |
| 84 |
|
| 85 |
if (options&IniIncludeDefaults) == IniNone && |
| 86 |
reflect.DeepEqual(val, option.defaultValue) { |
| 87 |
continue |
| 88 |
} |
| 89 |
|
| 90 |
if (options & IniIncludeComments) != IniNone { |
| 91 |
fmt.Fprintf(writer, "; %s\n", option.Description) |
| 92 |
} |
| 93 |
|
| 94 |
switch val.Type().Kind() { |
| 95 |
case reflect.Slice: |
| 96 |
for idx := 0; idx < val.Len(); idx++ { |
| 97 |
v, _ := convertToString(val.Index(idx), option.tag) |
| 98 |
fmt.Fprintf(writer, "%s = %s\n", option.iniName(), v) |
| 99 |
} |
| 100 |
case reflect.Map: |
| 101 |
for _, key := range val.MapKeys() { |
| 102 |
k, _ := convertToString(key, option.tag) |
| 103 |
v, _ := convertToString(val.MapIndex(key), option.tag) |
| 104 |
|
| 105 |
fmt.Fprintf(writer, "%s = %s:%s\n", option.iniName(), k, v) |
| 106 |
} |
| 107 |
default: |
| 108 |
v, _ := convertToString(val, option.tag) |
| 109 |
fmt.Fprintf(writer, "%s = %s\n", option.iniName(), v) |
| 110 |
} |
| 111 |
} |
| 112 |
}) |
| 113 |
} |
| 114 |
|
| 115 |
func readIniFromFile(filename string) (Ini, error) { |
| 116 |
file, err := os.Open(filename) |
| 117 |
|
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
|
| 122 |
defer file.Close() |
| 123 |
|
| 124 |
return readIni(file, filename) |
| 125 |
} |
| 126 |
|
| 127 |
func readIni(contents io.Reader, filename string) (Ini, error) { |
| 128 |
ret := make(Ini) |
| 129 |
|
| 130 |
reader := bufio.NewReader(contents) |
| 131 |
|
| 132 |
var section IniSection |
| 133 |
var sectionname string |
| 134 |
|
| 135 |
var lineno uint |
| 136 |
|
| 137 |
for { |
| 138 |
line, err := readFullLine(reader) |
| 139 |
|
| 140 |
if err == io.EOF { |
| 141 |
break |
| 142 |
} |
| 143 |
|
| 144 |
if err != nil { |
| 145 |
return nil, err |
| 146 |
} |
| 147 |
|
| 148 |
lineno++ |
| 149 |
line = strings.TrimSpace(line) |
| 150 |
|
| 151 |
|
| 152 |
if len(line) == 0 || line[0] == ';' { |
| 153 |
continue |
| 154 |
} |
| 155 |
|
| 156 |
if section == nil { |
| 157 |
if line[0] != '[' || line[len(line)-1] != ']' { |
| 158 |
return nil, &IniError{ |
| 159 |
Message: "malformed section header", |
| 160 |
File: filename, |
| 161 |
LineNumber: lineno, |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
name := strings.TrimSpace(line[1 : len(line)-1]) |
| 166 |
|
| 167 |
if len(name) == 0 { |
| 168 |
return nil, &IniError{ |
| 169 |
Message: "empty section name", |
| 170 |
File: filename, |
| 171 |
LineNumber: lineno, |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
sectionname = name |
| 176 |
section = ret[name] |
| 177 |
|
| 178 |
if section == nil { |
| 179 |
section = make(IniSection, 0, 10) |
| 180 |
ret[name] = section |
| 181 |
} |
| 182 |
|
| 183 |
continue |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
keyval := strings.SplitN(line, "=", 2) |
| 188 |
|
| 189 |
if len(keyval) != 2 { |
| 190 |
return nil, &IniError{ |
| 191 |
Message: "malformed key=value", |
| 192 |
File: filename, |
| 193 |
LineNumber: lineno, |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
name := strings.TrimSpace(keyval[0]) |
| 198 |
value := strings.TrimSpace(keyval[1]) |
| 199 |
|
| 200 |
section = append(section, IniValue{ |
| 201 |
Name: name, |
| 202 |
Value: value, |
| 203 |
}) |
| 204 |
|
| 205 |
ret[sectionname] = section |
| 206 |
} |
| 207 |
|
| 208 |
return ret, nil |
| 209 |
} |