diff --git a/cmd/cli/command/command.go b/cmd/cli/command/command.go index 4684f95..4833185 100644 --- a/cmd/cli/command/command.go +++ b/cmd/cli/command/command.go @@ -23,7 +23,6 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) { if len(args) == 0 { return NewEmpty() } - cmd, cmdArgs := args[0], args[1:] id, err := strconv.Atoi(cmd) @@ -38,6 +37,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) { return NewToday(conf) case "tomorrow": return NewTomorrow(conf) + case "list": + return NewList(conf, cmdArgs) case "add": return NewAdd(conf, cmdArgs) default: diff --git a/cmd/cli/command/list.go b/cmd/cli/command/list.go new file mode 100644 index 0000000..fa89ebe --- /dev/null +++ b/cmd/cli/command/list.go @@ -0,0 +1,81 @@ +package command + +import ( + "errors" + "fmt" + "strings" + + "git.ewintr.nl/gte/cmd/cli/format" + "git.ewintr.nl/gte/internal/configuration" + "git.ewintr.nl/gte/internal/process" + "git.ewintr.nl/gte/internal/storage" + "git.ewintr.nl/gte/internal/task" +) + +var ( + ErrUnknownFolder = errors.New("unknown folder") +) + +// List lists all the tasks in a project or a folder +type List struct { + local storage.LocalRepository + lister *process.List +} + +func (l *List) Cmd() string { return "list" } + +func NewList(conf *configuration.Configuration, cmdArgs []string) (*List, error) { + local, err := storage.NewSqlite(conf.Sqlite()) + if err != nil { + return &List{}, err + } + fmt.Printf("args: %+v\n", cmdArgs) + if len(cmdArgs) < 2 { + return &List{}, ErrInvalidAmountOfArgs + } + + reqs, err := parseReqs(cmdArgs[0], cmdArgs[1]) + if err != nil { + return &List{}, err + } + lister := process.NewList(local, reqs) + + return &List{ + local: local, + lister: lister, + }, nil +} + +func (l *List) Do() string { + res, err := l.lister.Process() + if err != nil { + return format.FormatError(err) + } + + if len(res.Tasks) == 0 { + return "no tasks there\n" + } + + return format.FormatTaskTable(l.local, res.Tasks) +} + +func parseReqs(kind, item string) (process.ListReqs, error) { + item = strings.ToLower(item) + switch kind { + case "folder": + for _, folder := range task.KnownFolders { + if item == strings.ToLower(folder) { + return process.ListReqs{ + Folder: folder, + }, nil + } + } + return process.ListReqs{}, ErrUnknownFolder + case "project": + return process.ListReqs{ + Project: item, + }, nil + } + + return process.ListReqs{}, process.ErrInvalidReqs +} diff --git a/cmd/cli/format/format.go b/cmd/cli/format/format.go index 064bf80..383e07c 100644 --- a/cmd/cli/format/format.go +++ b/cmd/cli/format/format.go @@ -30,10 +30,11 @@ func FormatTaskTable(local storage.LocalRepository, tasks []*task.Task) string { } func FormatTask(id int, t *task.Task) string { - output := fmt.Sprintf(`action: %s + output := fmt.Sprintf(`folder: %s +action: %s project: %s due: %s -`, t.Action, t.Project, t.Due.String()) +`, t.Folder, t.Action, t.Project, t.Due.String()) if t.IsRecurrer() { output += fmt.Sprintf("recur:%s", t.Recur.String()) }