-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (65 loc) · 1.9 KB
/
main.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
package main
import (
"errors"
"os"
"strconv"
"strings"
"time"
"google.golang.org/grpc"
pb "github.com/OpenPeeDeeP/ChessClock-CLI/chessclock"
"github.com/ianschenck/envflag"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var version string
var (
connection *grpc.ClientConn
client pb.ChessClockClient
mainLogger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
daemonConString string
)
func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
rootCmd.AddCommand(startCmd, stopCmd, tagsCmd, sheetsCmd, scheduleCmd, tallyCmd)
envflag.StringVar(&daemonConString, "CCD_CONNECTION_STRING", "localhost:4242", "Connection string to the daemon")
}
func main() {
envflag.Parse()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func startClient(log zerolog.Logger) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var err error
log.Debug().Str("con", daemonConString).Msg("Connecting to daemon")
connection, err = grpc.Dial(daemonConString, grpc.WithInsecure())
if err != nil {
log.Error().Err(err).Msg("Could not connect to the daemon")
return err
}
client = pb.NewChessClockClient(connection)
return nil
}
}
func stopClient(cmd *cobra.Command, args []string) error {
log.Debug().Msg("Disconnecting from the daemon")
return connection.Close()
}
func parseDate(date string) (int64, error) {
dateString := strings.Split(date, "/")
if len(dateString) != 3 {
return 0, errors.New("Date not in the proper format (YYYY/MM/DD)")
}
dateInts := make([]int, 0, 3)
for _, ds := range dateString {
i, err := strconv.Atoi(ds)
if err != nil {
return 0, errors.New("Date not in the proper format (YYYY/MM/DD)")
}
dateInts = append(dateInts, i)
}
dateTime := time.Date(dateInts[0], time.Month(dateInts[1]), dateInts[2], 0, 0, 0, 0, time.Local).Unix()
return dateTime, nil
}