-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.go
63 lines (56 loc) · 1.48 KB
/
stop.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
package main
import (
"context"
"errors"
"strings"
"time"
pb "github.com/OpenPeeDeeP/ChessClock-CLI/chessclock"
"github.com/spf13/cobra"
)
var reasons = make(map[string]pb.StopRequest_Reason)
var stopLogger = mainLogger.With().Str("cmd", "stop").Logger()
var stopCmd = &cobra.Command{
Use: "stop [eod|break|lunch]",
Short: "Stop all tasks for the specified reasons",
Args: validateStopCmdArgs,
RunE: stopCmdRun,
PreRunE: startClient(stopLogger),
PostRunE: stopClient,
}
func init() {
reasons["eod"] = pb.StopRequest_EndOfDay
reasons["break"] = pb.StopRequest_Break
reasons["lunch"] = pb.StopRequest_Lunch
}
func stopCmdRun(cmd *cobra.Command, args []string) error {
var reason string
if len(args) > 0 {
reason = args[0]
} else {
reason = "break"
}
_, err := client.Stop(context.Background(), &pb.StopRequest{
Timestamp: time.Now().Unix(),
Reason: reasons[strings.ToLower(reason)],
})
if err != nil {
stopLogger.Error().Err(err).Msg("Could not stop the previous task")
return err
}
return nil
}
func validateStopCmdArgs(cmd *cobra.Command, args []string) (err error) {
if len(args) > 1 {
err = errors.New("Too many arguments")
stopLogger.Error().Err(err).Msg("Incorrect Arguments")
return err
}
if len(args) > 0 {
if _, ok := reasons[strings.ToLower(args[0])]; !ok {
err = errors.New("Reason given is not a valid reason (eod, break, lunch)")
stopLogger.Error().Err(err).Msg("Incorrect Arguments")
return err
}
}
return nil
}