planner/plan/command/list.go

86 lines
1.8 KiB
Go

package command
import (
"fmt"
"slices"
"time"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
)
type ListArgs struct {
params storage.TaskListParams
}
func NewListArgs() ListArgs {
return ListArgs{}
}
func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) {
if len(main) > 1 {
return nil, ErrWrongCommand
}
now := time.Now()
today := item.NewDate(now.Year(), int(now.Month()), now.Day())
tomorrow := item.NewDate(now.Year(), int(now.Month()), now.Day()+1)
var date item.Date
var includeBefore, recurrer bool
switch len(main) {
case 0:
date = today
includeBefore = true
case 1:
switch {
case slices.Contains([]string{"today", "tod"}, main[0]):
date = today
includeBefore = true
case slices.Contains([]string{"tomorrow", "tom"}, main[0]):
date = tomorrow
case main[0] == "list":
}
case 2:
if main[0] != "list" || main[1] != "recur" {
return nil, ErrWrongCommand
}
recurrer = true
}
return &List{
args: ListArgs{
params: storage.TaskListParams{
Date: date,
IncludeBefore: includeBefore,
Recurrer: recurrer,
},
},
}, nil
}
type List struct {
args ListArgs
}
func (list *List) Do(deps Dependencies) ([][]string, error) {
localIDs, err := deps.LocalIDRepo.FindAll()
if err != nil {
return nil, fmt.Errorf("could not get local ids: %v", err)
}
all, err := deps.TaskRepo.FindMany(list.args.params)
if err != nil {
return nil, err
}
data := [][]string{{"id", "date", "dur", "title"}}
for _, e := range all {
lid, ok := localIDs[e.ID]
if !ok {
return nil, fmt.Errorf("could not find local id for %s", e.ID)
}
data = append(data, []string{fmt.Sprintf("%d", lid), e.Date.String(), e.Duration.String(), e.Title})
}
return data, nil
}