-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
99 lines (83 loc) · 3.03 KB
/
parser.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
package main
import (
"errors"
"fmt"
"sort"
"strings"
"time"
)
var allowedThemes = map[string]string{
"extracurricular": "[todoist] extracurricular work",
"project": "[todoist] project work", // productive
"ten percent": "[todoist] stuff I want to do on my own time",
"help": "[unplanned] when people ask for help",
"pager": "[unplanned] when the pager goes off",
"admin": "getting ready for my day, repeated tasks, email",
"coffee": "socializing around the office, Core Social meeting",
"meeting": "scheduled meetings, unrelated to projects or extracurricular",
"ops": "k8s work, dev ops, net ops", // productive
"code": "writing code", // productive
"toil": "manual work, generally deprovisions",
}
var productiveThemes = []string{"project", "ops", "code"}
// var allowedThemes = []string{
// // Todoist
// "extracurricular", // extracurricular work (Todoist project)
// "project", // project work (Todoist project)
// "ten percent", // stuff I want to do on my own time (Todoist project)
// // Other
// "admin", // getting ready for my day, repeated tasks, email
// "coffee", // socializing around the office, Core Social meeting
// "meeting", // scheduled meetings, unrelated to projects or extracurricular
// "ops", // k8s work, dev ops, net ops
// "pager", // unplanned requests, help, pages
// "toil", // manual work, generally deprovisions
// // "code", removed in favor of "project"
// // "design", removed in favor of "project"
// // "help", removed in favor of "pager"
// // "k8s", removed in favor of "ops"
// }
func isAllowedTheme(theme string) bool {
if _, ok := allowedThemes[theme]; ok {
return true
}
return false
}
func isProductiveTheme(theme string) bool {
for _, productive := range productiveThemes {
if theme == productive {
return true
}
}
return false
}
func allowedThemesErrorString(theme string) string {
errorString := fmt.Sprintf("error parsing data: theme '%s' is not allowed\n\nallowed themes are:\n", theme)
var sortedAllowedThemes []string
for theme, _ := range allowedThemes {
sortedAllowedThemes = append(sortedAllowedThemes, theme)
}
sort.Strings(sortedAllowedThemes)
for _, theme := range sortedAllowedThemes {
errorString = fmt.Sprintf("%s\n ➔ %s: %s", errorString, theme, allowedThemes[theme])
}
return errorString
}
func parseLine(line string) (string, time.Time, error) {
trimmed := strings.TrimPrefix(line, "## ")
lineParts := strings.Split(trimmed, " (")
if len(lineParts) != 2 {
return "", time.Time{}, errors.New(fmt.Sprintf("found malformed line: %s\n", line))
}
date := lineParts[0]
parsedDate, err := time.Parse("2006-01-02 Mon 15:04 PM MST", date)
if err != nil {
return "", time.Time{}, errors.New(fmt.Sprintf("error parsing date: %s\n", date))
}
category := strings.TrimSuffix(lineParts[1], ")")
theme := strings.Split(category, ", ")[0]
if !isAllowedTheme(theme) && theme != "break" {
return "", time.Time{}, errors.New(allowedThemesErrorString(theme))
}
return category, parsedDate, nil
}