diff --git a/cmd/cli/command/command.go b/cmd/cli/command/command.go index c78a048..7bf1f84 100644 --- a/cmd/cli/command/command.go +++ b/cmd/cli/command/command.go @@ -2,11 +2,9 @@ package command import ( "errors" - "fmt" "strconv" "git.ewintr.nl/gte/internal/configuration" - "git.ewintr.nl/gte/internal/storage" ) var ( @@ -67,22 +65,3 @@ func parseTaskCommand(id int, tArgs []string, conf *configuration.Configuration) return NewShow(id, conf) } } - -func findId(id int, local storage.LocalRepository) (string, error) { - localIds, err := local.LocalIds() - if err != nil { - return "", fmt.Errorf("%w: %v", ErrCouldNotFindTask, err) - } - var tId string - for remoteId, localId := range localIds { - if localId == id { - tId = remoteId - break - } - } - if tId == "" { - return "", ErrCouldNotFindTask - } - - return tId, nil -} diff --git a/cmd/cli/command/done.go b/cmd/cli/command/done.go index 34275b7..8fe77ba 100644 --- a/cmd/cli/command/done.go +++ b/cmd/cli/command/done.go @@ -23,12 +23,12 @@ func NewDone(localId int, conf *configuration.Configuration) (*Done, error) { disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP())) fields := process.UpdateFields{"done": "true"} - tId, err := findId(localId, local) + localTask, err := local.FindByLocalId(localId) if err != nil { return &Done{}, err } - updater := process.NewUpdate(local, disp, tId, fields) + updater := process.NewUpdate(local, disp, localTask.Id, fields) return &Done{ doner: updater, diff --git a/cmd/cli/command/folder.go b/cmd/cli/command/folder.go index 55bf0e8..389390b 100644 --- a/cmd/cli/command/folder.go +++ b/cmd/cli/command/folder.go @@ -11,7 +11,6 @@ import ( ) type Folder struct { - local storage.LocalRepository lister *process.List } @@ -43,7 +42,6 @@ func NewFolder(conf *configuration.Configuration, cmdArgs []string) (*Folder, er lister := process.NewList(local, reqs) return &Folder{ - local: local, lister: lister, }, nil } @@ -58,5 +56,5 @@ func (f *Folder) Do() string { return "no tasks here\n" } - return format.FormatTaskTable(f.local, res.Tasks) + return format.FormatTaskTable(res.Tasks) } diff --git a/cmd/cli/command/list.go b/cmd/cli/command/list.go index 60f7796..2fada63 100644 --- a/cmd/cli/command/list.go +++ b/cmd/cli/command/list.go @@ -17,7 +17,6 @@ var ( // List lists all the tasks in a project or a folder type List struct { - local storage.LocalRepository lister *process.List } @@ -39,7 +38,6 @@ func NewList(conf *configuration.Configuration, cmdArgs []string) (*List, error) lister := process.NewList(local, reqs) return &List{ - local: local, lister: lister, }, nil } @@ -50,11 +48,7 @@ func (l *List) Do() string { return format.FormatError(err) } - if len(res.Tasks) == 0 { - return "no tasks there\n" - } - - return format.FormatTaskTable(l.local, res.Tasks) + return format.FormatTaskTable(res.Tasks) } func parseReqs(kind, item string) (process.ListReqs, error) { diff --git a/cmd/cli/command/project.go b/cmd/cli/command/project.go index 06bd967..5727857 100644 --- a/cmd/cli/command/project.go +++ b/cmd/cli/command/project.go @@ -10,7 +10,6 @@ import ( ) type Project struct { - local storage.LocalRepository lister *process.List } @@ -30,7 +29,6 @@ func NewProject(conf *configuration.Configuration, cmdArgs []string) (*Project, lister := process.NewList(local, reqs) return &Project{ - local: local, lister: lister, }, nil } @@ -45,5 +43,5 @@ func (p *Project) Do() string { return "no tasks here\n" } - return format.FormatTaskTable(p.local, res.Tasks) + return format.FormatTaskTable(res.Tasks) } diff --git a/cmd/cli/command/show.go b/cmd/cli/command/show.go index c9e8038..55d5a6b 100644 --- a/cmd/cli/command/show.go +++ b/cmd/cli/command/show.go @@ -31,5 +31,5 @@ func (s *Show) Do() string { return format.FormatError(err) } - return format.FormatTask(s.id, t) + return format.FormatTask(t) } diff --git a/cmd/cli/command/today.go b/cmd/cli/command/today.go index 9763c02..5316726 100644 --- a/cmd/cli/command/today.go +++ b/cmd/cli/command/today.go @@ -10,8 +10,7 @@ import ( // Today lists all task that are due today or past their due date type Today struct { - local storage.LocalRepository - todayer *process.List + lister *process.List } func (t *Today) Cmd() string { return "today" } @@ -25,22 +24,18 @@ func NewToday(conf *configuration.Configuration) (*Today, error) { Due: task.Today, IncludeBefore: true, } - todayer := process.NewList(local, reqs) + lister := process.NewList(local, reqs) return &Today{ - local: local, - todayer: todayer, + lister: lister, }, nil } func (t *Today) Do() string { - res, err := t.todayer.Process() + res, err := t.lister.Process() if err != nil { return format.FormatError(err) } - if len(res.Tasks) == 0 { - return "nothing left\n" - } - return format.FormatTaskTable(t.local, res.Tasks) + return format.FormatTaskTable(res.Tasks) } diff --git a/cmd/cli/command/tomorrow.go b/cmd/cli/command/tomorrow.go index d68f545..8725532 100644 --- a/cmd/cli/command/tomorrow.go +++ b/cmd/cli/command/tomorrow.go @@ -10,8 +10,7 @@ import ( // Tomorrow lists all tasks that are due tomorrow type Tomorrow struct { - local storage.LocalRepository - tomorrower *process.List + lister *process.List } func (t *Tomorrow) Cmd() string { return "tomorrow" } @@ -25,16 +24,15 @@ func NewTomorrow(conf *configuration.Configuration) (*Tomorrow, error) { reqs := process.ListReqs{ Due: task.Today.Add(1), } - tomorrower := process.NewList(local, reqs) + lister := process.NewList(local, reqs) return &Tomorrow{ - local: local, - tomorrower: tomorrower, + lister: lister, }, nil } func (t *Tomorrow) Do() string { - res, err := t.tomorrower.Process() + res, err := t.lister.Process() if err != nil { return format.FormatError(err) } @@ -43,5 +41,5 @@ func (t *Tomorrow) Do() string { return "nothing to do tomorrow\n" } - return format.FormatTaskTable(t.local, res.Tasks) + return format.FormatTaskTable(res.Tasks) } diff --git a/cmd/cli/command/update.go b/cmd/cli/command/update.go index 9471c7e..2114208 100644 --- a/cmd/cli/command/update.go +++ b/cmd/cli/command/update.go @@ -34,12 +34,12 @@ func NewUpdate(localId int, conf *configuration.Configuration, cmdArgs []string) if err != nil { return &Update{}, err } - tId, err := findId(localId, local) + localTask, err := local.FindByLocalId(localId) if err != nil { return &Update{}, err } - updater := process.NewUpdate(local, disp, tId, fields) + updater := process.NewUpdate(local, disp, localTask.Id, fields) return &Update{ updater: updater, diff --git a/cmd/cli/format/format.go b/cmd/cli/format/format.go index 8c35d84..e069af5 100644 --- a/cmd/cli/format/format.go +++ b/cmd/cli/format/format.go @@ -3,7 +3,6 @@ package format import ( "fmt" - "git.ewintr.nl/gte/internal/storage" "git.ewintr.nl/gte/internal/task" ) @@ -11,25 +10,20 @@ func FormatError(err error) string { return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error()) } -func FormatTaskTable(local storage.LocalRepository, tasks []*task.LocalTask) string { +func FormatTaskTable(tasks []*task.LocalTask) string { if len(tasks) == 0 { return "no tasks to display\n" } - localIds, err := local.LocalIds() - if err != nil { - return FormatError(err) - } - var output string for _, t := range tasks { - output += fmt.Sprintf("%d\t%s\t%s\n", localIds[t.Id], t.Due.String(), t.Action) + output += fmt.Sprintf("%d\t%s\t%s\n", t.LocalId, t.Due.String(), t.Action) } return output } -func FormatTask(id int, t *task.LocalTask) string { +func FormatTask(t *task.LocalTask) string { output := fmt.Sprintf(`folder: %s action: %s project: %s diff --git a/internal/storage/local.go b/internal/storage/local.go index e79455c..4495773 100644 --- a/internal/storage/local.go +++ b/internal/storage/local.go @@ -19,7 +19,6 @@ type LocalRepository interface { FindAllInProject(project string) ([]*task.LocalTask, error) FindById(id string) (*task.LocalTask, error) FindByLocalId(id int) (*task.LocalTask, error) - LocalIds() (map[string]int, error) } func NextLocalId(used []int) int { diff --git a/internal/storage/memory.go b/internal/storage/memory.go index 2dbf4b4..3bdd56f 100644 --- a/internal/storage/memory.go +++ b/internal/storage/memory.go @@ -101,7 +101,3 @@ func (m *Memory) FindByLocalId(localId int) (*task.LocalTask, error) { return &task.LocalTask{}, ErrTaskNotFound } - -func (m *Memory) LocalIds() (map[string]int, error) { - return m.localIds, nil -} diff --git a/internal/storage/memory_test.go b/internal/storage/memory_test.go index 8ee6de5..42d0c07 100644 --- a/internal/storage/memory_test.go +++ b/internal/storage/memory_test.go @@ -97,17 +97,4 @@ func TestMemory(t *testing.T) { test.OK(t, err) test.Equals(t, localTask2, act) }) - - t.Run("localids", func(t *testing.T) { - mem := storage.NewMemory() - test.OK(t, mem.SetTasks(tasks)) - act, err := mem.LocalIds() - test.OK(t, err) - exp := map[string]int{ - "id-1": 1, - "id-2": 2, - "id-3": 3, - } - test.Equals(t, exp, act) - }) } diff --git a/internal/storage/sqlite.go b/internal/storage/sqlite.go index 480ab71..85629e9 100644 --- a/internal/storage/sqlite.go +++ b/internal/storage/sqlite.go @@ -219,26 +219,6 @@ func (s *Sqlite) FindByLocalId(localId int) (*task.LocalTask, error) { return t, nil } -func (s *Sqlite) LocalIds() (map[string]int, error) { - rows, err := s.db.Query(`SELECT id, local_id FROM local_task`) - if err != nil { - return map[string]int{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) - } - - idMap := map[string]int{} - defer rows.Close() - for rows.Next() { - var id string - var local_id int - if err := rows.Scan(&id, &local_id); err != nil { - return map[string]int{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) - } - idMap[id] = local_id - } - - return idMap, nil -} - func tasksFromRows(rows *sql.Rows) ([]*task.LocalTask, error) { tasks := []*task.LocalTask{}