This commit is contained in:
23
hp/detector.go
Normal file
23
hp/detector.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func findHistoryFiles() []string {
|
||||
//FIXME make this support safari one day
|
||||
home := os.Getenv("HOME")
|
||||
chromeDir := home + "/Library/Application Support/Google/Chrome"
|
||||
defaultProfileDir := chromeDir + "/Default"
|
||||
output := make([]string, 0)
|
||||
output = append(output, defaultProfileDir+"/History")
|
||||
otherProfiles, err := filepath.Glob(chromeDir + "/Profile *")
|
||||
if err != nil {
|
||||
return output
|
||||
}
|
||||
for _, v := range otherProfiles {
|
||||
output = append(output, v+"/History")
|
||||
}
|
||||
return output
|
||||
}
|
||||
125
hp/historyposter.go
Normal file
125
hp/historyposter.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"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
|
||||
func CLIEntry(version, buildarch string) int {
|
||||
hp := new(HistoryPoster)
|
||||
hp.version = version
|
||||
hp.buildarch = buildarch
|
||||
hp.startup = time.Now()
|
||||
hp.newUrlChan = make(chan string)
|
||||
|
||||
c := make(chan os.Signal)
|
||||
signal.Ignore(syscall.SIGPIPE)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
hp.configure()
|
||||
hp.setupLogging()
|
||||
|
||||
hp.appCtx, hp.shutdownFunc = context.WithCancel(context.Background())
|
||||
go func() {
|
||||
// this sits and waits for an interrupt to be received
|
||||
sig := <-c
|
||||
log.Info().Msgf("signal received: %+v", sig)
|
||||
hp.shutdownFunc()
|
||||
}()
|
||||
return hp.runForever(hp.appCtx)
|
||||
}
|
||||
|
||||
// HistoryPoster is the main app framework object
|
||||
type HistoryPoster struct {
|
||||
version string
|
||||
buildarch string
|
||||
startup time.Time
|
||||
appCtx context.Context
|
||||
shutdownFunc context.CancelFunc
|
||||
newUrlChan chan string
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
if viper.GetBool("debug") {
|
||||
pp.Print(viper.AllSettings())
|
||||
}
|
||||
}
|
||||
|
||||
func (hp *HistoryPoster) runForever(ctx context.Context) int {
|
||||
|
||||
log.Info().Msg("this is where i do stuff")
|
||||
|
||||
_ = findHistoryFiles()
|
||||
|
||||
<-ctx.Done()
|
||||
log.Info().Msgf("shutting down")
|
||||
return 0
|
||||
}
|
||||
|
||||
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).
|
||||
Str("os", runtime.GOOS).
|
||||
Msg("starting")
|
||||
}
|
||||
Reference in New Issue
Block a user