/
config.go
101 lines (88 loc) · 2.92 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
/*
Copyright paskal.maksim@gmail.com
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type ExtraLabels struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type TelegramButton struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type ConfigDomains struct {
// name of domain
Name string `yaml:"name"`
// telegram token
Token string `yaml:"token"`
// telegram chatID
ChatID int64 `yaml:"chatID"` //nolint:tagliatelle
// add labels to alert
ExtraLabels []ExtraLabels `yaml:"extraLabels"`
// add buttons to prometheus alerts
PrometheusButtons []TelegramButton `yaml:"prometheusButtons"`
// add buttons to prometheus alerts
SentryButtons []TelegramButton `yaml:"sentryButtons"`
// pointer to BotAPI
bot *tgbotapi.BotAPI
}
type Config struct {
// defaults values to all domains
Defaults ConfigDomains `yaml:"defaults"`
// telegram domains
Domains []ConfigDomains `yaml:"domains"`
}
// add defaults values to domains.
func (config *Config) fillDefaults() {
for i, domain := range config.Domains {
if len(domain.ExtraLabels) == 0 {
config.Domains[i].ExtraLabels = config.Defaults.ExtraLabels
}
if len(domain.Token) == 0 {
config.Domains[i].Token = config.Defaults.Token
}
if len(domain.PrometheusButtons) == 0 {
config.Domains[i].PrometheusButtons = config.Defaults.PrometheusButtons
}
if len(domain.SentryButtons) == 0 {
config.Domains[i].SentryButtons = config.Defaults.SentryButtons
}
}
}
type appConfigType struct {
Version string
showVersion *bool
address *string
logLevel *string
logPretty *bool
chatServer *bool
defaultDomain *string
configFileName *string
gracefulStop *time.Duration
}
//nolint:gochecknoglobals
var appConfig = appConfigType{
Version: gitVersion,
showVersion: flag.Bool("version", false, "show version"),
address: flag.String("server.address", defaultAddress, "server port"),
logLevel: flag.String("log.level", "INFO", "log level"),
logPretty: flag.Bool("log.pretty", false, "log in pretty format"),
chatServer: flag.Bool("enableChatServer", false, "enableChatServer"),
defaultDomain: flag.String("defaultDomain", "default", "domain for default"),
configFileName: flag.String("config", "config.yaml", "config yaml path"),
gracefulStop: flag.Duration("gracefulStop", defaultGracefulStop, "time to wait for graceful stop"),
}