planner/plan/command/projects.go

65 lines
1.4 KiB
Go
Raw Normal View History

2025-01-05 12:17:57 +01:00
package command
import (
"fmt"
"sort"
"go-mod.ewintr.nl/planner/plan/format"
2025-01-13 09:13:48 +01:00
"go-mod.ewintr.nl/planner/sync/client"
2025-01-05 12:17:57 +01:00
)
type ProjectsArgs struct{}
func NewProjectsArgs() ProjectsArgs {
return ProjectsArgs{}
}
func (pa ProjectsArgs) Parse(main []string, fields map[string]string) (Command, error) {
if len(main) != 1 || main[0] != "projects" {
return nil, ErrWrongCommand
}
return Projects{}, nil
}
type Projects struct{}
2025-01-13 09:13:48 +01:00
func (ps Projects) Do(repos Repositories, _ client.Client) (CommandResult, error) {
tx, err := repos.Begin()
if err != nil {
return nil, fmt.Errorf("could not start transaction: %v", err)
}
defer tx.Rollback()
projects, err := repos.Task(tx).Projects()
2025-01-05 12:17:57 +01:00
if err != nil {
return nil, fmt.Errorf("could not find projects: %v", err)
}
2025-01-13 09:13:48 +01:00
if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("could not list projects: %v", err)
}
2025-01-05 12:17:57 +01:00
return ProjectsResult{
Projects: projects,
}, nil
}
type ProjectsResult struct {
Projects map[string]int
}
func (psr ProjectsResult) Render() string {
projects := make([]string, 0, len(psr.Projects))
for pr := range psr.Projects {
projects = append(projects, pr)
}
sort.Strings(projects)
data := [][]string{{"projects", "count"}}
for _, p := range projects {
data = append(data, []string{p, fmt.Sprintf("%d", psr.Projects[p])})
}
return fmt.Sprintf("\n%s\n", format.Table(data))
}