Options approach for creating new CwtchBot #2

Open
decentral1se wants to merge 3 commits from decentral1se/cwtchbot:options-refactor into main
2 changed files with 23 additions and 19 deletions

38
bot.go
View File

@ -2,21 +2,22 @@ package bot
import (
"crypto/rand"
"cwtch.im/cwtch/app"
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/peer"
"cwtch.im/cwtch/protocol/connections"
"cwtch.im/cwtch/settings"
"encoding/base64"
"encoding/json"
"git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
mrand "math/rand"
"os"
"path"
"path/filepath"
"time"
"cwtch.im/cwtch/app"
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/peer"
"cwtch.im/cwtch/protocol/connections"
"cwtch.im/cwtch/settings"
"git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
)
type CwtchBot struct {
@ -29,21 +30,24 @@ type CwtchBot struct {
experiments []string
}
func NewCwtchBot(userdir string, peername string) *CwtchBot {
cb := new(CwtchBot)
cb.dir = userdir
cb.peername = peername
cb.engineHooks = connections.DefaultEngineHooks{}
cb.experiments = nil
return cb
// CwtchBotOption modifies a new CwtchBot.
type CwtchBotOption func(c *CwtchBot)
// WithExperiments specifies the experiments option of a CwtchBot.
func WithExperiments(experiments []string) CwtchBotOption {
return func(c *CwtchBot) {
c.experiments = experiments
}
}
func NewCwtchBotWithExperiments(userdir string, peername string, experiments []string) *CwtchBot {
func NewCwtchBot(userdir, peername string, opts ...CwtchBotOption) *CwtchBot {
cb := new(CwtchBot)
cb.dir = userdir
cb.peername = peername
cb.engineHooks = connections.DefaultEngineHooks{}
cb.experiments = experiments
for _, optFunc := range opts {
optFunc(cb)
}
return cb
}