-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.go
121 lines (101 loc) · 3.81 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
// @license
// Copyright (C) 2022 Dinko Korunic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"github.com/BurntSushi/toml"
"github.com/dkorunic/e-dnevnik-bot/logger"
)
// user struct holds a single AAI/SSO username.
type user struct {
Username string `toml:"username"`
Password string `toml:"password"`
}
// telegram struct holds Telegram messenger configuration.
type telegram struct {
Token string `toml:"token"`
ChatIDs []string `toml:"chatids"`
}
// discord struct holds Discord messenger configuration.
type discord struct {
Token string `toml:"token"`
UserIDs []string `toml:"userids"`
}
// slack struct holds Slack messenger configuration.
type slack struct {
Token string `toml:"token"`
ChatIDs []string `toml:"chatids"`
}
// mail struct hold e-mail messenger configuration.
type mail struct {
Server string `toml:"server"`
Port string `toml:"port"`
Username string `toml:"username"`
Password string `toml:"password"`
From string `toml:"from"`
Subject string `toml:"subject"`
To []string `toml:"to"`
}
// calendar struct hold Google Calendar configuration.
type calendar struct {
Name string `toml:"name"`
}
// tomlConfig struct holds all other configuration structures.
type tomlConfig struct {
Calendar calendar `toml:"calendar"`
Mail mail `toml:"mail"`
Telegram telegram `toml:"telegram"`
Discord discord `toml:"discord"`
Slack slack `toml:"slack"`
User []user `toml:"user"`
telegramEnabled bool `toml:"telegram_enabled"`
discordEnabled bool `toml:"discord_enabled"`
slackEnabled bool `toml:"slack_enabled"`
mailEnabled bool `toml:"mail_enabled"`
calendarEnabled bool `toml:"calendar_enabled"`
}
// loadConfig attempts to load and decode configuration file in TOML format, doing a minimal sanity checking and
// optionally returning an error.
func loadConfig() (tomlConfig, error) {
var config tomlConfig
if _, err := toml.DecodeFile(*confFile, &config); err != nil {
return config, err
}
if config.Discord.Token != "" && len(config.Discord.UserIDs) > 0 {
logger.Info().Msg("Configuration: Discord messenger enabled")
config.discordEnabled = true
}
if config.Telegram.Token != "" && len(config.Telegram.ChatIDs) > 0 {
logger.Info().Msg("Configuration: Telegram messenger enabled")
config.telegramEnabled = true
}
if config.Slack.Token != "" && len(config.Slack.ChatIDs) > 0 {
logger.Info().Msg("Configuration: Slack messenger enabled")
config.slackEnabled = true
}
if config.Mail.Server != "" && config.Mail.From != "" && len(config.Mail.To) > 0 {
logger.Info().Msg("Configuration: e-mail messenger enabled")
config.mailEnabled = true
}
if config.Calendar.Name != "" {
config.calendarEnabled = true
}
return config, nil
}