historyposter/process/historyposter.go

85 lines
1.6 KiB
Go

package process
import (
"context"
"fmt"
"github.com/k0kubun/pp"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"os"
"time"
)
// CLIEntry is the main entrypoint
func CLIEntry(version string, buildarch string) int {
hp := new(HistoryPoster)
hp.version = version
hp.buildarch = buildarch
hp.startup = time.Now()
c := make(chan os.Signal, 1)
signal.Notify(hp.shutdownChannel, os.Interrupt)
hp.configure()
hp.setupLogging()
hp.setupDatabase()
ctx, cancel := context.WithCancel(context.Background())
go func() {
sig := <-c
log.Info().Msgf("signal received: %+v", sig)
// FIXME this might need to be scoped to a specific signal
cancel()
}()
return hp.runForever(ctx)
}
type HistoryPoster struct {
version string
buildarch string
startup time.Time
}
func (hp *HistoryPoster) runForever(ctx context.Context) int {
}
func (hp *HistoryPoster) setupLogging() {
log.Logger = log.With().Caller().Logger()
tty := isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())
if tty {
out := zerolog.NewConsoleWriter(
func(w *zerolog.ConsoleWriter) {
// Customize time format
w.TimeFormat = time.RFC3339
},
)
log.Logger = log.Output(out)
}
// always log in UTC
zerolog.TimestampFunc = func() time.Time {
return time.Now().UTC()
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if viper.GetBool("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
hp.identify()
}
func (hp *HistoryPoster) identify() {
log.Info().
Str("version", hp.version).
Str("buildarch", hp.buildarch).
Msg("starting")
}