From 47a5e8b83c035858d2a02d12c3847eeb86314473 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 31 Oct 2021 13:04:44 +0200 Subject: [PATCH] Use example config for default values --- config/bridge.go | 26 +++----------------------- config/config.go | 16 +++++++--------- main.go | 6 ++++++ 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index a74388a..fec408b 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -17,6 +17,7 @@ package config import ( + "fmt" "strconv" "strings" "text/template" @@ -97,29 +98,6 @@ type BridgeConfig struct { displaynameTemplate *template.Template `yaml:"-"` } -func (bc *BridgeConfig) setDefaults() { - bc.PortalMessageBuffer = 128 - - bc.CallNotices.Start = true - bc.CallNotices.End = true - - bc.HistorySync.CreatePortals = true - bc.HistorySync.MaxAge = 604800 - bc.UserAvatarSync = true - bc.BridgeMatrixLeave = true - - bc.SyncWithCustomPuppets = true - bc.DefaultBridgePresence = true - bc.DefaultBridgeReceipts = true - - bc.BridgeNotices = true - bc.EnableStatusBroadcast = true - - bc.ManagementRoomText.Welcome = "Hello, I'm a WhatsApp bridge bot." - bc.ManagementRoomText.WelcomeConnected = "Use `help` for help." - bc.ManagementRoomText.WelcomeUnconnected = "Use `help` for help or `login` to log in." -} - type umBridgeConfig BridgeConfig func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { @@ -131,6 +109,8 @@ func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate) if err != nil { return err + } else if !strings.Contains(bc.FormatUsername("1234567890"), "1234567890") { + return fmt.Errorf("username template is missing user ID placeholder") } bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate) diff --git a/config/config.go b/config/config.go index 4ad808d..661d6e4 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ package config import ( + "fmt" "io/ioutil" "gopkg.in/yaml.v2" @@ -25,6 +26,8 @@ import ( "maunium.net/go/mautrix/appservice" ) +var ExampleConfig string + type Config struct { Homeserver struct { Address string `yaml:"address"` @@ -88,14 +91,6 @@ func (config *Config) CanDoublePuppet(userID id.UserID) bool { return true } -func (config *Config) setDefaults() { - config.AppService.Database.MaxOpenConns = 20 - config.AppService.Database.MaxIdleConns = 2 - config.WhatsApp.OSName = "Mautrix-WhatsApp bridge" - config.WhatsApp.BrowserName = "mx-wa" - config.Bridge.setDefaults() -} - func Load(path string) (*Config, error) { data, err := ioutil.ReadFile(path) if err != nil { @@ -103,7 +98,10 @@ func Load(path string) (*Config, error) { } var config = &Config{} - config.setDefaults() + err = yaml.UnmarshalStrict([]byte(ExampleConfig), config) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal example config: %w", err) + } err = yaml.Unmarshal(data, config) return config, err } diff --git a/main.go b/main.go index 200a7c1..1d49226 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ import ( "sync" "syscall" "time" + _ "embed" "google.golang.org/protobuf/proto" @@ -70,6 +71,9 @@ var ( VersionString = "" ) +//go:embed example-config.yaml +var ExampleConfig string + func init() { if len(Tag) > 0 && Tag[0] == 'v' { Tag = Tag[1:] @@ -88,6 +92,8 @@ func init() { mautrix.DefaultUserAgent = fmt.Sprintf("mautrix-whatsapp/%s %s", Version, mautrix.DefaultUserAgent) WAVersion = strings.FieldsFunc(Version, func(r rune) bool { return r == '-' || r == '+' })[0] VersionString = fmt.Sprintf("%s %s (%s)", Name, Version, BuildTime) + + config.ExampleConfig = ExampleConfig } var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String()