-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerator.go
152 lines (128 loc) · 2.94 KB
/
generator.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
package ics
import (
"bytes"
"encoding/hex"
"strconv"
"strings"
"text/template"
"time"
)
func Generate(prodId string, events ...*Event) (string, error) {
obj := &generator{
ProdId: prodId,
Events: []string{},
}
eventTmpl, err := template.New("events").Parse(vevent)
if err != nil {
return "", err
}
for _, event := range events {
for idx := range event.Attendees {
if event.Attendees[idx].Rsvp == "" {
event.Attendees[idx].Rsvp = Rsvp_False
}
}
e := &vEvent{
Event: event,
DtStamp: FormatDateTime(time.Now()),
DtEnd: FormatDateTime(event.DtEnd),
DtStart: FormatDateTime(event.DtStart),
ExDate: make([]string, len(event.ExDate)),
Description: strings.Join(strings.Split(event.Description, "\n"), `\n`),
}
for i, exd := range event.ExDate {
e.ExDate[i] = FormatDateTime(exd)
}
event.dtStamp = e.DtStamp
event.UID = hex.EncodeToString([]byte(e.UID))
buf := &bytes.Buffer{}
if err := eventTmpl.Execute(buf, e); err != nil {
return "", err
}
obj.Events = append(obj.Events, buf.String())
}
buf := &bytes.Buffer{}
icsTmpl, err := template.New("ics").Parse(ics)
if err != nil {
return "", err
}
if err := icsTmpl.Execute(buf, obj); err != nil {
return "", err
}
return buf.String(), nil
}
func (event *Event) Generate(prodId string) (string, error) {
obj := &generator{
ProdId: prodId,
Events: []string{},
}
for idx := range event.Attendees {
if event.Attendees[idx].Rsvp == "" {
event.Attendees[idx].Rsvp = Rsvp_False
}
}
e := &vEvent{
Event: event,
DtStamp: FormatDateTime(time.Now()),
DtEnd: FormatDateTime(event.DtEnd),
DtStart: FormatDateTime(event.DtStart),
ExDate: make([]string, len(event.ExDate)),
Description: strings.Join(strings.Split(event.Description, "\n"), `\n`),
}
for i, exd := range event.ExDate {
e.ExDate[i] = FormatDateTime(exd)
}
event.dtStamp = e.DtStamp
event.UID = hex.EncodeToString([]byte(e.UID))
eventTmpl, err := template.New("events").Parse(vevent)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
if err := eventTmpl.Execute(buf, e); err != nil {
return "", err
}
obj.Events = append(obj.Events, buf.String())
buf = &bytes.Buffer{}
icsTmpl, err := template.New("ics").Parse(ics)
if err != nil {
return "", err
}
if err := icsTmpl.Execute(buf, obj); err != nil {
return "", err
}
return buf.String(), nil
}
type generator struct {
ProdId string
Events []string
}
func FormatDateTime(t time.Time) string {
dt := strconv.Itoa(t.Year())
month := strconv.Itoa(int(t.Month()))
if len(month) < 2 {
dt += "0"
}
dt += month
day := strconv.Itoa(t.Day())
if len(day) < 2 {
dt += "0"
}
dt += day + "T"
hour := strconv.Itoa(t.Hour())
if len(hour) < 2 {
dt += "0"
}
dt += hour
min := strconv.Itoa(t.Minute())
if len(min) < 2 {
dt += "0"
}
dt += min
sec := strconv.Itoa(t.Second())
if len(sec) < 2 {
dt += "0"
}
dt += sec + "Z"
return dt
}