-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (143 loc) · 4.04 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"time"
"github.com/gjolly/google-rotation-planner/cmd/google-rotation-planner/localcred"
"github.com/pkg/errors"
"google.golang.org/api/calendar/v3"
"gopkg.in/yaml.v3"
)
const week = 7 * 24 * time.Hour
func createShift(c *Config, member Member, frequence int, startDate time.Time, srv *calendar.Service) error {
endDate := startDate.Add(time.Duration(c.ShiftDuration * int(week)))
tz, err := time.LoadLocation("")
if err != nil {
return err
}
fmt.Printf("creating shift for %v, first shift starting on %v and finshing on %v (%v week(s))\n", member.Name, startDate, endDate, c.ShiftDuration)
event := &calendar.Event{
Summary: fmt.Sprintf("%v on duty", member.Name),
Description: c.Description,
Start: &calendar.EventDateTime{
Date: startDate.Format("2006-01-02"),
TimeZone: tz.String(),
},
End: &calendar.EventDateTime{
Date: endDate.Format("2006-01-02"),
TimeZone: tz.String(),
},
Recurrence: []string{fmt.Sprintf("RRULE:FREQ=WEEKLY;INTERVAL=%v", frequence)},
Attendees: []*calendar.EventAttendee{
{
Email: member.EmailAddr,
DisplayName: member.Name,
},
},
Transparency: "transparent",
}
_, err = srv.Events.Insert(c.CalendarID, event).SendNotifications(c.Notify).Do()
if err != nil {
return errors.Wrap(err, "unable to create event")
}
return nil
}
func createRota(c *Config, srv *calendar.Service) error {
for shiftNum, member := range c.Members {
startDate := c.StartDate.Add(time.Duration(shiftNum * c.ShiftDuration * int(week)))
err := createShift(c, member, len(c.Members), startDate, srv)
if err != nil {
return errors.Wrapf(err, "failed to create shift for %v", member)
}
}
return nil
}
// Member is a persone part of the rota
type Member struct {
Name string `yaml:"name"`
EmailAddr string `yaml:"email"`
}
func (m Member) String() string {
return fmt.Sprintf("%v (%v)", m.Name, m.EmailAddr)
}
// Config holds the informations about the google calendar
// to use, the members of the rota and other paramaters
// for the rota
type Config struct {
StartDate time.Time `yaml:"startDate"`
Members []Member `yaml:"members"`
CalendarID string `yaml:"calendarID"`
ShiftDuration int `yaml:"shiftDuration"`
Description string `yaml:"description"`
Notify bool `yaml:"notify"`
}
func parseConfig() (*Config, error) {
c := new(Config)
content, err := ioutil.ReadFile("config.yaml") // the file is inside the local directory
if err != nil {
return nil, err
}
err = yaml.Unmarshal(content, c)
if err != nil {
return nil, err
}
return c, nil
}
func cleanup(c *Config, srv *calendar.Service) error {
t := time.Now().Format(time.RFC3339)
events, err := srv.Events.List(c.CalendarID).ShowDeleted(false).TimeMin(t).Do()
if err != nil {
return errors.Wrap(err, "Unable to retrieve next ten of the user's events")
}
if len(events.Items) == 0 {
fmt.Println("No upcoming events found.")
} else {
for _, item := range events.Items {
fmt.Println("deleting ", item.Summary)
err = srv.Events.Delete(c.CalendarID, item.Id).Do()
if err != nil {
fmt.Println(err)
}
}
}
return nil
}
var FlagCleanup = flag.Bool("cleanup", false, "Remove all the recurring events from the calendar.")
func main() {
flag.Parse()
ctx := context.Background()
home, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Cannot find user's home folder: %v", err)
}
cfgDir := path.Join(home, ".google-rotation-planner")
provider := new(localcred.Provider)
err = provider.InitConfig(cfgDir)
if err != nil {
log.Fatalf("Unable to init config: %v", err)
}
srv, err := provider.Service(ctx, cfgDir)
if err != nil {
log.Fatalf("Unable to retrieve Calendar client: %v", err)
}
c, err := parseConfig()
if err != nil {
log.Fatalf("Unable to parse config: %v", err)
}
if *FlagCleanup {
err = cleanup(c, srv)
if err != nil {
log.Fatal(err)
}
return
}
err = createRota(c, srv)
if err != nil {
log.Fatalf("Failed to create rota: %v", err)
}
}