/
config.go
157 lines (131 loc) · 4.12 KB
/
config.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
package tga
import (
"encoding/json"
"fmt"
"os"
"time"
)
type Config struct {
TooGoodToGoConfig TooGoodToGoConfig `json:"tooGoodToGoConfig"`
SendConfig SendConfig `json:"sendConfig"`
Verbose bool `json:"verbose"`
}
// Custom duration to be able to unmarshall it from strings
type Duration struct {
time.Duration
}
type TooGoodToGoAccount struct {
Email string `json:"email"`
UserAgent string `json:"userAgent"`
}
type TooGoodToGoConfig struct {
Accounts []TooGoodToGoAccount `json:"accounts"`
Language string `json:"language"`
AverageRequestsPeriod Duration `json:"averageRequestsPeriod"`
TooManyRequestsPausePeriod Duration `json:"tooManyRequestsPausePeriod"`
ActiveOrdersReminderPeriod Duration `json:"activeOrdersReminderPeriod"`
LogInEmailValidationRequestsPeriod Duration `json:"logInEmailValidationRequestsPeriod"`
LogInEmailValidationTimeoutDuration Duration `json:"logInEmailValidationTimeoutDuration"`
LogInValidityDuration Duration `json:"logInValidityDuration"`
TokenValidityDuration Duration `json:"tokenValidityDuration"`
SearchConfig SearchConfig `json:"searchConfig"`
}
type Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
type SearchConfig struct {
Origin Location `json:"origin"`
RadiusInKm int `json:"radiusInKm"`
NbMaxResults int `json:"nbMaxResults"`
FavoritesOnly bool `json:"favoritesOnly"`
WithStockOnly bool `json:"withStockOnly"`
}
type SendActionType int
const (
NoSend SendActionType = iota
SendEmail
SendWhatsApp
)
func (s SendActionType) String() string {
switch s {
case NoSend:
return ""
case SendEmail:
return "email"
case SendWhatsApp:
return "whatsapp"
}
return "<error>"
}
func NewSendActionType(str string) (SendActionType, error) {
if str == "" {
return NoSend, nil
}
if str == "email" {
return SendEmail, nil
}
if str == "whatsapp" {
return SendWhatsApp, nil
}
return -1, fmt.Errorf("unknown send action type %v", str)
}
func (s SendActionType) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *SendActionType) UnmarshalJSON(b []byte) error {
sendAction, err := NewSendActionType(string(b[1 : len(b)-1]))
if err != nil {
return fmt.Errorf("error from NewSendActionType: %w", err)
}
*s = sendAction
return nil
}
type SendConfig struct {
EmailConfig EmailConfig `json:"emailConfig"`
WhatsAppConfig WhatsAppConfig `json:"whatsAppConfig"`
SendAction SendActionType `json:"sendAction"`
}
type EmailConfig struct {
EmailFrom string `json:"emailFrom"`
EmailTo string `json:"emailTo"`
GmailApiKeyFile string `json:"gmailApiKeyFile"`
OauthPortCallback int `json:"oauthPortCallBack"`
}
type WhatsAppConfig struct {
GroupNameTo string `json:"groupNameTo"`
UserNameTo string `json:"userNameTo"`
}
func ReadConfigFromFile(filePath string) (*Config, error) {
configDataBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("error from os.ReadFile: %w", err)
}
config := &Config{}
err = json.Unmarshal(configDataBytes, config)
if err != nil {
return nil, fmt.Errorf("error from json.Unmarshal: %w", err)
}
if len(config.TooGoodToGoConfig.Accounts) == 0 {
return nil, fmt.Errorf("you need to specify at least one too good to go account\n")
}
glog.Printf("loaded configuration from %v\n", filePath)
return config, err
}
func (duration *Duration) UnmarshalJSON(b []byte) error {
var unmarshalledJson interface{}
err := json.Unmarshal(b, &unmarshalledJson)
if err != nil {
return fmt.Errorf("error from json.Unmarshal: %w", err)
}
switch value := unmarshalledJson.(type) {
case string:
duration.Duration, err = time.ParseDuration(value)
if err != nil {
return fmt.Errorf("error from time.ParseDuration: %w", err)
}
default:
return fmt.Errorf("invalid duration: %#v, provide it as string", unmarshalledJson)
}
return nil
}