-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
148 lines (123 loc) · 3.24 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
package main
import (
"fmt"
"os"
"sync"
"github.com/spf13/viper"
)
const CONFIG_PATH = "whispergogo.json"
var (
config *Config
once sync.Once
)
type Config struct {
v *viper.Viper
mu sync.RWMutex
AlertStatus bool `mapstructure:"alert_status"`
DarkTheme bool `mapstructure:"dark_theme"`
Keywords []Keyword `mapstructure:"keywords"`
ChatFilters []ChatType `mapstructure:"chat_filters"`
AlertType AlertType `mapstructure:"alert_type"`
TelegramChatID string `mapstructure:"telegram_chat_id"`
}
type AlertType string
const (
AlertTypeBoth AlertType = "both"
AlertTypeChatFilter AlertType = "chat_filter"
AlertTypeKeyword AlertType = "keyword"
)
type Keyword struct {
Keyword string `mapstructure:"keyword"`
Enable bool `mapstructure:"enable"`
}
func GetConfig() (*Config, error) {
var initErr error
once.Do(func() {
config = &Config{
v: viper.New(),
mu: sync.RWMutex{},
}
if err := config.autoInit(); err != nil {
initErr = err
}
})
if initErr != nil {
return nil, initErr
}
return config, nil
}
func (c *Config) autoInit() error {
if _, err := os.Stat(CONFIG_PATH); os.IsNotExist(err) {
c.setDefaults()
if err := c.Save(); err != nil {
return fmt.Errorf("failed to save default config: %v", err)
}
}
c.v.SetConfigType("json")
c.v.SetConfigFile(CONFIG_PATH)
if err := c.v.ReadInConfig(); err != nil {
return fmt.Errorf("failed to read config: %v", err)
}
if err := c.v.Unmarshal(c); err != nil {
return fmt.Errorf("failed to unmarshal config: %v", err)
}
return c.Validate()
}
func (c *Config) Validate() error {
switch c.AlertType {
case AlertTypeBoth, AlertTypeChatFilter, AlertTypeKeyword:
default:
return fmt.Errorf("invalid alert type: %s", c.AlertType)
}
return nil
}
func (c *Config) setDefaults() {
c.defaultKeywords()
c.defaultChatFilter()
c.defaultAlertType()
}
func (c *Config) defaultKeywords() {
c.mu.Lock()
defer c.mu.Unlock()
c.Keywords = []Keyword{
{Keyword: "สวัสดี เราต้องการซื้อ", Enable: true},
// {Keyword: "(แท็บ", Enable: true},
// {Keyword: "ตำแหน่ง: ซ้าย", Enable: true},
{Keyword: "Hi, I would like to buy your", Enable: true},
{Keyword: "Здравствуйте, хочу купить у вас", Enable: true},
{Keyword: "안녕하세요", Enable: true},
// {Keyword: `(stash tab "`, Enable: true},
// {Keyword: `position: left`, Enable: true},
}
}
func (c *Config) defaultChatFilter() {
c.mu.Lock()
defer c.mu.Unlock()
c.ChatFilters = []ChatType{
ChatTypeWhisper,
}
}
func (c *Config) defaultAlertType() {
c.mu.Lock()
defer c.mu.Unlock()
c.AlertType = AlertTypeBoth
}
func (c *Config) Save() error {
c.mu.Lock()
defer c.mu.Unlock()
if err := c.v.MergeConfigMap(map[string]interface{}{
"keywords": c.Keywords,
"alert_status": c.AlertStatus,
"dark_theme": c.DarkTheme,
"chat_filters": c.ChatFilters,
"alert_type": c.AlertType,
"telegram_chat_id": c.TelegramChatID,
}); err != nil {
return fmt.Errorf("failed to merge config: %w", err)
}
return c.v.WriteConfigAs(CONFIG_PATH)
}
func (c *Config) Update(updateFn func(*Config)) error {
updateFn(c)
return c.Save()
}