go-pingbot/main.go

140 lines
2.5 KiB
Go
Raw Normal View History

2021-07-11 21:19:37 +00:00
package main
import (
"bufio"
"fmt"
2021-08-14 21:43:30 +00:00
"os"
"sync"
"time"
2021-07-29 18:51:15 +00:00
2021-08-12 19:26:54 +00:00
"github.com/MedzikUser/go-utils/common"
2021-08-14 21:43:30 +00:00
"github.com/MedzikUser/go-utils/updater"
"github.com/jpillora/opts"
2021-07-11 21:19:37 +00:00
"gitlab.com/gaming0skar123/go/pingbot/backend"
"gitlab.com/gaming0skar123/go/pingbot/config"
2021-07-29 18:51:15 +00:00
"gitlab.com/gaming0skar123/go/pingbot/database/mongo"
2021-07-11 21:19:37 +00:00
"gitlab.com/gaming0skar123/go/pingbot/website"
)
var log = common.Log
type cmdOpts struct {
Update bool `opts:"help=update version to latest e.g. if update is major"`
}
2021-07-11 21:19:37 +00:00
func main() {
log.Info("You're using verion: ", config.Version)
var wg sync.WaitGroup
c := cmdOpts{}
opts.Parse(&c)
if c.Update {
client := updater.Client{
GitHub: config.GH_Repo,
GitHubToken: config.GH_Token,
Version: config.Version,
Binary: "pingbot.out",
CheckEvery: config.Toml.AutoUpdate.Check * time.Minute,
AfterUpdate: func() {
log.Info("Updated!")
if !config.Toml.Options.Stop_After_Ping {
os.Exit(0)
}
},
Major: false,
}
err := client.Update()
if err != nil && err.Error() == "major update" {
fmt.Print("Update to new major version? (y/N) ")
reader := bufio.NewReader((os.Stdin))
char, _, err := reader.ReadRune()
if err != nil {
fmt.Println(err)
}
switch char {
case 'y':
client.Major = true
2021-08-31 18:22:45 +00:00
err := client.Update()
if err != nil {
log.Error(err)
os.Exit(1)
}
case 'Y':
client.Major = true
2021-08-31 18:22:45 +00:00
err := client.Update()
if err != nil {
log.Error(err)
os.Exit(1)
}
default:
log.Warn("Canceled!")
os.Exit(2)
}
} else if err != nil {
log.Error(err)
os.Exit(1)
} else {
log.Info("You're using latest version!")
os.Exit(0)
}
}
2021-09-02 20:44:51 +00:00
mongo.Connect()
2021-07-29 18:51:15 +00:00
if config.Toml.AutoUpdate.Enabled {
wg.Add(1)
2021-08-14 21:43:30 +00:00
client := updater.Client{
2021-08-18 16:51:32 +00:00
GitHub: config.GH_Repo,
GitHubToken: config.GH_Token,
2021-08-18 16:51:32 +00:00
Version: config.Version,
Binary: "pingbot.out",
2021-08-22 10:04:12 +00:00
CheckEvery: config.Toml.AutoUpdate.Check * time.Minute,
2021-08-14 21:43:30 +00:00
AfterUpdate: func() {
log.Info("Updated!")
2021-08-29 10:14:35 +00:00
if !config.Toml.Options.Stop_After_Ping {
os.Exit(0)
}
2021-08-14 21:43:30 +00:00
},
2021-08-22 10:04:12 +00:00
Major: false,
2021-08-14 21:43:30 +00:00
}
go client.AutoUpdater()
2021-07-29 18:51:15 +00:00
} else {
log.Warn("Auto Update -> Disabled")
}
2021-07-11 21:19:37 +00:00
2021-08-29 10:14:35 +00:00
if config.Toml.Options.Stop_After_Ping {
backend.StopAfterPing()
2021-08-29 10:14:35 +00:00
os.Exit(0)
}
if config.Toml.HTTP.Enabled {
wg.Add(1)
go website.Server()
} else {
log.Warn("HTTP Server -> Disabled")
}
if config.Toml.Backend.Enabled {
wg.Add(1)
go backend.Ticker()
} else {
log.Warn("Backend -> Disabled")
}
config.StartTime = time.Now()
wg.Wait()
2021-07-11 21:19:37 +00:00
}