Repository navigation
Expand file tree
/
Copy pathlist.go
More file actions
160 lines (129 loc) · 3.79 KB
/
Copy pathlist.go
File metadata and controls
160 lines (129 loc) · 3.79 KB
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
156
157
158
159
160
package cli
import (
"strconv"
"github.com/dominikbraun/timetrace/core"
"github.com/dominikbraun/timetrace/out"
"github.com/spf13/cobra"
)
func listCommand(t *core.Timetrace) *cobra.Command {
list := &cobra.Command{
Use: "list",
Short: "List all resources",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
}
list.AddCommand(listProjectsCommand(t))
list.AddCommand(listRecordsCommand(t))
return list
}
func listProjectsCommand(t *core.Timetrace) *cobra.Command {
listProjects := &cobra.Command{
Use: "projects",
Short: "List all projects",
Run: func(cmd *cobra.Command, args []string) {
allProjects, err := t.ListProjects()
if err != nil {
out.Err("Failed to list projects: %s", err.Error())
return
}
// remove all modules from the project list
parentProjects := removeModules(allProjects)
rows := make([][]string, len(parentProjects))
for i, project := range parentProjects {
allModules, err := t.ListProjectModules(project)
if err != nil {
out.Err("Failed to load project modules: %s", err.Error())
return
}
rows[i] = make([]string, 3)
rows[i][0] = strconv.Itoa(i + 1)
rows[i][1] = project.Key
rows[i][2] = allModules
}
out.Table([]string{"#", "Key", "Modules"}, rows)
},
}
return listProjects
}
type listRecordsOptions struct {
isOnlyDisplayingBillable bool
projectKeyFilter string
}
func listRecordsCommand(t *core.Timetrace) *cobra.Command {
var options listRecordsOptions
listRecords := &cobra.Command{
Use: "records {<YYYY-MM-DD>|today|yesterday}",
Short: "List all records from a date",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
date, err := t.Formatter().ParseDate(args[0])
if err != nil {
out.Err("failed to parse date: %s", err.Error())
return
}
records, err := t.ListRecords(date)
if err != nil {
out.Err("failed to list records: %s", err.Error())
return
}
if len(options.projectKeyFilter) > 0 {
records = filterProjectRecords(records, options.projectKeyFilter)
}
if options.isOnlyDisplayingBillable {
records = filterBillableRecords(records)
}
rows := make([][]string, len(records))
for i, record := range records {
end := defaultString
if record.End != nil {
end = t.Formatter().TimeString(*record.End)
}
billable := defaultBool
if record.IsBillable {
billable = "yes"
}
rows[i] = make([]string, 6)
rows[i][0] = strconv.Itoa(i + 1)
rows[i][1] = t.Formatter().RecordKey(record)
rows[i][2] = record.Project.Key
rows[i][3] = t.Formatter().TimeString(record.Start)
rows[i][4] = end
rows[i][5] = billable
}
out.Table([]string{"#", "Key", "Project", "Start", "End", "Billable"}, rows)
},
}
listRecords.Flags().BoolVarP(&options.isOnlyDisplayingBillable, "billable", "b",
false, `only display billable records`)
listRecords.Flags().StringVarP(&options.projectKeyFilter, "project", "p",
"", "filter by project key")
return listRecords
}
func filterBillableRecords(records []*core.Record) []*core.Record {
billableRecords := []*core.Record{}
for _, record := range records {
if record.IsBillable == true {
billableRecords = append(billableRecords, record)
}
}
return billableRecords
}
func filterProjectRecords(records []*core.Record, key string) []*core.Record {
projectRecords := []*core.Record{}
for _, record := range records {
if record.Project.Key == key || record.Project.Parent() == key {
projectRecords = append(projectRecords, record)
}
}
return projectRecords
}
func removeModules(allProjects []*core.Project) []*core.Project {
var parentProjects []*core.Project
for _, p := range allProjects {
if !p.IsModule() {
parentProjects = append(parentProjects, p)
}
}
return parentProjects
}