Explorar el Código

Start of the run command and the bridge type

Gary Kramlich hace 3 años
padre
commit
4be28617e0
Se han modificado 3 ficheros con 90 adiciones y 0 borrados
  1. 53 0
      bridge/bridge.go
  2. 2 0
      main.go
  3. 35 0
      run/cmd.go

+ 53 - 0
bridge/bridge.go

@@ -0,0 +1,53 @@
+package bridge
+
+import (
+	log "maunium.net/go/maulogger/v2"
+	"maunium.net/go/mautrix/appservice"
+
+	"gitlab.com/beeper/discord/config"
+	"gitlab.com/beeper/discord/version"
+)
+
+type Bridge struct {
+	config *config.Config
+
+	log log.Logger
+
+	as             *appservice.AppService
+	eventProcessor *appservice.EventProcessor
+	bot            *appservice.IntentAPI
+}
+
+func New(cfg *config.Config) (*Bridge, error) {
+	// Create the logger.
+	logger, err := cfg.CreateLogger()
+	if err != nil {
+		return nil, err
+	}
+
+	logger.Infoln("Initializing version", version.String)
+
+	// Create the app service.
+	appservice, err := cfg.CreateAppService()
+	if err != nil {
+		return nil, err
+	}
+	appservice.Log = log.Sub("matrix")
+
+	// Create the bridge.
+	bridge := &Bridge{
+		config: cfg,
+		log:    logger,
+		as:     appservice,
+	}
+
+	return bridge, nil
+}
+
+func (b *Bridge) Start() {
+	b.log.Infoln("bridge started")
+}
+
+func (b *Bridge) Stop() {
+	b.log.Infoln("bridge stopped")
+}

+ 2 - 0
main.go

@@ -10,6 +10,7 @@ import (
 	"gitlab.com/beeper/discord/consts"
 	"gitlab.com/beeper/discord/globals"
 	"gitlab.com/beeper/discord/registration"
+	"gitlab.com/beeper/discord/run"
 	"gitlab.com/beeper/discord/version"
 )
 
@@ -18,6 +19,7 @@ var cli struct {
 
 	GenerateConfig       config.Cmd       `kong:"cmd,help='Generate the default configuration and exit.'"`
 	GenerateRegistration registration.Cmd `kong:"cmd,help='Generate the registration file for synapse and exit.'"`
+	Run                  run.Cmd          `kong:"cmd,help='Run the bridge.',default='1'"`
 	Version              version.Cmd      `kong:"cmd,help='Display the version and exit.'"`
 }
 

+ 35 - 0
run/cmd.go

@@ -0,0 +1,35 @@
+package run
+
+import (
+	"os"
+	"os/signal"
+	"syscall"
+
+	"gitlab.com/beeper/discord/bridge"
+	"gitlab.com/beeper/discord/config"
+	"gitlab.com/beeper/discord/globals"
+)
+
+type Cmd struct{}
+
+func (c *Cmd) Run(g *globals.Globals) error {
+	cfg, err := config.FromFile(g.Config)
+	if err != nil {
+		return err
+	}
+
+	bridge, err := bridge.New(cfg)
+	if err != nil {
+		return err
+	}
+
+	bridge.Start()
+
+	ch := make(chan os.Signal)
+	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
+	<-ch
+
+	bridge.Stop()
+
+	return nil
+}