1
0
mirror of https://github.com/pinpox/gitea-matrix-bot synced 2024-11-22 19:31:58 +01:00

Adher to my newly established coding standards.

This commit is contained in:
Pablo Ovelleiro Corral 2019-05-15 18:25:32 +02:00
parent 76960a5320
commit 3b9ee2f12e
No known key found for this signature in database
GPG Key ID: F7C1D57C8464E825

86
main.go

@ -2,9 +2,11 @@ package main
import ( import (
// "fmt" // "fmt"
"time"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
"time"
// "time" // "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -14,63 +16,63 @@ var err error
var mygiteabot *GiteaBot var mygiteabot *GiteaBot
var ( var (
verboseFlag = kingpin.Flag("verbose", "Verbose mode, displays additional information.").Short('v').Default("false").Bool() 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() 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() 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() syncSecsFlag = kingpin.Flag("sync", "Matrix synchronizing interval").Default("1").Int()
) )
func init() { func init() {
//Parse flags and set log-level //Parse flags and set log-level
kingpin.Version("1.0.0") kingpin.Version("1.0.0")
kingpin.Parse() kingpin.Parse()
if !*verboseFlag { if !*verboseFlag {
log.SetLevel(log.InfoLevel) log.SetLevel(log.InfoLevel)
} else { } else {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} }
log.SetFormatter(&log.TextFormatter{ log.SetFormatter(&log.TextFormatter{
FullTimestamp: true, FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05", TimestampFormat: "2006-01-02 15:04:05",
}) })
//Load config //Load config
log.Debugf("Using configuration file %s", *configFlag) log.Debugf("Using configuration file %s", *configFlag)
cfg, err = ini.Load(*configFlag) cfg, err = ini.Load(*configFlag)
if err != nil { if err != nil {
log.Fatalf("Fail to read file: %v", err) log.Fatalf("Fail to read file: %v", err)
} }
matrixUser := cfg.Section("matrix").Key("matrix_user").String() matrixUser := cfg.Section("matrix").Key("matrix_user").String()
matrixPass := cfg.Section("matrix").Key("matrix_pass").String() matrixPass := cfg.Section("matrix").Key("matrix_pass").String()
matrixHost := cfg.Section("matrix").Key("matrix_pass").String() matrixHost := cfg.Section("matrix").Key("matrix_pass").String()
dbPath := cfg.Section("bot").Key("db_path").String() dbPath := cfg.Section("bot").Key("db_path").String()
//Set up the bot //Set up the bot
// func NewGiteaBot(user, pass, host, string, DBPath string) *GiteaBot { // func NewGiteaBot(user, pass, host, string, DBPath string) *GiteaBot {
mygiteabot = NewGiteaBot(matrixUser, matrixPass, matrixHost, dbPath) mygiteabot = NewGiteaBot(matrixUser, matrixPass, matrixHost, dbPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Debug("Bot created") log.Debug("Bot created")
} }
func main() { func main() {
log.Info("Setting up POST-listener") log.Info("Setting up POST-listener")
go func() { go func() {
for { for {
mygiteabot.Sync() mygiteabot.Sync()
time.Sleep(time.Duration(*syncSecsFlag) * time.Second) time.Sleep(time.Duration(*syncSecsFlag) * time.Second)
} }
}() }()
setupListener() setupListener()
} }