From d27880e03e26250168ca9b96681bfb33b89619b1 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 11 Sep 2022 13:50:34 +0200 Subject: [PATCH] config: use structs to group DB/MsgAuth --- cmd/soju/main.go | 4 ++-- cmd/sojuctl/main.go | 2 +- config/config.go | 39 +++++++++++++++++++++++--------------- contrib/znc-import/main.go | 2 +- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/cmd/soju/main.go b/cmd/soju/main.go index e8b64ae..468b1f5 100644 --- a/cmd/soju/main.go +++ b/cmd/soju/main.go @@ -86,7 +86,7 @@ func loadConfig() (*config.Server, *soju.Config, error) { cfg := &soju.Config{ Hostname: raw.Hostname, Title: raw.Title, - LogPath: raw.MsgStoreSource, + LogPath: raw.MsgStore.Source, HTTPOrigins: raw.HTTPOrigins, AcceptProxyIPs: raw.AcceptProxyIPs, MaxUserNetworks: raw.MaxUserNetworks, @@ -118,7 +118,7 @@ func main() { log.Printf("failed to bump max number of opened files: %v", err) } - db, err := database.Open(cfg.SQLDriver, cfg.SQLSource) + db, err := database.Open(cfg.DB.Driver, cfg.DB.Source) if err != nil { log.Fatalf("failed to open database: %v", err) } diff --git a/cmd/sojuctl/main.go b/cmd/sojuctl/main.go index 24ed1ec..bde1d86 100644 --- a/cmd/sojuctl/main.go +++ b/cmd/sojuctl/main.go @@ -45,7 +45,7 @@ func main() { cfg = config.Defaults() } - db, err := database.Open(cfg.SQLDriver, cfg.SQLSource) + db, err := database.Open(cfg.DB.Driver, cfg.DB.Source) if err != nil { log.Fatalf("failed to open database: %v", err) } diff --git a/config/config.go b/config/config.go index 27260e7..0f447b5 100644 --- a/config/config.go +++ b/config/config.go @@ -36,6 +36,14 @@ type TLS struct { CertPath, KeyPath string } +type DB struct { + Driver, Source string +} + +type MsgStore struct { + Driver, Source string +} + type Server struct { Listen []string TLS *TLS @@ -43,11 +51,8 @@ type Server struct { Title string MOTDPath string - SQLDriver string - SQLSource string - - MsgStoreDriver string - MsgStoreSource string + DB DB + MsgStore MsgStore HTTPOrigins []string AcceptProxyIPs IPSet @@ -63,10 +68,14 @@ func Defaults() *Server { hostname = "localhost" } return &Server{ - Hostname: hostname, - SQLDriver: "sqlite3", - SQLSource: "soju.db", - MsgStoreDriver: "memory", + Hostname: hostname, + DB: DB{ + Driver: "sqlite3", + Source: "soju.db", + }, + MsgStore: MsgStore{ + Driver: "memory", + }, MaxUserNetworks: -1, MultiUpstream: true, } @@ -109,22 +118,22 @@ func parse(cfg scfg.Block) (*Server, error) { } srv.TLS = tls case "db": - if err := d.ParseParams(&srv.SQLDriver, &srv.SQLSource); err != nil { + if err := d.ParseParams(&srv.DB.Driver, &srv.DB.Source); err != nil { return nil, err } case "message-store", "log": - if err := d.ParseParams(&srv.MsgStoreDriver); err != nil { + if err := d.ParseParams(&srv.MsgStore.Driver); err != nil { return nil, err } - switch srv.MsgStoreDriver { + switch srv.MsgStore.Driver { case "memory": - srv.MsgStoreSource = "" + srv.MsgStore.Source = "" case "fs": - if err := d.ParseParams(nil, &srv.MsgStoreSource); err != nil { + if err := d.ParseParams(nil, &srv.MsgStore.Source); err != nil { return nil, err } default: - return nil, fmt.Errorf("directive %q: unknown driver %q", d.Name, srv.MsgStoreDriver) + return nil, fmt.Errorf("directive %q: unknown driver %q", d.Name, srv.MsgStore.Driver) } case "http-origin": srv.HTTPOrigins = d.Params diff --git a/contrib/znc-import/main.go b/contrib/znc-import/main.go index afb6a9e..51284d6 100644 --- a/contrib/znc-import/main.go +++ b/contrib/znc-import/main.go @@ -64,7 +64,7 @@ func main() { ctx := context.Background() - db, err := database.Open(cfg.SQLDriver, cfg.SQLSource) + db, err := database.Open(cfg.DB.Driver, cfg.DB.Source) if err != nil { log.Fatalf("failed to open database: %v", err) }