Switch to go-scfg

We don't use child directives yet, but likely will in the future.
This commit is contained in:
Simon Ser 2020-10-25 18:10:50 +01:00
parent 05aafb5edf
commit 0709ac143f
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 13 additions and 47 deletions

View File

@ -1,13 +1,11 @@
package config package config
import ( import (
"bufio"
"fmt" "fmt"
"io"
"net" "net"
"os" "os"
"github.com/google/shlex" "git.sr.ht/~emersion/go-scfg"
) )
type IPSet []*net.IPNet type IPSet []*net.IPNet
@ -62,59 +60,39 @@ func Defaults() *Server {
} }
func Load(path string) (*Server, error) { func Load(path string) (*Server, error) {
f, err := os.Open(path) cfg, err := scfg.Load(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() return parse(cfg)
return Parse(f)
}
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() srv := Defaults()
for _, d := range directives { for _, d := range cfg {
switch d.Name { switch d.Name {
case "listen": case "listen":
var uri string var uri string
if err := d.parseParams(&uri); err != nil { if err := d.ParseParams(&uri); err != nil {
return nil, err return nil, err
} }
srv.Listen = append(srv.Listen, uri) srv.Listen = append(srv.Listen, uri)
case "hostname": case "hostname":
if err := d.parseParams(&srv.Hostname); err != nil { if err := d.ParseParams(&srv.Hostname); err != nil {
return nil, err return nil, err
} }
case "tls": case "tls":
tls := &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 return nil, err
} }
srv.TLS = tls srv.TLS = tls
case "sql": 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 return nil, err
} }
case "log": case "log":
if err := d.parseParams(&srv.LogPath); err != nil { if err := d.ParseParams(&srv.LogPath); err != nil {
return nil, err return nil, err
} }
case "http-origin": case "http-origin":
@ -135,18 +113,3 @@ func Parse(r io.Reader) (*Server, error) {
return srv, nil 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
}

1
go.mod
View File

@ -3,6 +3,7 @@ module git.sr.ht/~emersion/soju
go 1.13 go 1.13
require ( require (
git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/klauspost/compress v1.10.11 // indirect github.com/klauspost/compress v1.10.11 // indirect

2
go.sum
View File

@ -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/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=