-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheets.go
52 lines (46 loc) · 1.28 KB
/
sheets.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
package main
import (
"context"
"errors"
"fmt"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/OpenPeeDeeP/ChessClock-CLI/chessclock"
"github.com/spf13/cobra"
)
var sheetsLogger = mainLogger.With().Str("cmd", "sheets").Logger()
var sheetsCmd = &cobra.Command{
Use: "sheets",
Short: "list all of the timesheets",
Args: validateSheetsCmdArgs,
RunE: sheetsCmdRun,
PreRunE: startClient(sheetsLogger),
PostRunE: stopClient,
}
func sheetsCmdRun(cmd *cobra.Command, args []string) error {
sheets, err := client.ListTimeSheets(context.Background(), &pb.ListTimeSheetsRequest{})
if err != nil {
if st, ok := status.FromError(err); ok && st.Code() == codes.NotFound {
sheetsLogger.Error().Msg("No timesheets found")
return err
}
sheetsLogger.Error().Err(err).Msg("Could not get a list of time sheets")
return err
}
for _, sheet := range sheets.GetDates() {
date := time.Unix(sheet, 0).UTC()
fmt.Printf("%04d/%02d/%02d\n", date.Year(), date.Month(), date.Day())
}
return nil
}
func validateSheetsCmdArgs(cmd *cobra.Command, args []string) (err error) {
switch {
case len(args) > 0:
err = errors.New("Too many arguments")
sheetsLogger.Error().Err(err).Msg("Incorrect Arguments")
return err
default:
return nil
}
}