lints now!
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Jeffrey Paul 2020-09-21 20:30:12 -07:00
parent a52878297f
commit 2c5f901b08
4 changed files with 51 additions and 39 deletions

View File

@ -132,6 +132,9 @@ issues:
- linters: - linters:
- gocritic - gocritic
text: "unnecessaryDefer:" text: "unnecessaryDefer:"
- linters:
- gocritic
text: "commentedOutCode:"
run: run:
skip-dirs: skip-dirs:

View File

@ -9,12 +9,13 @@ import (
"time" "time"
"git.eeqj.de/sneak/goutil" "git.eeqj.de/sneak/goutil"
"github.com/k0kubun/pp"
// db driver:
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func (hp *HistoryPoster) postUrls(ctx context.Context, cancel context.CancelFunc) { func (hp *HistoryPoster) postURLs(ctx context.Context, cancel context.CancelFunc) {
log.Info().Msg("finding history files") log.Info().Msg("finding history files")
files, err := findHistoryFiles() files, err := findHistoryFiles()
if err != nil { if err != nil {
@ -30,40 +31,39 @@ func (hp *HistoryPoster) postUrls(ctx context.Context, cancel context.CancelFunc
hp.shutdown(err.Error(), -1) hp.shutdown(err.Error(), -1)
} }
for _, hitem := range hl { for _, hitem := range hl {
hp.processUrlFromHistory(hitem) hp.processURLFromHistory(hitem)
} }
} }
} }
func (hp *HistoryPoster) processUrlFromHistory(hi historyItem) { func (hp *HistoryPoster) processURLFromHistory(hi historyItem) {
log.Debug(). log.Debug().
Str("url", hi.url). Str("url", hi.URL).
Msg("got url to process") Msg("got url to process")
if hp.store.UrlAlreadySeen(hi.url) { if hp.store.URLAlreadySeen(hi.URL) {
return return
} }
log.Debug(). log.Debug().
Str("url", hi.url). Str("url", hi.URL).
Msg("url is new, must be posted") Msg("url is new, must be posted")
err := hp.postUrl(hi) err := hp.postURL(hi)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Msg("url could not be posted :(") Msg("url could not be posted :(")
} else { } else {
hp.store.MarkUrlSeen(hi.url) hp.store.MarkURLSeen(hi.URL)
} }
} }
func (hp *HistoryPoster) postUrl(hi historyItem) error { func (hp *HistoryPoster) postURL(hi historyItem) error {
// FIXME // FIXME
//panic("unimplemented") // panic("unimplemented")
return nil return nil
} }
func findHistoryFiles() ([]string, error) { func findHistoryFiles() ([]string, error) {
//FIXME make this support safari one day // FIXME make this support safari one day
home := os.Getenv("HOME") home := os.Getenv("HOME")
chromeDir := home + "/Library/Application Support/Google/Chrome" chromeDir := home + "/Library/Application Support/Google/Chrome"
defaultProfileDir := chromeDir + "/Default" defaultProfileDir := chromeDir + "/Default"
@ -81,10 +81,10 @@ func findHistoryFiles() ([]string, error) {
} }
type historyItem struct { type historyItem struct {
last_visit_time time.Time lastVisitTime time.Time
url string URL string
title string title string
visit_count int visitCount int
} }
func dumpHistoryFromChromeHistoryFile(path string) ([]historyItem, error) { func dumpHistoryFromChromeHistoryFile(path string) ([]historyItem, error) {
@ -98,7 +98,15 @@ func dumpHistoryFromChromeHistoryFile(path string) ([]historyItem, error) {
Msg("created tempdir") Msg("created tempdir")
dbfn := tempdir + "/History" dbfn := tempdir + "/History"
goutil.CopyFile(path, dbfn) err = goutil.CopyFile(path, dbfn)
if err != nil {
log.Error().
Str("source", path).
Str("target", dbfn).
Err(err).
Msg("unable to copy history file")
return nil, err
}
log.Debug(). log.Debug().
Str("dbfn", dbfn). Str("dbfn", dbfn).
Msg("copied history file") Msg("copied history file")
@ -145,30 +153,34 @@ func dumpHistoryFromChromeHistoryFile(path string) ([]historyItem, error) {
return nil, err return nil, err
} }
if rows.Err() != nil {
return nil, rows.Err()
}
defer rows.Close() defer rows.Close()
output := make([]historyItem, 0) output := make([]historyItem, 0)
for rows.Next() { for rows.Next() {
//log.Debug().Msg("processing row") // log.Debug().Msg("processing row")
var last_visit_time int64 var lastVisitTime int64
var url string var url string
var title string var title string
var visit_count int var visitCount int
err := rows.Scan(&last_visit_time, &url, &title, &visit_count) err := rows.Scan(&lastVisitTime, &url, &title, &visitCount)
if err != nil { if err != nil {
log.Debug().Err(err).Msg("row error") log.Debug().Err(err).Msg("row error")
return nil, err return nil, err
} }
t := goutil.TimeFromWebKit(last_visit_time).UTC() t := goutil.TimeFromWebKit(lastVisitTime).UTC()
hi := historyItem{ hi := historyItem{
last_visit_time: t, lastVisitTime: t,
url: url, URL: url,
title: title, title: title,
visit_count: visit_count, visitCount: visitCount,
} }
output = append(output, hi) output = append(output, hi)
} }
pp.Print(output) // pp.Print(output)
return output, nil return output, nil
} }

View File

@ -25,7 +25,6 @@ type HistoryPoster struct {
startup time.Time startup time.Time
appCtx context.Context appCtx context.Context
shutdownFunc context.CancelFunc shutdownFunc context.CancelFunc
newUrlChan chan string
exitCode int exitCode int
store *store.Store store *store.Store
logfh *os.File logfh *os.File
@ -38,7 +37,6 @@ func CLIEntry(version, buildarch string) int {
hp.buildarch = buildarch hp.buildarch = buildarch
hp.startup = time.Now() hp.startup = time.Now()
hp.exitCode = 0 hp.exitCode = 0
hp.newUrlChan = make(chan string)
c := make(chan os.Signal) c := make(chan os.Signal)
signal.Ignore(syscall.SIGPIPE) signal.Ignore(syscall.SIGPIPE)
@ -47,12 +45,12 @@ func CLIEntry(version, buildarch string) int {
hp.configure() hp.configure()
hp.setupLogging() hp.setupLogging()
store, err := store.NewStore() s, err := store.NewStore()
if err != nil { if err != nil {
hp.shutdown("cannot create state file: "+err.Error(), -1) hp.shutdown("cannot create state file: "+err.Error(), -1)
return hp.exitCode return hp.exitCode
} }
hp.store = store hp.store = s
hp.appCtx, hp.shutdownFunc = context.WithCancel(context.Background()) hp.appCtx, hp.shutdownFunc = context.WithCancel(context.Background())
go func() { go func() {
@ -86,13 +84,12 @@ func (hp *HistoryPoster) configure() {
} }
} }
//if viper.GetBool("debug") { // if viper.GetBool("debug") {
// pp.Print(viper.AllSettings()) // pp.Print(viper.AllSettings())
//} // }
} }
func (hp *HistoryPoster) runForever(ctx context.Context) int { func (hp *HistoryPoster) runForever(ctx context.Context) int {
log.Info().Msg("entering main loop") log.Info().Msg("entering main loop")
interval := 60 * time.Second interval := 60 * time.Second
@ -101,13 +98,13 @@ func (hp *HistoryPoster) runForever(ctx context.Context) int {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
go func() { go func() {
// do it once right now, without an insta-tick // do it once right now, without an insta-tick
go func() { hp.postUrls(context.WithTimeout(ctx, timeout)) }() go func() { hp.postURLs(context.WithTimeout(ctx, timeout)) }()
// then go do it repeatedly: // then go do it repeatedly:
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
go func() { hp.postUrls(context.WithTimeout(ctx, timeout)) }() go func() { hp.postURLs(context.WithTimeout(ctx, timeout)) }()
case <-ctx.Done(): case <-ctx.Done():
ticker.Stop() ticker.Stop()
return return

View File

@ -62,7 +62,7 @@ func (s *Store) Close() {
s.db.Close() s.db.Close()
} }
func (s *Store) MarkUrlSeen(url string) { func (s *Store) MarkURLSeen(url string) {
q := fmt.Sprintf(`INSERT into %s (url, posted) VALUES (?, date('now'));`, tablename) q := fmt.Sprintf(`INSERT into %s (url, posted) VALUES (?, date('now'));`, tablename)
_, err := s.db.Exec(q, url) _, err := s.db.Exec(q, url)
if err != nil { if err != nil {
@ -75,7 +75,7 @@ func (s *Store) MarkUrlSeen(url string) {
Msg("url added to db") Msg("url added to db")
} }
func (s *Store) UrlAlreadySeen(url string) bool { func (s *Store) URLAlreadySeen(url string) bool {
q := fmt.Sprintf(`select id from %s where url = ?;`, tablename) q := fmt.Sprintf(`select id from %s where url = ?;`, tablename)
row := s.db.QueryRow(q, url) row := s.db.QueryRow(q, url)