0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-01-12 19:48:12 +00:00
crazy-max_diun/cmd/main.go
2020-06-07 22:04:31 +02:00

94 lines
2.1 KiB
Go

package main
import (
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/alecthomas/kong"
"github.com/crazy-max/diun/v4/internal/app"
"github.com/crazy-max/diun/v4/internal/config"
"github.com/crazy-max/diun/v4/internal/logging"
"github.com/crazy-max/diun/v4/internal/model"
"github.com/rs/zerolog/log"
)
var (
diun *app.Diun
cli model.Cli
version = "dev"
meta = model.Meta{
ID: "diun",
Name: "Diun",
Desc: "Docker image update notifier",
URL: "https://github.com/crazy-max/diun",
Logo: "https://raw.githubusercontent.com/crazy-max/diun/master/.res/diun.png",
Author: "CrazyMax",
}
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
meta.Version = version
meta.UserAgent = fmt.Sprintf("%s/%s go/%s %s", meta.ID, meta.Version, runtime.Version()[2:], strings.Title(runtime.GOOS))
// Parse command line
_ = kong.Parse(&cli,
kong.Name(meta.ID),
kong.Description(fmt.Sprintf("%s. More info: %s", meta.Desc, meta.URL)),
kong.UsageOnError(),
kong.Vars{
"version": fmt.Sprintf("%s", version),
},
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
Summary: true,
}))
// Load timezone location
location, err := time.LoadLocation(cli.Timezone)
if err != nil {
log.Panic().Err(err).Msgf("Cannot load timezone %s", cli.Timezone)
}
// Init
logging.Configure(&cli, location)
log.Info().Str("version", version).Msgf("Starting %s", meta.Name)
// Handle os signals
channel := make(chan os.Signal)
signal.Notify(channel, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-channel
diun.Close()
log.Warn().Msgf("Caught signal %v", sig)
os.Exit(0)
}()
// Load configuration
cfg, err := config.Load(cli.Cfgfile)
if err != nil {
log.Fatal().Err(err).Msg("Cannot load configuration")
}
log.Debug().Msg(cfg.String())
// Init
if diun, err = app.New(meta, cfg, location); err != nil {
log.Fatal().Err(err).Msgf("Cannot initialize %s", meta.Name)
}
// Test notif
if cli.TestNotif {
diun.TestNotif()
return
}
// Start
if err = diun.Start(); err != nil {
log.Fatal().Err(err).Msgf("Cannot start %s", meta.Name)
}
}