From 516f0923f5554b9016b4d827929b0b78648d0f75 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Fri, 3 Sep 2021 09:31:41 +0200 Subject: [PATCH] send command --- cmd/cli/command/command.go | 2 ++ cmd/cli/command/send.go | 36 +++++++++++++++++++++++++++++++++++ internal/process/send.go | 13 ++++++++----- internal/process/send_test.go | 8 ++++++-- 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 cmd/cli/command/send.go diff --git a/cmd/cli/command/command.go b/cmd/cli/command/command.go index 45b6344..90d74fd 100644 --- a/cmd/cli/command/command.go +++ b/cmd/cli/command/command.go @@ -33,6 +33,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) { switch cmd { case "fetch": return NewFetch(conf) + case "send": + return NewSend(conf) case "today": return NewToday(conf) case "tomorrow": diff --git a/cmd/cli/command/send.go b/cmd/cli/command/send.go new file mode 100644 index 0000000..c8bba70 --- /dev/null +++ b/cmd/cli/command/send.go @@ -0,0 +1,36 @@ +package command + +import ( + "fmt" + + "git.ewintr.nl/gte/cmd/cli/format" + "git.ewintr.nl/gte/internal/configuration" + "git.ewintr.nl/gte/internal/process" + "git.ewintr.nl/gte/internal/storage" + "git.ewintr.nl/gte/pkg/msend" +) + +type Send struct { + sender *process.Send +} + +func NewSend(conf *configuration.Configuration) (*Send, error) { + local, err := storage.NewSqlite(conf.Sqlite()) + if err != nil { + return &Send{}, err + } + disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP())) + + return &Send{ + sender: process.NewSend(local, disp), + }, nil +} + +func (s *Send) Do() string { + count, err := s.sender.Process() + if err != nil { + return format.FormatError(err) + } + + return fmt.Sprintf("sent %d tasks\n", count) +} diff --git a/internal/process/send.go b/internal/process/send.go index 356f298..ebc2e6e 100644 --- a/internal/process/send.go +++ b/internal/process/send.go @@ -25,12 +25,13 @@ func NewSend(local storage.LocalRepository, disp *storage.Dispatcher) *Send { } } -func (s *Send) Process() error { +func (s *Send) Process() (int, error) { tasks, err := s.local.FindAll() if err != nil { - return fmt.Errorf("%w: %v", ErrSendTasks, err) + return 0, fmt.Errorf("%w: %v", ErrSendTasks, err) } + var count int for _, t := range tasks { if t.LocalStatus != task.STATUS_UPDATED { continue @@ -38,12 +39,14 @@ func (s *Send) Process() error { t.ApplyUpdate() if err := s.disp.Dispatch(&t.Task); err != nil { - return fmt.Errorf("%w: %v", ErrSendTasks, err) + return 0, fmt.Errorf("%w: %v", ErrSendTasks, err) } if err := s.local.MarkDispatched(t.LocalId); err != nil { - return fmt.Errorf("%w: %v", ErrSendTasks, err) + return 0, fmt.Errorf("%w: %v", ErrSendTasks, err) } + + count++ } - return nil + return count, nil } diff --git a/internal/process/send_test.go b/internal/process/send_test.go index 37a5ea0..f41ad11 100644 --- a/internal/process/send_test.go +++ b/internal/process/send_test.go @@ -35,7 +35,9 @@ func TestSend(t *testing.T) { out := msend.NewMemory() disp := storage.NewDispatcher(out) send := process.NewSend(local, disp) - test.OK(t, send.Process()) + res, err := send.Process() + test.OK(t, err) + test.Equals(t, 0, res) test.Assert(t, len(out.Messages) == 0, "amount of messages was not 0") }) @@ -53,7 +55,9 @@ func TestSend(t *testing.T) { out := msend.NewMemory() disp := storage.NewDispatcher(out) send := process.NewSend(local, disp) - test.OK(t, send.Process()) + res, err := send.Process() + test.OK(t, err) + test.Equals(t, 1, res) test.Assert(t, len(out.Messages) == 1, "amount of messages was not 1") expSubject := "project1 - updated" test.Equals(t, expSubject, out.Messages[0].Subject)