package command import ( "fmt" "slices" "time" "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/format" "go-mod.ewintr.nl/planner/plan/storage" ) type ListArgs struct { fieldTPL map[string][]string HasRecurrer bool From item.Date To item.Date Project string } func NewListArgs() ListArgs { return ListArgs{ fieldTPL: map[string][]string{ "project": {"p", "project"}, "from": {"f", "from"}, "to": {"t", "to"}, "recurring": {"rec", "recurring"}, }, } } func (la ListArgs) Parse(main []string, fields map[string]string) (Command, error) { if len(main) > 2 { return nil, ErrWrongCommand } fields, err := ResolveFields(fields, la.fieldTPL) if err != nil { return nil, err } now := time.Now() today := item.NewDate(now.Year(), int(now.Month()), now.Day()) switch len(main) { case 0: fields["to"] = today.String() case 1: switch { case slices.Contains([]string{"tod", "today"}, main[0]): fields["to"] = today.String() case slices.Contains([]string{"tom", "tomorrow"}, main[0]): fields["from"] = today.Add(1).String() fields["to"] = today.Add(1).String() case main[0] == "week": fields["from"] = today.String() fields["to"] = today.Add(7).String() case main[0] == "recur": fields["recurring"] = "true" case main[0] == "list": fields["from"] = today.String() fields["to"] = today.String() default: return nil, ErrWrongCommand } } var fromDate, toDate item.Date var hasRecurrer bool var project string if val, ok := fields["from"]; ok { fromDate = item.NewDateFromString(val) } if val, ok := fields["to"]; ok { toDate = item.NewDateFromString(val) } if val, ok := fields["recurrer"]; ok && val == "true" { hasRecurrer = true } if val, ok := fields["project"]; ok { project = val } return List{ Args: ListArgs{ HasRecurrer: hasRecurrer, From: fromDate, To: toDate, Project: project, }, }, nil } type List struct { Args ListArgs } func (list List) Do(deps Dependencies) (CommandResult, 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(storage.TaskListParams{ HasRecurrer: list.Args.HasRecurrer, From: list.Args.From, To: list.Args.To, Project: list.Args.Project, }) if err != nil { return nil, err } res := make([]TaskWithLID, 0, len(all)) for _, tsk := range all { lid, ok := localIDs[tsk.ID] if !ok { return nil, fmt.Errorf("could not find local id for %s", tsk.ID) } res = append(res, TaskWithLID{ LocalID: lid, Task: tsk, }) } return ListResult{ Tasks: res, }, nil } type TaskWithLID struct { LocalID int Task item.Task } type ListResult struct { Tasks []TaskWithLID } func (lr ListResult) Render() string { data := [][]string{{"id", "project", "date", "dur", "title"}} for _, tl := range lr.Tasks { data = append(data, []string{ fmt.Sprintf("%d", tl.LocalID), tl.Task.Project, tl.Task.Date.String(), tl.Task.Duration.String(), tl.Task.Title, }) } return fmt.Sprintf("\n%s\n", format.Table(data)) }