1
0
mirror of https://github.com/pinpox/gitea-matrix-bot synced 2024-11-22 10:21:58 +01:00
gitea-matrix-bot/main.go

77 lines
1.8 KiB
Go
Raw Permalink Normal View History

2019-04-13 12:31:28 +02:00
package main
import (
// "fmt"
2019-04-13 12:31:28 +02:00
"github.com/go-ini/ini"
2023-07-13 15:42:23 +02:00
"github.com/alecthomas/kingpin/v2"
2019-04-20 15:34:03 +02:00
"time"
// "time"
log "github.com/sirupsen/logrus"
2019-04-13 12:31:28 +02:00
)
var cfg *ini.File
var err error
2019-04-14 22:38:13 +02:00
var mygiteabot *GiteaBot
2019-04-13 12:31:28 +02:00
2019-04-20 15:34:03 +02:00
var (
verboseFlag = kingpin.Flag("verbose", "Verbose mode, displays additional information.").Short('v').Default("false").Bool()
configFlag = kingpin.Flag("config", "Configuration file to use").Short('c').Default("config.ini").String()
initDBFlag = kingpin.Flag("initdb", "Initialize the database. If it exists, it will be overwritten!").Default("false").Bool()
syncSecsFlag = kingpin.Flag("sync", "Matrix synchronizing interval").Default("1").Int()
)
2019-04-13 12:31:28 +02:00
func init() {
2019-04-14 22:51:52 +02:00
2019-04-20 15:34:03 +02:00
//Parse flags and set log-level
kingpin.Version("1.0.0")
kingpin.Parse()
if !*verboseFlag {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.DebugLevel)
}
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
2019-04-20 15:34:03 +02:00
2019-04-14 22:51:52 +02:00
//Load config
2019-04-20 15:34:03 +02:00
log.Debugf("Using configuration file %s", *configFlag)
cfg, err = ini.Load(*configFlag)
2019-04-13 12:31:28 +02:00
if err != nil {
log.Fatalf("Fail to read file: %v", err)
2019-04-13 12:31:28 +02:00
}
matrixUser := cfg.Section("matrix").Key("matrix_user").String()
matrixPass := cfg.Section("matrix").Key("matrix_pass").String()
2023-07-13 18:29:50 +02:00
matrixHost := cfg.Section("matrix").Key("matrix_host").String()
2019-04-20 15:34:03 +02:00
dbPath := cfg.Section("bot").Key("db_path").String()
2019-04-13 12:31:28 +02:00
2019-04-20 15:34:03 +02:00
//Set up the bot
2019-04-23 17:57:32 +02:00
// func NewGiteaBot(user, pass, host, string, DBPath string) *GiteaBot {
2019-04-22 20:27:53 +02:00
mygiteabot = NewGiteaBot(matrixUser, matrixPass, matrixHost, dbPath)
2019-04-13 12:31:28 +02:00
if err != nil {
log.Fatal(err)
}
2019-04-20 15:34:03 +02:00
log.Debug("Bot created")
}
2019-04-13 12:31:28 +02:00
func main() {
2019-04-13 12:31:28 +02:00
2019-04-20 15:34:03 +02:00
log.Info("Setting up POST-listener")
2019-04-18 21:27:49 +02:00
go func() {
for {
mygiteabot.Sync()
2019-04-20 15:34:03 +02:00
time.Sleep(time.Duration(*syncSecsFlag) * time.Second)
2019-04-18 21:27:49 +02:00
}
}()
2019-04-13 12:31:28 +02:00
setupListener()
}