-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule.go
79 lines (73 loc) · 1.96 KB
/
schedule.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
package main
import (
"context"
"errors"
"fmt"
"os"
"text/tabwriter"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/OpenPeeDeeP/ChessClock-CLI/chessclock"
"github.com/spf13/cobra"
)
var scheduleLogger = mainLogger.With().Str("cmd", "schedule").Logger()
var scheduleCmd = &cobra.Command{
Use: "schedule [DATE]",
Short: "Show all of the tasks for the specified date (defaults to today)",
Args: validateScheduleCmdArgs,
RunE: scheduleCmdRun,
PreRunE: startClient(scheduleLogger),
PostRunE: stopClient,
}
func scheduleCmdRun(cmd *cobra.Command, args []string) error {
var date int64
var err error
if len(args) < 1 {
date = time.Now().Unix()
} else {
date, err = parseDate(args[0])
if err != nil {
scheduleLogger.Error().Err(err).Msg("How did you get here")
return err
}
}
res, err := client.Schedule(context.Background(), &pb.ScheduleRequest{
Date: date,
})
if err != nil {
if st, ok := status.FromError(err); ok && st.Code() == codes.NotFound {
if len(args) < 1 {
scheduleLogger.Error().Msg("No task started for today")
} else {
scheduleLogger.Error().Msg("No task for that date")
}
return err
}
scheduleLogger.Error().Err(err).Msg("Could not get the schedule")
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
defer w.Flush()
fmt.Fprint(w, "TIME(UTC)\tTAG\tDESCRIPTION\n")
for _, task := range res.GetTasks() {
tm := time.Unix(task.GetTimestamp(), 0).UTC()
fmt.Fprintf(w, "%s\t%s\t%s\n", tm.Format("15:04:05"), task.GetTag(), task.GetDescription())
}
return nil
}
func validateScheduleCmdArgs(cmd *cobra.Command, args []string) (err error) {
if len(args) > 1 {
err = errors.New("Too many arguments")
scheduleLogger.Error().Err(err).Msg("Incorrect Arguments")
return err
}
if len(args) != 1 {
return nil
}
if _, err = parseDate(args[0]); err != nil {
scheduleLogger.Error().Err(err).Msg("Incorrect Arguments")
return err
}
return nil
}