-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
253 lines (238 loc) · 6.03 KB
/
handlers.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"context"
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/DarthBenro008/pigil/internal/database"
service2 "github.com/DarthBenro008/pigil/internal/service"
"github.com/DarthBenro008/pigil/internal/types"
"github.com/DarthBenro008/pigil/internal/utils"
"golang.org/x/oauth2"
"time"
)
const handlerTag = "handlers"
func InsertCommand(service database.Service,
information types.CommandInformation) {
err := service.Insert(information)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
}
func ListCommand(service database.Service) {
data, err := service.List()
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
if len(*data) == 0 {
utils.InformationLogger("No history found yet!")
return
}
utils.PrintInformation(data)
}
func GoogleAuth(service database.Service) {
email, err := service.GetConfig(utils.UserEmail)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
if email != "" {
utils.InformationLogger(fmt.Sprintf("already logged in with %s\n",
email))
return
}
config := service2.OAuthGoogleConfig()
config = service2.GoogleLogin(config)
data := service2.GoogleCallback(config)
userEmail := types.ConfigurationInformation{
Key: utils.UserEmail,
Value: data.Email,
}
userRT := types.ConfigurationInformation{
Key: utils.UserAT,
Value: data.AccessToken,
}
userAT := types.ConfigurationInformation{
Key: utils.UserRT,
Value: data.RefreshToken,
}
err = service.InsertConfig(userEmail)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
err = service.InsertConfig(userRT)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
err = service.InsertConfig(userAT)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
}
func Status(service database.Service) {
list, err := service.ListConfig()
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
if len(*list) == 0 {
utils.InformationLogger("No settings found yet!")
return
}
utils.StatusPrinter(list)
}
func Logout(service database.Service) {
err := service.DeleteConfigDb()
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
}
func Notify(service database.Service, information types.CommandInformation) {
for _, channel := range utils.Channels {
value, err := service.GetConfig(channel)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
if value == utils.DbTrue {
switch channel {
case utils.Email:
err = emailNotifier(service, information)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
case utils.Discord:
err = discordNotifier(service, information)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
}
}
}
}
func emailNotifier(service database.Service, information types.CommandInformation) error {
email, err := service.GetConfig(utils.UserEmail)
if err != nil {
return err
}
if email == "" {
utils.ErrorInformation("You are not authenticated! pigil cannot" +
" notify via email, please run `pigil bumf auth`")
return nil
}
at, err := service.GetConfig(utils.UserAT)
if err != nil {
return err
}
rt, err := service.GetConfig(utils.UserRT)
if err != nil {
return err
}
cred := oauth2.Token{AccessToken: at, RefreshToken: rt,
Expiry: time.Now().Add(5)}
client := service2.OAuthGoogleConfig().Client(context.Background(), &cred)
service2.SendEmail(client, email, information)
return nil
}
func discordNotifier(service database.Service, information types.CommandInformation) error {
url, err := service.GetConfig(utils.DbDiscordUrl)
msg := utils.GenerateDiscordMessage(information)
err, status, resp := service2.SendPostRequest(url, msg)
if status != 204 {
return errors.New(resp)
}
if err != nil {
return err
}
return nil
}
func NotificationSelector(service database.Service) {
var selectedChannels []string
prompt := &survey.MultiSelect{
Message: "Select Channel of Notification",
Options: utils.Channels,
Default: utils.Email,
}
err := survey.AskOne(prompt, &selectedChannels, survey.WithValidator(func(ans interface{}) error {
if len(ans.([]core.OptionAnswer)) == 0 {
return errors.New("please select one of the following")
}
return nil
}))
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
for _, channel := range utils.Channels {
if utils.Contains(selectedChannels, channel) {
err := service.InsertConfig(types.ConfigurationInformation{
Key: channel,
Value: utils.DbTrue,
})
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
} else {
err := service.InsertConfig(types.ConfigurationInformation{
Key: channel,
Value: utils.DbFalse,
})
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
}
}
}
func DiscordToggle(service database.Service, toggle bool) {
if !toggle {
err := service.InsertConfig(types.ConfigurationInformation{
Key: utils.Discord,
Value: utils.DbTrue,
})
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
utils.InformationLogger("Discord Webhook has been disabled!")
return
}
input := ""
prompt := &survey.Input{
Message: "Please enter discord webhook url: ",
}
err := survey.AskOne(prompt, &input, survey.WithValidator(survey.Required))
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
err = service.InsertConfig(types.ConfigurationInformation{
Key: utils.DbDiscordUrl,
Value: input,
})
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
err = service.InsertConfig(types.ConfigurationInformation{
Key: utils.Discord,
Value: utils.DbTrue,
})
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
}
func Help() {
msg := utils.GenerateHelp(Version)
fmt.Println(msg)
}
func IsFirstTime(service database.Service) {
value, err := service.GetConfig(utils.DbFirstTime)
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
if value == "" {
fmt.Println("Welcome to Pigil! Please set the notification channels")
NotificationSelector(service)
err = service.InsertConfig(types.ConfigurationInformation{
Key: utils.DbFirstTime,
Value: time.Now().String(),
})
if err != nil {
utils.ErrorLogger(err, handlerTag)
}
}
}