From 1a239b3f324df50725a53872c16022c455695db3 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Mon, 1 Nov 2021 10:01:21 +0100 Subject: [PATCH] sync command --- cmd/cli/command/command.go | 2 ++ cmd/cli/command/sync.go | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 cmd/cli/command/sync.go diff --git a/cmd/cli/command/command.go b/cmd/cli/command/command.go index dcd3c2d..754e4fd 100644 --- a/cmd/cli/command/command.go +++ b/cmd/cli/command/command.go @@ -34,6 +34,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) { return NewFetch(conf) case "send": return NewSend(conf) + case "sync": + return NewSync(conf) case "today": return NewToday(conf) case "tomorrow": diff --git a/cmd/cli/command/sync.go b/cmd/cli/command/sync.go new file mode 100644 index 0000000..b0281a7 --- /dev/null +++ b/cmd/cli/command/sync.go @@ -0,0 +1,60 @@ +package command + +import ( + "fmt" + "time" + + "ewintr.nl/gte/cmd/cli/format" + "ewintr.nl/gte/internal/configuration" + "ewintr.nl/gte/internal/process" + "ewintr.nl/gte/internal/storage" + "ewintr.nl/gte/pkg/msend" + "ewintr.nl/gte/pkg/mstore" +) + +type Sync struct { + fetcher *process.Fetch + sender *process.Send + fetchInterval time.Duration + fetchLatest time.Time +} + +func NewSync(conf *configuration.Configuration) (*Sync, error) { + local, err := storage.NewSqlite(conf.Sqlite()) + if err != nil { + return &Sync{}, err + } + remote := storage.NewRemoteRepository(mstore.NewIMAP(conf.IMAP())) + disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP())) + + fetchLatest, err := local.LatestSync() + if err != nil { + return &Sync{}, err + } + fetchInterval := 15 * time.Minute // not yet configurable + + return &Sync{ + fetcher: process.NewFetch(remote, local), + sender: process.NewSend(local, disp), + fetchInterval: fetchInterval, + fetchLatest: fetchLatest, + }, nil +} + +func (s *Sync) Do() string { + countSend, err := s.sender.Process() + if err != nil { + return format.FormatError(err) + } + + if time.Now().Before(s.fetchLatest.Add(s.fetchInterval)) { + return fmt.Sprintf("sent %d tasks, not time to fetch yet\n", countSend) + } + + fResult, err := s.fetcher.Process() + if err != nil { + return format.FormatError(err) + } + + return fmt.Sprintf("sent %d, fetched %d tasks\n", countSend, fResult.Count) +}