-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (123 loc) · 3.39 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
145
146
147
148
149
150
151
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/dteare/gocerb/cerb"
)
var cerbCredsFilepath string
func main() {
flag.StringVar(&cerbCredsFilepath, "cerb-creds", "~/.config/cerb/creds.json", "Path to file containing Cerb credentials.")
flag.Parse()
client := &http.Client{}
creds, err := loadCerbCreds()
if err != nil {
fmt.Printf("Failed to load cerb creds: %v\n", err)
fmt.Printf("\nSETUP Instructions: Copy sample-creds.json to ~/.config/cerb/creds.json and edit to use your api keys or pass in the -cerb-creds parameter to specify where to find it.")
return
}
c := cerb.NewCerberus(*creds, *client)
testCreateTicket(c)
// testFindTicketsByEmail(c)
// testListOpenTickets(c)
// testListGroups(c)
}
func testCreateTicket(c cerb.Cerberus) {
q := cerb.CustomerQuestion{
BucketID: 1049,
GroupID: 900,
Content: "Hello there! ❤️",
From: "[email protected]",
Notes: "Some exciting notes that stand out in a stunning yellow. 🎨",
Subject: "GoCerb! 🤘🏼",
To: "[email protected]",
Participants: []string{"[email protected]"},
CustomFields: []cerb.CustomField{
cerb.CustomField{
ID: 37, // Found using Search > Custom Fields in your Cerb workspace
Value: "1",
},
},
}
m, err := c.CreateMessage(q)
if err != nil {
fmt.Printf("Failed to create message: %v\n", err)
os.Exit(1)
}
fmt.Printf("Create message %d within ticket %d! 💌 %s\n", m.ID, m.ID, m.TicketURL)
customFields := []cerb.CustomField{
cerb.CustomField{
ID: 37,
Value: "1",
},
}
err = c.SetCustomTicketFields(m.TicketID, customFields)
if err != nil {
fmt.Printf("Failed to set custom fields: %v", err)
os.Exit(1)
}
}
func testFindTicketsByEmail(c cerb.Cerberus) {
email := "[email protected]"
tickets, err := c.FindTicketsByEmail(email)
if err != nil {
fmt.Printf("Failed to find tickets by email: %v\n", err)
os.Exit(1)
}
fmt.Printf("Found %d open tickets for %s.\n", len(*tickets), email)
}
func testListOpenTickets(c cerb.Cerberus) {
page := 0
for {
tickets, remaining, err := c.ListOpenTickets(page)
if err != nil {
fmt.Printf("Error finding open cerb tickets: %v\n", err)
os.Exit(1)
}
fmt.Printf("Loaded %d tickets from page %d. %d tickets remain on subsequent pages.\n", len(*tickets), page, remaining)
// for _, t := range *tickets {
// fmt.Println("\t", t.Email)
// }
if remaining == 0 {
break
}
page++
}
}
func testListGroups(c cerb.Cerberus) {
groups, err := c.FindAllGroupsAndBuckets()
if err != nil {
panic(err)
}
fmt.Printf("Found %d groups:\n", len(*groups))
for _, group := range *groups {
fmt.Printf("%s (%d)\n", group.Name, group.ID)
for _, bucket := range group.Buckets {
fmt.Printf("\t%s (%d)\n", bucket.Name, bucket.ID)
}
}
}
func loadCerbCreds() (*cerb.CerberusCreds, error) {
path := cerbCredsFilepath
if strings.HasPrefix(path, "~/") {
usr, _ := user.Current()
home := usr.HomeDir
path = filepath.Join(home, path[2:])
}
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Error loading credentials from %s: %v", path, err)
}
var creds cerb.CerberusCreds
err = json.Unmarshal(bytes, &creds)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling Cerb creds from %s: %v", path, err)
}
return &creds, nil
}