From 0709ac143f5c73a73767cce63ed6aec558139ecf Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 25 Oct 2020 18:10:50 +0100 Subject: [PATCH] Switch to go-scfg We don't use child directives yet, but likely will in the future. --- config/config.go | 57 +++++++++--------------------------------------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 13 insertions(+), 47 deletions(-) diff --git a/config/config.go b/config/config.go index 690a035..e8574aa 100644 --- a/config/config.go +++ b/config/config.go @@ -1,13 +1,11 @@ package config import ( - "bufio" "fmt" - "io" "net" "os" - "github.com/google/shlex" + "git.sr.ht/~emersion/go-scfg" ) type IPSet []*net.IPNet @@ -62,59 +60,39 @@ func Defaults() *Server { } func Load(path string) (*Server, error) { - f, err := os.Open(path) + cfg, err := scfg.Load(path) if err != nil { return nil, err } - defer f.Close() - - return Parse(f) + return parse(cfg) } -func Parse(r io.Reader) (*Server, error) { - scanner := bufio.NewScanner(r) - - var directives []directive - for scanner.Scan() { - words, err := shlex.Split(scanner.Text()) - if err != nil { - return nil, fmt.Errorf("failed to parse config file: %v", err) - } else if len(words) == 0 { - continue - } - - name, params := words[0], words[1:] - directives = append(directives, directive{name, params}) - } - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("failed to read config file: %v", err) - } - +func parse(cfg scfg.Block) (*Server, error) { srv := Defaults() - for _, d := range directives { + for _, d := range cfg { switch d.Name { case "listen": var uri string - if err := d.parseParams(&uri); err != nil { + if err := d.ParseParams(&uri); err != nil { return nil, err } srv.Listen = append(srv.Listen, uri) case "hostname": - if err := d.parseParams(&srv.Hostname); err != nil { + if err := d.ParseParams(&srv.Hostname); err != nil { return nil, err } case "tls": tls := &TLS{} - if err := d.parseParams(&tls.CertPath, &tls.KeyPath); err != nil { + if err := d.ParseParams(&tls.CertPath, &tls.KeyPath); err != nil { return nil, err } srv.TLS = tls case "sql": - if err := d.parseParams(&srv.SQLDriver, &srv.SQLSource); err != nil { + if err := d.ParseParams(&srv.SQLDriver, &srv.SQLSource); err != nil { return nil, err } case "log": - if err := d.parseParams(&srv.LogPath); err != nil { + if err := d.ParseParams(&srv.LogPath); err != nil { return nil, err } case "http-origin": @@ -135,18 +113,3 @@ func Parse(r io.Reader) (*Server, error) { return srv, nil } - -type directive struct { - Name string - Params []string -} - -func (d *directive) parseParams(out ...*string) error { - if len(d.Params) != len(out) { - return fmt.Errorf("directive %q has wrong number of parameters: expected %v, got %v", d.Name, len(out), len(d.Params)) - } - for i := range out { - *out[i] = d.Params[i] - } - return nil -} diff --git a/go.mod b/go.mod index 6d25354..a222e32 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.sr.ht/~emersion/soju go 1.13 require ( + git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/klauspost/compress v1.10.11 // indirect diff --git a/go.sum b/go.sum index 8e1787b..e9007a0 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc h1:51BD67xFX+bozd3ZRuOUfalrhx4/nQSh6A9lI08rYOk= +git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc/go.mod h1:t+Ww6SR24yYnXzEWiNlOY0AFo5E9B73X++10lrSpp4U= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=