-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdone.go
155 lines (136 loc) · 4.05 KB
/
done.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package done
import (
"errors"
"io"
"strings"
"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/cmdcompl"
"github.com/lucassabreu/clockify-cli/pkg/cmdcomplutil"
"github.com/lucassabreu/clockify-cli/pkg/cmdutil"
"github.com/lucassabreu/clockify-cli/pkg/search"
"github.com/lucassabreu/clockify-cli/strhlp"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
// NewCmdDone represents the close command
func NewCmdDone(
f cmdutil.Factory,
report func(io.Writer, *util.OutputFlags, []dto.Task) error,
) *cobra.Command {
of := util.OutputFlags{}
cmd := &cobra.Command{
Use: "done <task>...",
Aliases: []string{"mark-as-done", "end"},
Args: cmdutil.RequiredNamedArgs("task"),
ValidArgsFunction: cmdcompl.CombineSuggestionsToArgs(
cmdcomplutil.NewTaskAutoComplete(f, true)),
Short: "Edits a task to done",
Long: "Edits a task to done, similar to doing `task edit <task> --done`",
Example: heredoc.Docf(`
$ %[1]s ls
+--------------------------+--------+--------+
| ID | NAME | STATUS |
+--------------------------+--------+--------+
| 62adfcc8c22de9759e739d66 | Five | ACTIVE |
| 62adfcc4c22de9759e739d64 | Four | ACTIVE |
| 62adfcb649445270d7becfca | Three | ACTIVE |
| 62adfcb149445270d7becfc8 | Second | ACTIVE |
| 62adfcaa4ebb4f143c92bf8b | First | ACTIVE |
+--------------------------+--------+--------+
$ %[1]s done first second 62adfcb649445270d7becfca
+--------------------------+--------+--------+
| ID | NAME | STATUS |
+--------------------------+--------+--------+
| 62adfcaa4ebb4f143c92bf8b | First | DONE |
| 62adfcb149445270d7becfc8 | Second | DONE |
| 62adfcb649445270d7becfca | Three | DONE |
+--------------------------+--------+--------+
$ %[1]s done four
id,name,status
62adfcc4c22de9759e739d64,Four,DONE
$ %[1]s done five
No active task with id or name containing 'five' was found
`, "clockify-cli task -p cli"),
RunE: func(cmd *cobra.Command, args []string) error {
if err := of.Check(); err != nil {
return err
}
project, _ := cmd.Flags().GetString("project")
project = strings.TrimSpace(project)
if project == "" {
return errors.New("project should not be empty")
}
ids := strhlp.Map(strings.TrimSpace, args)
if strhlp.Search("", ids) != -1 {
return errors.New("task id/name should not be empty")
}
workspace, err := f.GetWorkspaceID()
if err != nil {
return err
}
c, err := f.Client()
if err != nil {
return err
}
if f.Config().IsAllowNameForID() {
if project, err = search.GetProjectByName(
c, f.Config(), workspace, project, ""); err != nil {
return err
}
if ids, err = search.GetTasksByName(
c,
api.GetTasksParam{
Workspace: workspace,
ProjectID: project,
Active: true,
},
ids,
); err != nil {
var errNF search.ErrNotFound
if errors.As(err, &errNF) {
return errors.New(
"No active task with id or name containing '" +
errNF.Reference + "' was found")
}
return err
}
}
tasks := make([]dto.Task, len(ids))
var g errgroup.Group
for i := 0; i < len(ids); i++ {
j := i
g.Go(func() error {
t, err := c.GetTask(api.GetTaskParam{
Workspace: workspace,
ProjectID: project,
TaskID: ids[j],
})
if err != nil {
return err
}
tasks[j], err = c.UpdateTask(api.UpdateTaskParam{
Workspace: workspace,
ProjectID: t.ProjectID,
TaskID: t.ID,
Name: t.Name,
Status: api.TaskStatusDone,
})
return err
})
}
if err := g.Wait(); err != nil {
return err
}
if report == nil {
return util.TaskReport(cmd, of, tasks...)
}
return report(cmd.OutOrStdout(), &of, tasks)
},
}
cmdutil.AddProjectFlags(cmd, f)
util.TaskAddReportFlags(cmd, &of)
return cmd
}