~ewintr/gte

516f0923f5554b9016b4d827929b0b78648d0f75 — Erik Winter 3 months ago a296c25
send command
M cmd/cli/command/command.go => cmd/cli/command/command.go +2 -0
@@ 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":

A cmd/cli/command/send.go => cmd/cli/command/send.go +36 -0
@@ 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)
}

M internal/process/send.go => internal/process/send.go +8 -5
@@ 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
}

M internal/process/send_test.go => internal/process/send_test.go +6 -2
@@ 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)