-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathics.go
76 lines (64 loc) · 1.76 KB
/
ics.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
package export
import (
_ "embed"
"errors"
"fmt"
"io"
"text/template"
"github.com/orltom/on-call-schedule/pkg/apis"
)
//go:embed ics.tmpl
var templateICSFile string
var _ apis.Exporter = &TableExporter{}
type event struct {
apis.Shift
UID string
Category string
XColor string
Attendee string
Summary string
Description string
}
type ICSExporter struct {
template *template.Template
}
func NewICSExporter() *ICSExporter {
tmpl, _ := template.New("ical").Parse(templateICSFile)
return &ICSExporter{template: tmpl}
}
func (e *ICSExporter) Write(plan []apis.Shift, writer io.Writer) error {
if plan == nil {
return errors.New("shifts must not be nil")
}
events := make([]event, 0, len(plan))
for idx := range plan {
s := plan[idx]
events = append(events, mapPrimaryEvent(s), mapSecondaryEvent(s))
}
if err := e.template.ExecuteTemplate(writer, "icalCalendar", events); err != nil {
return fmt.Errorf("can not generate ICS: %w", err)
}
return nil
}
func mapPrimaryEvent(shift apis.Shift) event {
return event{
Shift: shift,
UID: "primary-" + shift.Start.Format("20060102"),
Category: "Primary",
XColor: "#FF5733",
Attendee: string(shift.Primary),
Summary: "Primary On-Call: " + string(shift.Primary),
Description: fmt.Sprintf("Primary: %s\\nSecondary: %s", shift.Primary, shift.Secondary),
}
}
func mapSecondaryEvent(shift apis.Shift) event {
return event{
Shift: shift,
UID: "secondary-" + shift.Start.Format("20060102"),
Category: "Secondary",
XColor: "#33FF57",
Attendee: string(shift.Secondary),
Summary: "Secondary On-Call: " + string(shift.Secondary),
Description: fmt.Sprintf("Primary: %s\\nSecondary: %s", shift.Primary, shift.Secondary),
}
}