-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (64 loc) · 1.92 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
76
77
78
79
80
package main
import (
"calsync/calendar/gcal"
"calsync/calendar/maccalendar"
"calsync/config"
"context"
"fmt"
"log"
"os"
"calsync/calendar"
"golang.org/x/oauth2/google"
gCalenader "google.golang.org/api/calendar/v3"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
log.Printf("Running calsync version: %s-%s-%s", version, commit, date)
ctx := context.Background()
cfg, err := config.GetConfig(os.Getenv("HOME") + "/.config/calsync/config.toml")
if err != nil {
log.Fatalf("Failed to get config: %s", err)
}
events, err := getMacEvents(ctx, cfg)
if err != nil {
log.Fatalf("Failed to get events from Mac calendar: %s", err)
}
gCli, err := newGoogleClient(ctx, cfg)
if err != nil {
log.Fatalf("Failed to initialize Google client: %s", err)
}
if err := gCli.SyncCalendar(events); err != nil {
log.Fatalf("Failed to sync calendar: %s", err)
}
}
func getMacEvents(ctx context.Context, cfg *config.Config) ([]calendar.Event, error) {
macCli, err := maccalendar.New(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("Failed to initialize Mac's client: %s", err)
}
events, err := macCli.GetEvents()
if err != nil {
return nil, fmt.Errorf("Couldn't get list of events from Mac calendar: %s", err)
}
return events, nil
}
func newGoogleClient(ctx context.Context, cfg *config.Config) (*gcal.Client, error) {
b, err := os.ReadFile(cfg.CredentialsFile())
if err != nil {
return nil, fmt.Errorf("Unable to read client secret file, location: %s: err: %w", cfg.CredentialsFile(), err)
}
// If modifying these scopes, delete your previously saved token.json.
oAuthCfg, err := google.ConfigFromJSON(b, gCalenader.CalendarScope)
if err != nil {
return nil, fmt.Errorf("Unable to parse client secret file to oAuthCfg: %v", err)
}
client, err := gcal.New(ctx, cfg, oAuthCfg)
if err != nil {
return nil, fmt.Errorf("Failed to get initialize gcal: %s", err)
}
return client, nil
}