project list
This commit is contained in:
parent
8cc7e888ed
commit
a4b0f89901
|
@ -37,6 +37,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) {
|
|||
return NewToday(conf)
|
||||
case "tomorrow":
|
||||
return NewTomorrow(conf)
|
||||
case "projects":
|
||||
return NewProjects(conf)
|
||||
case "list":
|
||||
return NewList(conf, cmdArgs)
|
||||
case "add":
|
||||
|
|
|
@ -2,7 +2,6 @@ package command
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.ewintr.nl/gte/cmd/cli/format"
|
||||
|
@ -29,7 +28,6 @@ func NewList(conf *configuration.Configuration, cmdArgs []string) (*List, error)
|
|||
if err != nil {
|
||||
return &List{}, err
|
||||
}
|
||||
fmt.Printf("args: %+v\n", cmdArgs)
|
||||
if len(cmdArgs) < 2 {
|
||||
return &List{}, ErrInvalidAmountOfArgs
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type Projects struct {
|
||||
local storage.LocalRepository
|
||||
projecter *process.Projects
|
||||
}
|
||||
|
||||
func (p *Projects) Cmd() string { return "projects" }
|
||||
|
||||
func NewProjects(conf *configuration.Configuration) (*Projects, error) {
|
||||
local, err := storage.NewSqlite(conf.Sqlite())
|
||||
if err != nil {
|
||||
return &Projects{}, err
|
||||
}
|
||||
|
||||
projecter := process.NewProjects(local)
|
||||
|
||||
return &Projects{
|
||||
local: local,
|
||||
projecter: projecter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Projects) Do() string {
|
||||
projects, err := p.projecter.Process()
|
||||
if err != nil {
|
||||
return format.FormatError(err)
|
||||
}
|
||||
|
||||
if len(projects) == 0 {
|
||||
return "no projects here\n"
|
||||
}
|
||||
|
||||
var out string
|
||||
for _, project := range projects {
|
||||
if project != "" {
|
||||
out += fmt.Sprintf("%s\n", project)
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package process
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"git.ewintr.nl/gte/internal/storage"
|
||||
"git.ewintr.nl/gte/internal/task"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCouldNotFetchProjects = errors.New("could not fetch projects")
|
||||
)
|
||||
|
||||
type Projects struct {
|
||||
local storage.LocalRepository
|
||||
}
|
||||
|
||||
func NewProjects(local storage.LocalRepository) *Projects {
|
||||
return &Projects{
|
||||
local: local,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Projects) Process() ([]string, error) {
|
||||
allTasks := []*task.Task{}
|
||||
for _, folder := range []string{task.FOLDER_NEW, task.FOLDER_PLANNED, task.FOLDER_UNPLANNED} {
|
||||
folderTasks, err := p.local.FindAllInFolder(folder)
|
||||
if err != nil {
|
||||
return []string{}, fmt.Errorf("%w: %v", ErrCouldNotFetchProjects, err)
|
||||
}
|
||||
allTasks = append(allTasks, folderTasks...)
|
||||
}
|
||||
|
||||
knownMap := map[string]bool{}
|
||||
for _, t := range allTasks {
|
||||
knownMap[t.Project] = true
|
||||
}
|
||||
known := []string{}
|
||||
for p := range knownMap {
|
||||
known = append(known, p)
|
||||
}
|
||||
sort.Strings(known)
|
||||
|
||||
return known, nil
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package process_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.ewintr.nl/go-kit/test"
|
||||
"git.ewintr.nl/gte/internal/process"
|
||||
"git.ewintr.nl/gte/internal/storage"
|
||||
"git.ewintr.nl/gte/internal/task"
|
||||
)
|
||||
|
||||
func TestProjects(t *testing.T) {
|
||||
project1, project2, project3 := "project-1", "project-2", "project-3"
|
||||
|
||||
task1 := &task.Task{
|
||||
Id: "id1",
|
||||
Version: 1,
|
||||
Action: "action1",
|
||||
Folder: task.FOLDER_NEW,
|
||||
Project: project1,
|
||||
}
|
||||
task2 := &task.Task{
|
||||
Id: "id2",
|
||||
Version: 1,
|
||||
Action: "action2",
|
||||
Due: task.NewDate(2021, 8, 19),
|
||||
Folder: task.FOLDER_PLANNED,
|
||||
Project: project2,
|
||||
}
|
||||
task3 := &task.Task{
|
||||
Id: "id3",
|
||||
Version: 1,
|
||||
Action: "action3",
|
||||
Due: task.NewDate(2021, 8, 18),
|
||||
Folder: task.FOLDER_PLANNED,
|
||||
Project: project2,
|
||||
}
|
||||
task4 := &task.Task{
|
||||
Id: "id4",
|
||||
Version: 1,
|
||||
Action: "action4",
|
||||
Due: task.NewDate(2021, 8, 17),
|
||||
Folder: task.FOLDER_UNPLANNED,
|
||||
Project: project3,
|
||||
}
|
||||
allTasks := []*task.Task{task1, task2, task3, task4}
|
||||
|
||||
local := storage.NewMemory()
|
||||
test.OK(t, local.SetTasks(allTasks))
|
||||
|
||||
t.Run("all", func(t *testing.T) {
|
||||
exp := []string{project1, project2, project3}
|
||||
list := process.NewProjects(local)
|
||||
act, err := list.Process()
|
||||
test.OK(t, err)
|
||||
test.Equals(t, exp, act)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue