Remove config update stuff

This commit is contained in:
Tulir Asokan 2018-08-29 23:48:15 +03:00
parent c7348f29b0
commit 5a1a6f9c3c
4 changed files with 8 additions and 227 deletions

View file

@ -1,115 +0,0 @@
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package config
import (
"strings"
)
type RecursiveMap map[interface{}]interface{}
func (rm RecursiveMap) GetDefault(path string, defVal interface{}) interface{} {
val, ok := rm.Get(path)
if !ok {
return defVal
}
return val
}
func (rm RecursiveMap) GetMap(path string) RecursiveMap {
val := rm.GetDefault(path, nil)
if val == nil {
return nil
}
newRM, ok := val.(map[interface{}]interface{})
if ok {
return RecursiveMap(newRM)
}
return nil
}
func (rm RecursiveMap) Get(path string) (interface{}, bool) {
if index := strings.IndexRune(path, '.'); index >= 0 {
key := path[:index]
path = path[index+1:]
submap := rm.GetMap(key)
if submap == nil {
return nil, false
}
return submap.Get(path)
}
val, ok := rm[path]
return val, ok
}
func (rm RecursiveMap) GetIntDefault(path string, defVal int) int {
val, ok := rm.GetInt(path)
if !ok {
return defVal
}
return val
}
func (rm RecursiveMap) GetInt(path string) (int, bool) {
val, ok := rm.Get(path)
if !ok {
return 0, ok
}
intVal, ok := val.(int)
return intVal, ok
}
func (rm RecursiveMap) GetStringDefault(path string, defVal string) string {
val, ok := rm.GetString(path)
if !ok {
return defVal
}
return val
}
func (rm RecursiveMap) GetString(path string) (string, bool) {
val, ok := rm.Get(path)
if !ok {
return "", ok
}
strVal, ok := val.(string)
return strVal, ok
}
func (rm RecursiveMap) Set(path string, value interface{}) {
if index := strings.IndexRune(path, '.'); index >= 0 {
key := path[:index]
path = path[index+1:]
nextRM := rm.GetMap(key)
if nextRM == nil {
nextRM = make(RecursiveMap)
rm[key] = nextRM
}
nextRM.Set(path, value)
return
}
rm[path] = value
}
func (rm RecursiveMap) CopyFrom(otherRM RecursiveMap, path string) {
val, ok := otherRM.Get(path)
if ok {
rm.Set(path, val)
}
}

View file

@ -1,100 +0,0 @@
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package config
import (
"io/ioutil"
"gopkg.in/yaml.v2"
)
func Update(path, basePath string) error {
oldCfgData, err := ioutil.ReadFile(path)
if err != nil {
return err
}
oldCfg := make(RecursiveMap)
err = yaml.Unmarshal(oldCfgData, &oldCfg)
if err != nil {
return err
}
baseCfgData, err := ioutil.ReadFile(basePath)
if err != nil {
return err
}
baseCfg := make(RecursiveMap)
err = yaml.Unmarshal(baseCfgData, &baseCfg)
if err != nil {
return err
}
err = runUpdate(oldCfg, baseCfg)
if err != nil {
return err
}
newCfgData, err := yaml.Marshal(&baseCfg)
if err != nil {
return err
}
return ioutil.WriteFile(path, newCfgData, 0600)
}
func runUpdate(oldCfg, newCfg RecursiveMap) error {
cp := func(path string) {
newCfg.CopyFrom(oldCfg, path)
}
cp("homeserver.address")
cp("homeserver.domain")
cp("appservice.address")
cp("appservice.hostname")
cp("appservice.port")
cp("appservice.database.type")
cp("appservice.database.uri")
cp("appservice.state_store_path")
cp("appservice.id")
cp("appservice.bot.username")
cp("appservice.bot.displayname")
cp("appservice.bot.avatar")
cp("appservice.bot.as_token")
cp("appservice.bot.hs_token")
cp("bridge.username_template")
cp("bridge.displayname_template")
cp("bridge.command_prefix")
cp("bridge.permissions")
cp("logging.directory")
cp("logging.file_name_format")
cp("logging.file_date_format")
cp("logging.file_mode")
cp("logging.timestamp_format")
cp("logging.print_level")
return nil
}

