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:
- gocritic
text: "unnecessaryDefer:"
- linters:
- gocritic
text: "commentedOutCode:"
run:
skip-dirs:

View File

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

View File

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

View File

@ -62,7 +62,7 @@ func (s *Store) 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)
_, err := s.db.Exec(q, url)
if err != nil {
@ -75,7 +75,7 @@ func (s *Store) MarkUrlSeen(url string) {
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)
row := s.db.QueryRow(q, url)