-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
labels.go
65 lines (52 loc) · 1.43 KB
/
labels.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
package main
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
"google.golang.org/api/gmail/v1"
)
type labelMap map[string]string
func getLabelMap() (labelMap, error) {
// Get the labels for the user and map its name to its ID.
l, err := api.Users.Labels.List(gmailUser).Do()
if err != nil {
return nil, fmt.Errorf("listing labels failed: %v", err)
}
labels := labelMap{}
for _, label := range l.Labels {
labels[strings.ToLower(label.Name)] = label.Id
}
return labels, nil
}
func getLabelMapOnID() (labelMap, error) {
// Get the labels for the user and map its name to its ID.
l, err := api.Users.Labels.List(gmailUser).Do()
if err != nil {
return nil, fmt.Errorf("listing labels failed: %v", err)
}
labels := labelMap{}
for _, label := range l.Labels {
labels[label.Id] = label.Name
}
return labels, nil
}
func (m *labelMap) createLabelIfDoesNotExist(name string) (string, error) {
// De reference the pointer so we can index.
labels := *m
// Try to find the label.
id, ok := labels[strings.ToLower(name)]
if ok {
// We found the label.
return id, nil
}
// Create the label if it does not exist.
label, err := api.Users.Labels.Create(gmailUser, &gmail.Label{Name: name}).Do()
if err != nil {
return "", fmt.Errorf("creating label %s failed: %v", name, err)
}
logrus.Infof("Created label: %s", name)
// Update our label map.
labels[strings.ToLower(name)] = label.Id
m = &labels
return label.Id, nil
}