Use example config for default values

This commit is contained in:
Tulir Asokan 2021-10-31 13:04:44 +02:00
parent 1d877771e4
commit 47a5e8b83c
3 changed files with 16 additions and 32 deletions

View file

@ -17,6 +17,7 @@
package config package config
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
@ -97,29 +98,6 @@ type BridgeConfig struct {
displaynameTemplate *template.Template `yaml:"-"` 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 type umBridgeConfig BridgeConfig
func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { 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) bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate)
if err != nil { if err != nil {
return err 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) bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate)

View file

@ -17,6 +17,7 @@
package config package config
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -25,6 +26,8 @@ import (
"maunium.net/go/mautrix/appservice" "maunium.net/go/mautrix/appservice"
) )
var ExampleConfig string
type Config struct { type Config struct {
Homeserver struct { Homeserver struct {
Address string `yaml:"address"` Address string `yaml:"address"`
@ -88,14 +91,6 @@ func (config *Config) CanDoublePuppet(userID id.UserID) bool {
return true 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) { func Load(path string) (*Config, error) {
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
@ -103,7 +98,10 @@ func Load(path string) (*Config, error) {
} }
var config = &Config{} 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) err = yaml.Unmarshal(data, config)
return config, err return config, err
} }

View file

@ -26,6 +26,7 @@ import (
"sync" "sync"
"syscall" "syscall"
"time" "time"
_ "embed"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -70,6 +71,9 @@ var (
VersionString = "" VersionString = ""
) )
//go:embed example-config.yaml
var ExampleConfig string
func init() { func init() {
if len(Tag) > 0 && Tag[0] == 'v' { if len(Tag) > 0 && Tag[0] == 'v' {
Tag = Tag[1:] Tag = Tag[1:]
@ -88,6 +92,8 @@ func init() {
mautrix.DefaultUserAgent = fmt.Sprintf("mautrix-whatsapp/%s %s", Version, mautrix.DefaultUserAgent) mautrix.DefaultUserAgent = fmt.Sprintf("mautrix-whatsapp/%s %s", Version, mautrix.DefaultUserAgent)
WAVersion = strings.FieldsFunc(Version, func(r rune) bool { return r == '-' || r == '+' })[0] WAVersion = strings.FieldsFunc(Version, func(r rune) bool { return r == '-' || r == '+' })[0]
VersionString = fmt.Sprintf("%s %s (%s)", Name, Version, BuildTime) 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() var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String()