~ewintr/gte

99b8476cdf47bd7c2a3ecc024d2240edb60ee7fc — Erik Winter 3 months ago 86ad6cb
rename sync to fetch
4 files changed, 24 insertions(+), 24 deletions(-)

M cmd/cli/command/command.go
R cmd/cli/command/{sync.go => fetch.go}
R internal/process/{sync.go => fetch.go}
R internal/process/{sync_test.go => fetch_test.go}
M cmd/cli/command/command.go => cmd/cli/command/command.go +2 -2
@@ 31,8 31,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) {
	}

	switch cmd {
	case "sync":
		return NewSync(conf)
	case "fetch":
		return NewFetch(conf)
	case "today":
		return NewToday(conf)
	case "tomorrow":

R cmd/cli/command/sync.go => cmd/cli/command/fetch.go +10 -10
@@ 10,29 10,29 @@ import (
	"git.ewintr.nl/gte/pkg/mstore"
)

type Sync struct {
	syncer *process.Sync
type Fetch struct {
	fetcher *process.Fetch
}

func NewSync(conf *configuration.Configuration) (*Sync, error) {
func NewFetch(conf *configuration.Configuration) (*Fetch, error) {
	msgStore := mstore.NewIMAP(conf.IMAP())
	remote := storage.NewRemoteRepository(msgStore)
	local, err := storage.NewSqlite(conf.Sqlite())
	if err != nil {
		return &Sync{}, err
		return &Fetch{}, err
	}
	syncer := process.NewSync(remote, local)
	fetcher := process.NewFetch(remote, local)

	return &Sync{
		syncer: syncer,
	return &Fetch{
		fetcher: fetcher,
	}, nil
}

func (s *Sync) Do() string {
	result, err := s.syncer.Process()
func (s *Fetch) Do() string {
	result, err := s.fetcher.Process()
	if err != nil {
		return format.FormatError(err)
	}

	return fmt.Sprintf("synced %d tasks\n", result.Count)
	return fmt.Sprintf("fetched %d tasks\n", result.Count)
}

R internal/process/sync.go => internal/process/fetch.go +10 -10
@@ 10,28 10,28 @@ import (
)

var (
	ErrSyncProcess = errors.New("could not sync local repository")
	ErrFetchProcess = errors.New("could not fetch tasks")
)

// Sync fetches all tasks in regular folders from the remote repository and overwrites what is stored locally
type Sync struct {
// Fetch fetches all tasks in regular folders from the remote repository and overwrites what is stored locally
type Fetch struct {
	remote *storage.RemoteRepository
	local  storage.LocalRepository
}

type SyncResult struct {
type FetchResult struct {
	Duration string `json:"duration"`
	Count    int    `json:"count"`
}

func NewSync(remote *storage.RemoteRepository, local storage.LocalRepository) *Sync {
	return &Sync{
func NewFetch(remote *storage.RemoteRepository, local storage.LocalRepository) *Fetch {
	return &Fetch{
		remote: remote,
		local:  local,
	}
}

func (s *Sync) Process() (*SyncResult, error) {
func (s *Fetch) Process() (*FetchResult, error) {
	start := time.Now()

	tasks := []*task.Task{}


@@ 41,7 41,7 @@ func (s *Sync) Process() (*SyncResult, error) {
		}
		folderTasks, err := s.remote.FindAll(folder)
		if err != nil {
			return &SyncResult{}, fmt.Errorf("%w: %v", ErrSyncProcess, err)
			return &FetchResult{}, fmt.Errorf("%w: %v", ErrFetchProcess, err)
		}

		for _, t := range folderTasks {


@@ 50,10 50,10 @@ func (s *Sync) Process() (*SyncResult, error) {
	}

	if err := s.local.SetTasks(tasks); err != nil {
		return &SyncResult{}, fmt.Errorf("%w: %v", ErrSyncProcess, err)
		return &FetchResult{}, fmt.Errorf("%w: %v", ErrFetchProcess, err)
	}

	return &SyncResult{
	return &FetchResult{
		Duration: time.Since(start).String(),
		Count:    len(tasks),
	}, nil

R internal/process/sync_test.go => internal/process/fetch_test.go +2 -2
@@ 11,7 11,7 @@ import (
	"git.ewintr.nl/gte/pkg/mstore"
)

func TestSyncProcess(t *testing.T) {
func TestFetchProcess(t *testing.T) {
	task1 := &task.Task{
		Id:      "id1",
		Version: 1,


@@ 35,7 35,7 @@ func TestSyncProcess(t *testing.T) {
	remote := storage.NewRemoteRepository(mstorer)
	local := storage.NewMemory()

	syncer := process.NewSync(remote, local)
	syncer := process.NewFetch(remote, local)
	actResult, err := syncer.Process()
	test.OK(t, err)
	test.Equals(t, 2, actResult.Count)