View file

@ -29,7 +29,7 @@ appservice:
# Appservice bot details. # Appservice bot details.
bot: bot:
# Username of the appservice bot. # Username of the appservice bot.
username: whatsapp username: whatsappbot
# Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty
# to leave display name/avatar as-is. # to leave display name/avatar as-is.
displayname: WhatsApp bridge bot displayname: WhatsApp bridge bot

18
main.go
View file

@ -91,16 +91,12 @@ func NewBridge() *Bridge {
portalsByJID: make(map[database.PortalKey]*Portal), portalsByJID: make(map[database.PortalKey]*Portal),
puppets: make(map[types.WhatsAppID]*Puppet), puppets: make(map[types.WhatsAppID]*Puppet),
} }
err := config.Update(*configPath, *baseConfigPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to update config:", err)
os.Exit(10)
}
var err error
bridge.Config, err = config.Load(*configPath) bridge.Config, err = config.Load(*configPath)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Failed to load config:", err) fmt.Fprintln(os.Stderr, "Failed to load config:", err)
os.Exit(11) os.Exit(10)
} }
return bridge return bridge
} }
@ -111,7 +107,7 @@ func (bridge *Bridge) Init() {
bridge.AS, err = bridge.Config.MakeAppService() bridge.AS, err = bridge.Config.MakeAppService()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Failed to initialize AppService:", err) fmt.Fprintln(os.Stderr, "Failed to initialize AppService:", err)
os.Exit(12) os.Exit(11)
} }
bridge.AS.Init() bridge.AS.Init()
bridge.Bot = bridge.AS.BotIntent() bridge.Bot = bridge.AS.BotIntent()
@ -122,7 +118,7 @@ func (bridge *Bridge) Init() {
err = log.OpenFile() err = log.OpenFile()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Failed to open log file:", err) fmt.Fprintln(os.Stderr, "Failed to open log file:", err)
os.Exit(13) os.Exit(12)
} }
bridge.AS.Log = log.Sub("Matrix") bridge.AS.Log = log.Sub("Matrix")
@ -131,7 +127,7 @@ func (bridge *Bridge) Init() {
err = bridge.StateStore.Load() err = bridge.StateStore.Load()
if err != nil { if err != nil {
bridge.Log.Fatalln("Failed to load state store:", err) bridge.Log.Fatalln("Failed to load state store:", err)
os.Exit(14) os.Exit(13)
} }
bridge.AS.StateStore = bridge.StateStore bridge.AS.StateStore = bridge.StateStore
@ -139,7 +135,7 @@ func (bridge *Bridge) Init() {
bridge.DB, err = database.New(bridge.Config.AppService.Database.URI) bridge.DB, err = database.New(bridge.Config.AppService.Database.URI)
if err != nil { if err != nil {
bridge.Log.Fatalln("Failed to initialize database:", err) bridge.Log.Fatalln("Failed to initialize database:", err)
os.Exit(15) os.Exit(14)
} }
bridge.Log.Debugln("Initializing Matrix event processor") bridge.Log.Debugln("Initializing Matrix event processor")
@ -153,7 +149,7 @@ func (bridge *Bridge) Start() {
err := bridge.DB.CreateTables() err := bridge.DB.CreateTables()
if err != nil { if err != nil {
bridge.Log.Fatalln("Failed to create database tables:", err) bridge.Log.Fatalln("Failed to create database tables:", err)
os.Exit(16) os.Exit(15)
} }
bridge.Log.Debugln("Starting application service HTTP server") bridge.Log.Debugln("Starting application service HTTP server")
go bridge.AS.Start() go bridge.AS.Start()