5
5
"fmt"
6
6
"strconv"
7
7
"strings"
8
+ "time"
8
9
9
10
"github.com/spf13/cobra"
10
11
@@ -13,7 +14,7 @@ import (
13
14
"go.octolab.org/toolset/maintainer/internal/config"
14
15
"go.octolab.org/toolset/maintainer/internal/model/github/contribution"
15
16
"go.octolab.org/toolset/maintainer/internal/pkg/http"
16
- "go.octolab.org/toolset/maintainer/internal/pkg/time"
17
+ xtime "go.octolab.org/toolset/maintainer/internal/pkg/time"
17
18
"go.octolab.org/toolset/maintainer/internal/pkg/unsafe"
18
19
"go.octolab.org/toolset/maintainer/internal/service/github"
19
20
)
@@ -66,7 +67,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
66
67
RunE : func (cmd * cobra.Command , args []string ) error {
67
68
// dependencies and defaults
68
69
service := github .New (http .TokenSourcedClient (cmd .Context (), cnf .Token ))
69
- construct , date := time .RangeByWeeks , time .Now ().UTC ()
70
+ construct , date := xtime .RangeByWeeks , time .Now ().UTC ()
70
71
zero := unsafe .ReturnBool (cmd .Flags ().GetBool ("with-zero" ))
71
72
72
73
// input validation: date(year,+month,+week{day})
@@ -80,14 +81,14 @@ func Contribution(cnf *config.Tool) *cobra.Command {
80
81
}
81
82
82
83
switch input := args [0 ]; len (input ) {
83
- case len (time .RFC3339Year ):
84
- date , err = time .Parse (time .RFC3339Year , input )
85
- construct = time .RangeByYears
86
- case len (time .RFC3339Month ):
87
- date , err = time .Parse (time .RFC3339Month , input )
88
- construct = time .RangeByMonths
89
- case len (time .RFC3339Day ):
90
- date , err = time .Parse (time .RFC3339Day , input )
84
+ case len (xtime .RFC3339Year ):
85
+ date , err = time .Parse (xtime .RFC3339Year , input )
86
+ construct = xtime .RangeByYears
87
+ case len (xtime .RFC3339Month ):
88
+ date , err = time .Parse (xtime .RFC3339Month , input )
89
+ construct = xtime .RangeByMonths
90
+ case len (xtime .RFC3339Day ):
91
+ date , err = time .Parse (xtime .RFC3339Day , input )
91
92
default :
92
93
err = fmt .Errorf ("unsupported format" )
93
94
}
@@ -170,7 +171,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
170
171
fallthrough
171
172
case 1 :
172
173
if raw [0 ] != "now" && raw [0 ] != "" {
173
- date , err = time .Parse (time .RFC3339Day , raw [0 ])
174
+ date , err = time .Parse (xtime .RFC3339Day , raw [0 ])
174
175
}
175
176
if err != nil {
176
177
return wrap (err )
@@ -181,7 +182,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
181
182
}
182
183
183
184
// data provisioning
184
- scope := time .RangeByWeeks (date , weeks , half ).Shift (- time .Day ).ExcludeFuture ()
185
+ scope := xtime .RangeByWeeks (date , weeks , half ).Shift (- xtime .Day ).ExcludeFuture ()
185
186
chm , err := service .ContributionHeatMap (cmd .Context (), scope )
186
187
if err != nil {
187
188
return err
@@ -209,7 +210,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
209
210
RunE : func (cmd * cobra.Command , args []string ) error {
210
211
// dependencies and defaults
211
212
service := github .New (http .TokenSourcedClient (cmd .Context (), cnf .Token ))
212
- date := time .TruncateToYear (time .Now ().UTC ())
213
+ date := xtime .TruncateToYear (time .Now ().UTC ())
213
214
214
215
// input validation: date(year)
215
216
if len (args ) == 1 {
@@ -222,8 +223,8 @@ func Contribution(cnf *config.Tool) *cobra.Command {
222
223
}
223
224
224
225
switch input := args [0 ]; len (input ) {
225
- case len (time .RFC3339Year ):
226
- date , err = time .Parse (time .RFC3339Year , input )
226
+ case len (xtime .RFC3339Year ):
227
+ date , err = time .Parse (xtime .RFC3339Year , input )
227
228
default :
228
229
err = fmt .Errorf ("unsupported format" )
229
230
}
@@ -233,7 +234,7 @@ func Contribution(cnf *config.Tool) *cobra.Command {
233
234
}
234
235
235
236
// data provisioning
236
- scope := time .RangeByYears (date , 0 , false ).ExcludeFuture ()
237
+ scope := xtime .RangeByYears (date , 0 , false ).ExcludeFuture ()
237
238
chm , err := service .ContributionHeatMap (cmd .Context (), scope )
238
239
if err != nil {
239
240
return err
@@ -267,85 +268,11 @@ func Contribution(cnf *config.Tool) *cobra.Command {
267
268
suggest := cobra.Command {
268
269
Use : "suggest" ,
269
270
Args : cobra .MaximumNArgs (1 ),
270
- RunE : func (cmd * cobra.Command , args []string ) error {
271
- // dependencies and defaults
272
- service := github .New (http .TokenSourcedClient (cmd .Context (), cnf .Token ))
273
- date , weeks , half := time .TruncateToYear (time .Now ().UTC ()), 5 , false
274
- delta := unsafe .ReturnBool (cmd .Flags ().GetBool ("delta" ))
275
- short := unsafe .ReturnBool (cmd .Flags ().GetBool ("short" ))
276
- target := unsafe .ReturnUint (cmd .Flags ().GetUint ("target" ))
277
-
278
- // input validation: date(year,+month,+week{day})[/{+-}weeks]
279
- if len (args ) == 1 {
280
- var err error
281
- wrap := func (err error ) error {
282
- return fmt .Errorf (
283
- "please provide argument in format YYYY[-mm[-dd]][/[+|-]weeks], e.g., 2006-01: %w" ,
284
- fmt .Errorf ("invalid argument %q: %w" , args [0 ], err ),
285
- )
286
- }
287
-
288
- input := args [0 ]
289
- raw := strings .Split (input , "/" )
290
- switch len (raw ) {
291
- case 2 :
292
- weeks , err = strconv .Atoi (raw [1 ])
293
- if err != nil {
294
- return wrap (err )
295
- }
296
- // +%d and positive %d have the same value, but different semantic
297
- // invariant: len(raw[1]) > 0, because weeks > 0 and invariant(time.RangeByWeeks)
298
- if weeks > 0 && raw [1 ][0 ] != '+' {
299
- half = true
300
- }
301
- fallthrough
302
- case 1 :
303
- input = raw [0 ]
304
- default :
305
- return wrap (fmt .Errorf ("too many parts" ))
306
- }
307
-
308
- switch len (input ) {
309
- case len (time .RFC3339Year ):
310
- date , err = time .Parse (time .RFC3339Year , input )
311
- case len (time .RFC3339Month ):
312
- date , err = time .Parse (time .RFC3339Month , input )
313
- case len (time .RFC3339Day ):
314
- date , err = time .Parse (time .RFC3339Day , input )
315
- default :
316
- err = fmt .Errorf ("unsupported format" )
317
- }
318
- if err != nil {
319
- return wrap (err )
320
- }
321
- }
322
-
323
- // data provisioning
324
- scope := time .NewRange (
325
- time .RangeByWeeks (date , weeks , half ).From (),
326
- time .Now ().UTC (),
327
- )
328
- chm , err := service .ContributionHeatMap (cmd .Context (), scope )
329
- if err != nil {
330
- return err
331
- }
332
-
333
- value := contribution .Suggest (chm , date , scope .To (), target )
334
- area := time .RangeByWeeks (value .Day , weeks , half ).Shift (- time .Day ) // start from prev Sunday
335
- data := contribution .HistogramByWeekday (chm .Subset (area ), false )
336
-
337
- // data presentation
338
- return view .Suggest (cmd , area , data , view.SuggestOption {
339
- Suggest : value ,
340
- Current : chm [value .Day ],
341
- Delta : delta ,
342
- Short : short ,
343
- })
344
- },
271
+ RunE : exec .Contribution (cnf ),
345
272
}
346
273
suggest .Flags ().Bool ("delta" , false , "shows relatively" )
347
274
suggest .Flags ().Bool ("short" , false , "shows only date" )
348
- suggest .Flags ().Int ("target" , 5 , "minimum contributions" )
275
+ suggest .Flags ().Uint ("target" , 5 , "minimum contributions" )
349
276
cmd .AddCommand (& suggest )
350
277
351
278
return & cmd
0 commit comments