-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlist.go
92 lines (77 loc) · 2.34 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package list
import (
"io"
"github.com/MakeNowJust/heredoc"
"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/api/dto"
"github.com/lucassabreu/clockify-cli/pkg/cmd/task/util"
"github.com/lucassabreu/clockify-cli/pkg/cmdutil"
"github.com/lucassabreu/clockify-cli/pkg/search"
"github.com/spf13/cobra"
)
// NewCmdList represents the list command
func NewCmdList(
f cmdutil.Factory,
report func(io.Writer, *util.OutputFlags, []dto.Task) error,
) *cobra.Command {
of := util.OutputFlags{}
cmd := &cobra.Command{
Use: "list",
Short: "List tasks in a Clockify project",
Example: heredoc.Docf(`
$ %[1]s --project special
+--------------------------+----------+--------+
| ID | NAME | STATUS |
+--------------------------+----------+--------+
| 62aa4eed49445270d7b9666c | Inactive | DONE |
| 62aa4ee64ebb4f143c8d5225 | Second | ACTIVE |
| 62aa4ea2c22de9759e6e3a0e | First | ACTIVE |
+--------------------------+----------+--------+
$ %[1]s --project special --active --quiet
62aa4ee64ebb4f143c8d5225
62aa4ea2c22de9759e6e3a0e
$ %[1]s --project special --name inact --csv
id,name,status
62aa4eed49445270d7b9666c,Inactive,DONE
`, "clockify-cli task list"),
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
workspace, err := f.GetWorkspaceID()
if err != nil {
return err
}
c, err := f.Client()
if err != nil {
return err
}
p := api.GetTasksParam{
Workspace: workspace,
PaginationParam: api.AllPages(),
}
p.Active, _ = cmd.Flags().GetBool("active")
p.Name, _ = cmd.Flags().GetString("name")
p.ProjectID, _ = cmd.Flags().GetString("project")
if f.Config().IsAllowNameForID() &&
p.ProjectID != "" {
if p.ProjectID, err = search.GetProjectByName(
c, f.Config(), workspace, p.ProjectID, ""); err != nil {
return err
}
}
tasks, err := c.GetTasks(p)
if err != nil {
return err
}
if report == nil {
return util.TaskReport(cmd, of, tasks...)
}
return report(cmd.OutOrStdout(), &of, tasks)
},
}
cmd.Flags().StringP("name", "n", "",
"will be used to filter the tag by name")
cmd.Flags().BoolP("active", "a", false, "display only active tasks")
util.TaskAddReportFlags(cmd, &of)
cmdutil.AddProjectFlags(cmd, f)
return cmd
}