WIP on v1
This commit is contained in:
103
parser.go
103
parser.go
@@ -2,66 +2,107 @@ package conf
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
configKeyValueRe = regexp.MustCompile(`(\w+) *= *(.+)\b[\t| |\n]*`)
|
||||
configOptionRe = regexp.MustCompile(`(\w+)`)
|
||||
configCommentedOut = "#"
|
||||
const (
|
||||
strComment = "#"
|
||||
strEqSign = "="
|
||||
)
|
||||
|
||||
// ParseFile reads values from file. It returns nil and error if os.Open(filename) fails.
|
||||
var (
|
||||
ErrFormat = errors.New("conf: line does not match \"key = value\" pattern")
|
||||
ErrDuplicateKey = errors.New("conf: duplicate key found")
|
||||
)
|
||||
|
||||
// Open reads values from file. It returns nil and error if os.Open(filename) fails.
|
||||
// It would be wise to always check returned error.
|
||||
// ParseFile captures two types of values: "key = value" and "option". Either key value pair or
|
||||
// option must be put in its own line in the file.
|
||||
// Key or option must be a single word. In example line:
|
||||
// "option1 option2"
|
||||
//
|
||||
// "option1 option2"
|
||||
//
|
||||
// only option1 will be captured. option2 needs to be in a separate line in the file to take effect.
|
||||
// In line "key = value1,value2,value3" all of value1, value2, and value3 will be
|
||||
// captured. They can be later accesed separately with Setting's Split() method.
|
||||
func ParseFile(filename string) (*Config, error) {
|
||||
func Open(filename string) (*Conf, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
return parseReader(file), nil
|
||||
|
||||
c, err := parseReader(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, file.Close()
|
||||
}
|
||||
|
||||
// ParseReader reads from r and returns Config. See also ParseFile.
|
||||
func ParseReader(r io.Reader) *Config {
|
||||
// Read reads from r and returns Config.
|
||||
func Read(r io.Reader) (*Conf, error) {
|
||||
return parseReader(r)
|
||||
}
|
||||
|
||||
// ParseReadCloser reads from r, returns Config and calls r.Close().
|
||||
// See also ParseFile.
|
||||
func ParseReadCloser(r io.ReadCloser) *Config {
|
||||
defer r.Close()
|
||||
return parseReader(r)
|
||||
}
|
||||
func parseReader(r io.Reader) (*Conf, error) {
|
||||
settings := make(map[string]string)
|
||||
|
||||
func parseReader(r io.Reader) *Config {
|
||||
var c Config
|
||||
c.Settings = make(map[string]string)
|
||||
c.Options = make(map[string]struct{})
|
||||
scanner := bufio.NewScanner(r)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, configCommentedOut) {
|
||||
|
||||
line = trim(stripComment(line))
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case configKeyValueRe.MatchString(line):
|
||||
kvpair := configKeyValueRe.FindStringSubmatch(line)
|
||||
c.Settings[kvpair[1]] = kvpair[2]
|
||||
case configOptionRe.MatchString(line):
|
||||
opt := configOptionRe.FindString(line)
|
||||
c.Options[opt] = struct{}{}
|
||||
|
||||
key, value, ok := toKV(line)
|
||||
if !ok {
|
||||
return nil, ErrFormat
|
||||
}
|
||||
|
||||
if _, ok := settings[key]; ok {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDuplicateKey, key)
|
||||
}
|
||||
|
||||
settings[key] = value
|
||||
}
|
||||
return &c
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Conf{
|
||||
settings: settings,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func toKV(s string) (string, string, bool) {
|
||||
k, v, ok := strings.Cut(s, strEqSign)
|
||||
if !ok || k == "" || v == "" {
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
return trim(k), trim(v), true
|
||||
}
|
||||
|
||||
func trim(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func stripComment(s string) string {
|
||||
idx := strings.Index(s, strComment)
|
||||
if idx > -1 {
|
||||
s = s[:idx]
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
Reference in New Issue
Block a user