/
gh-tracker.go
161 lines (133 loc) · 3.73 KB
/
gh-tracker.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
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"fmt"
"flag"
"log"
"net/smtp"
"os"
"github.com/BurntSushi/toml"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
// Configuration struct that holds the tracker runtime configuration
type Configuration struct {
Environment string `toml:"environment"`
API apiConfiguration `toml:"api"`
Mail mailConfiguration `toml:"mail"`
Logger loggerConfiguration `toml:"logger"`
}
type apiConfiguration struct {
TokenAccess string `toml:"token_access"`
}
type mailConfiguration struct {
Server string `toml:"server"`
SenderMail string `toml:"sender_mail"`
ReceiptMail string `toml:"receipt_mail"`
}
type loggerConfiguration struct {
File string `toml:"prefix"`
Prefix string `toml:"file"`
}
// Tracker is the struct that holds the configuration for the tracker client
type Tracker struct {
config Configuration
logger *log.Logger
}
func usage() {
fmt.Println("Usage: gh-tracker <config_file>")
}
func (t Tracker) loadConfigFromFile(configFile string, config *Configuration) error {
_, err := os.Stat(configFile)
if err != nil {
log.Fatal("Config file is missing: ", configFile)
}
//config := Configuration{}
if _, err := toml.DecodeFile(configFile, &config); err != nil {
log.Fatal(err)
}
return nil
}
func (t Tracker) sendMail(body string) error {
// Connect to the remote SMTP server.
c, err := smtp.Dial(t.config.Mail.ReceiptMail)
if err != nil {
return err
}
defer c.Close()
// Set the sender and recipient.
c.Mail(t.config.Mail.SenderMail)
c.Rcpt(t.config.Mail.ReceiptMail)
// Send the email body.
wc, err := c.Data()
if err != nil {
return err
}
defer wc.Close()
buf := bytes.NewBufferString(body)
if _, err = buf.WriteTo(wc); err != nil {
return err
}
return nil
}
func initialize(configFile string) (Tracker, error) {
tracker := Tracker{}
//Load configuration from file
err := tracker.loadConfigFromFile(configFile, &tracker.config)
if err != nil {
return tracker, err
}
// Configure logger
var loggerOut *os.File
if tracker.config.Environment == "development" {
loggerOut = os.Stderr
} else if tracker.config.Environment == "production" {
loggerOut, err = os.Open(tracker.config.Logger.File)
if err != nil {
panic("Cannot open log file")
}
} else {
loggerOut = os.Stderr
}
tracker.logger = log.New(loggerOut, tracker.config.Logger.Prefix, log.Ldate|log.Ltime|log.Llongfile)
return tracker, nil
}
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
usage()
os.Exit(1)
}
tracker, err := initialize(flag.Arg(0))
if err != nil {
tracker.logger.Panicf("Error initializing the tracker: %s", err.Error())
}
tokenService := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: tracker.config.API.TokenAccess},
)
tokenContext := oauth2.NewClient(oauth2.NoContext, tokenService)
client := github.NewClient(tokenContext)
// Get unread notifications
//opt := &github.NotificationListOptions{All: true}
notifications, _, err := client.Activity.ListNotifications(nil)
if err != nil {
tracker.logger.Printf("Error listing notifications: %s", err.Error())
}
//fmt.Printf("Lenght of notifications array: %d\n", len(notifications))
//If there are some notification, send mail
if len(notifications) > 0 {
bodyStr := fmt.Sprintf("There are %d new nofications in your GitHub account.\n\n", len(notifications))
for _, notification := range notifications {
var readed string
if *notification.Unread {
readed = "Not readed"
} else{
readed = "Readed"
}
bodyStr += fmt.Sprintf("\t%s\t[%s]\t%s: %s. \n", readed, *notification.ID,*notification.Repository.FullName, *notification.Subject.Title)
}
tracker.logger.Println(bodyStr)
tracker.sendMail(bodyStr)
}
}