remove weird dependecy on local repository in commands

This commit is contained in:
Erik Winter 2021-08-20 10:01:35 +02:00
parent 33f0b43cdc
commit c035bf5cab
14 changed files with 21 additions and 103 deletions

View File

@ -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
}

View File

@ -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,

View File

@ -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)
}

View File

@ -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) {

View File

@ -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)
}

View File

@ -31,5 +31,5 @@ func (s *Show) Do() string {
return format.FormatError(err)
}
return format.FormatTask(s.id, t)
return format.FormatTask(t)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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,

View File

@ -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

View File

@ -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 {

View File

@ -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
}

View File

@ -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)
})
}

View File

@ -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{}