mirror of
https://github.com/chylex/SMTP-Relay.git
synced 2024-11-21 12:42:45 +01:00
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
// IniParser is a parser for config files in classic key/value style format. Each
|
|
// line is tokenized as a single key/value pair. The first "=" delimited
|
|
// token in the line is interpreted as the flag name, and all remaining tokens
|
|
// are interpreted as the value. Any leading hyphens on the flag name are
|
|
// ignored.
|
|
func IniParser(r io.Reader, set func(name, value string) error) error {
|
|
s := bufio.NewScanner(r)
|
|
for s.Scan() {
|
|
line := strings.TrimSpace(s.Text())
|
|
if line == "" {
|
|
continue // skip empties
|
|
}
|
|
|
|
if line[0] == '#' || line[0] == ';' {
|
|
continue // skip comments
|
|
}
|
|
|
|
var (
|
|
name string
|
|
value string
|
|
index = strings.IndexRune(line, '=')
|
|
)
|
|
if index < 0 {
|
|
name, value = line, "true" // boolean option
|
|
} else {
|
|
name, value = strings.TrimSpace(line[:index]), strings.Trim(strings.TrimSpace(line[index+1:]), "\"")
|
|
}
|
|
|
|
if i := strings.Index(value, " #"); i >= 0 {
|
|
value = strings.TrimSpace(value[:i])
|
|
}
|
|
|
|
if err := set(name, value); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|