2020-09-21 20:04:20 +00:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-21 21:11:18 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-09-21 21:44:29 +00:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2020-09-21 21:11:18 +00:00
|
|
|
"time"
|
|
|
|
|
2020-09-21 20:04:20 +00:00
|
|
|
"github.com/k0kubun/pp"
|
|
|
|
"github.com/mattn/go-isatty"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CLIEntry is the main entrypoint
|
2020-09-21 21:11:18 +00:00
|
|
|
func CLIEntry(version, buildarch string) int {
|
2020-09-21 20:04:20 +00:00
|
|
|
hp := new(HistoryPoster)
|
|
|
|
hp.version = version
|
|
|
|
hp.buildarch = buildarch
|
|
|
|
hp.startup = time.Now()
|
2020-09-21 21:44:29 +00:00
|
|
|
hp.newUrlChan = make(chan string)
|
2020-09-21 20:04:20 +00:00
|
|
|
|
2020-09-21 21:44:29 +00:00
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Ignore(syscall.SIGPIPE)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
2020-09-21 20:04:20 +00:00
|
|
|
|
|
|
|
hp.configure()
|
|
|
|
hp.setupLogging()
|
|
|
|
|
2020-09-21 21:44:29 +00:00
|
|
|
hp.appCtx, hp.shutdownFunc = context.WithCancel(context.Background())
|
2020-09-21 20:04:20 +00:00
|
|
|
go func() {
|
2020-09-21 21:11:18 +00:00
|
|
|
// this sits and waits for an interrupt to be received
|
2020-09-21 20:04:20 +00:00
|
|
|
sig := <-c
|
|
|
|
log.Info().Msgf("signal received: %+v", sig)
|
2020-09-21 21:44:29 +00:00
|
|
|
hp.shutdownFunc()
|
2020-09-21 20:04:20 +00:00
|
|
|
}()
|
2020-09-21 21:44:29 +00:00
|
|
|
return hp.runForever(hp.appCtx)
|
2020-09-21 20:04:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:44:29 +00:00
|
|
|
// HistoryPoster is the main app framework object
|
2020-09-21 20:04:20 +00:00
|
|
|
type HistoryPoster struct {
|
2020-09-21 21:44:29 +00:00
|
|
|
version string
|
|
|
|
buildarch string
|
|
|
|
startup time.Time
|
|
|
|
appCtx context.Context
|
|
|
|
shutdownFunc context.CancelFunc
|
|
|
|
newUrlChan chan string
|
2020-09-21 20:04:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:11:18 +00:00
|
|
|
func (hp *HistoryPoster) configure() {
|
|
|
|
viper.SetConfigName("historyposter")
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.AddConfigPath("/etc/historyposter") // path to look for the config file in
|
|
|
|
viper.AddConfigPath("$HOME/.config/historyposter") // call multiple times to add many search paths
|
|
|
|
|
|
|
|
viper.SetEnvPrefix("HISTORYPOSTER")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
viper.SetDefault("Debug", false)
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
// Config file not found; ignore error if desired
|
|
|
|
} else {
|
|
|
|
// Config file was found but another error was produced
|
|
|
|
log.Panic().
|
|
|
|
Err(err).
|
|
|
|
Msg("cannot read config file")
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 20:04:20 +00:00
|
|
|
|
2020-09-21 21:11:18 +00:00
|
|
|
if viper.GetBool("debug") {
|
|
|
|
pp.Print(viper.AllSettings())
|
|
|
|
}
|
2020-09-21 20:04:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:11:18 +00:00
|
|
|
func (hp *HistoryPoster) runForever(ctx context.Context) int {
|
2020-09-21 21:44:29 +00:00
|
|
|
|
|
|
|
log.Info().Msg("this is where i do stuff")
|
|
|
|
|
|
|
|
_ = findHistoryFiles()
|
|
|
|
|
2020-09-21 21:11:18 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
log.Info().Msgf("shutting down")
|
|
|
|
return 0
|
|
|
|
}
|
2020-09-21 20:04:20 +00:00
|
|
|
|
2020-09-21 21:11:18 +00:00
|
|
|
func (hp *HistoryPoster) setupLogging() {
|
2020-09-21 20:04:20 +00:00
|
|
|
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).
|
2020-09-21 21:44:29 +00:00
|
|
|
Str("os", runtime.GOOS).
|
2020-09-21 20:04:20 +00:00
|
|
|
Msg("starting")
|
|
|
|
}
|