-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (125 loc) · 2.76 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
)
func usage(w io.Writer) {
fmt.Println(`NAME:
org2gcal - Convert time-log to json format for gcal
Usage:
org2gcal [date]
DATE:
format 2006-1-2
note This argument is optional. if you do not specify this, date is used today.`)
}
var (
fileName string
calendarID string
nIO, errIO io.Writer
)
func main() {
nIO, errIO = os.Stdin, os.Stdout
fileName = "events.json"
calendarID = os.Getenv("CALENDAR_ID")
if calendarID == "" {
calendarID = "primary"
}
var date string
switch len(os.Args) {
case 1:
date = time.Now().Format("2006-1-2")
case 2:
if os.Args[1] == "--help" {
usage(nIO)
os.Exit(0)
}
date = os.Args[1]
default:
failed(fmt.Sprintf("Invalid argument number. got=%v, want=%v", len(os.Args), "1 or 2"))
}
t, err := time.ParseInLocation("2006-1-2", date, time.Local)
if err != nil {
failed(err.Error())
}
f, _ := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0600)
w := bufio.NewWriter(f)
defer func() {
w.Flush()
f.Close()
}()
body, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
lines := strings.Split(string(body), "\n")
var events []Event
var startTime *time.Time
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
break
}
log := strings.SplitN(line, " ", 3)[1:]
clock := strings.Split(log[0], ":")
h, err := strconv.Atoi(clock[0])
if err != nil {
failed(err.Error())
}
m, err := strconv.Atoi(clock[1])
if err != nil {
failed(err.Error())
}
endTime := t.Add(time.Hour*time.Duration(h) + time.Minute*time.Duration(m))
if startTime == nil {
startTime = &endTime
}
events = append(events, NewEvent(log[1], *startTime, endTime))
startTime = &endTime
}
if d, err := json.Marshal(events); err != nil {
failed(err.Error())
} else {
w.Write(d)
}
}
func failed(message string) {
fmt.Fprintln(errIO, message)
usage(nIO)
os.Exit(1)
}
func NewEvent(summary string, start, end time.Time) Event {
return Event{
CalendarID: calendarID,
Resource: EventResource{
Summary: summary,
Start: NewEventTime(start),
End: NewEventTime(end),
},
}
}
func NewEventTime(t time.Time) EventTime {
return EventTime{
DateTime: t.Format("2006-01-02T15:04:05-0700"),
}
}
type Event struct {
CalendarID string `json:"calendarId"`
Resource EventResource `json:"resource"`
}
type EventResource struct {
Summary string `json:"summary,omitempty"`
Location string `json:"location,omitempty"`
Description string `json:"description,omitempty"`
Start EventTime `json:"start,omitempty"`
End EventTime `json:"end,omitempty"`
}
type EventTime struct {
DateTime string `json:"dateTime,omitempty"`
}