@@ -3,51 +3,23 @@ package main
3
3
import (
4
4
"context"
5
5
"os"
6
+ "path/filepath"
6
7
"strings"
7
8
"time"
8
9
9
- "github.com/alexflint/go-arg"
10
10
"github.com/docker/docker/api/types"
11
11
"github.com/docker/docker/api/types/filters"
12
12
"github.com/docker/docker/client"
13
+ "gopkg.in/yaml.v3"
13
14
14
15
"github.com/rs/zerolog"
15
16
)
16
17
17
- type args struct {
18
- Pushover bool `arg:"env:PUSHOVER" default:"false" help:"Enable/Disable Pushover Notification (True/False)"`
19
- PushoverAPIToken string `arg:"env:PUSHOVER_APITOKEN" help:"Pushover's API Token/Key"`
20
- PushoverUserKey string `arg:"env:PUSHOVER_USER" help:"Pushover's User Key"`
21
- Gotify bool `arg:"env:GOTIFY" default:"false" help:"Enable/Disable Gotify Notification (True/False)"`
22
- GotifyURL string `arg:"env:GOTIFY_URL" help:"URL of your Gotify server"`
23
- GotifyToken string `arg:"env:GOTIFY_TOKEN" help:"Gotify's App Token"`
24
- Mail bool `arg:"env:MAIL" default:"false" help:"Enable/Disable Mail (SMTP) Notification (True/False)"`
25
- MailFrom string `arg:"env:MAIL_FROM" help:"[email protected] "`
26
- MailTo string `arg:"env:MAIL_TO" help:"[email protected] "`
27
- MailUser string `arg:"env:MAIL_USER" help:"SMTP Username"`
28
- MailPassword string `arg:"env:MAIL_PASSWORD" help:"SMTP Password"`
29
- MailPort int `arg:"env:MAIL_PORT" default:"587" help:"SMTP Port"`
30
- MailHost string `arg:"env:MAIL_HOST" help:"SMTP Host"`
31
- Mattermost bool `arg:"env:MATTERMOST" default:"false" help:"Enable/Disable Mattermost Notification (True/False)"`
32
- MattermostURL string `arg:"env:MATTERMOST_URL" help:"URL of your Mattermost incoming webhook"`
33
- MattermostChannel string `arg:"env:MATTERMOST_CHANNEL" help:"Mattermost channel to post in"`
34
- MattermostUser string `arg:"env:MATTERMOST_USER" default:"Docker Event Monitor" help:"Mattermost user to post as"`
35
- Reporters []string `arg:"-"`
36
- Delay time.Duration `arg:"env:DELAY" default:"500ms" help:"Delay before next message is send"`
37
- FilterStrings []string `arg:"env:FILTER,--filter,separate" help:"Filter docker events using Docker syntax."`
38
- Filter map [string ][]string `arg:"-"`
39
- ExcludeStrings []string `arg:"env:EXCLUDE,--exclude,separate" help:"Exclude docker events using Docker syntax."`
40
- Exclude map [string ][]string `arg:"-"`
41
- LogLevel string `arg:"env:LOG_LEVEL" default:"info" help:"Set log level. Use debug for more logging."`
42
- ServerTag string `arg:"env:SERVER_TAG" help:"Prefix to include in the title of notifications. Useful when running docker-event-monitors on multiple machines."`
43
- Version bool `arg:"-v" help:"Print version information."`
44
- }
45
-
46
- // Creating a global logger
18
+ // Create global logger
47
19
var logger zerolog.Logger
48
20
49
21
// hold the supplied run-time arguments globally
50
- var glb_arguments args
22
+ var glb_arguments config
51
23
52
24
// version information, are injected during build process
53
25
var (
@@ -59,45 +31,46 @@ var (
59
31
)
60
32
61
33
func init () {
34
+ loadConfig ()
62
35
parseArgs ()
63
- configureLogger (glb_arguments .LogLevel )
36
+ configureLogger (glb_arguments .Options . LogLevel )
64
37
65
- if glb_arguments .Pushover {
66
- if len (glb_arguments .PushoverAPIToken ) == 0 {
67
- logger .Fatal ().Msg ("Pushover enabled . Pushover API token required!" )
38
+ if glb_arguments .Reporter . Pushover . Enabled {
39
+ if len (glb_arguments .Reporter . Pushover . APIToken ) == 0 {
40
+ logger .Fatal ().Msg ("Pushover Enabled . Pushover API token required!" )
68
41
}
69
- if len (glb_arguments .PushoverUserKey ) == 0 {
70
- logger .Fatal ().Msg ("Pushover enabled . Pushover user key required!" )
42
+ if len (glb_arguments .Reporter . Pushover . UserKey ) == 0 {
43
+ logger .Fatal ().Msg ("Pushover Enabled . Pushover user key required!" )
71
44
}
72
45
}
73
- if glb_arguments .Gotify {
74
- if len (glb_arguments .GotifyURL ) == 0 {
75
- logger .Fatal ().Msg ("Gotify enabled . Gotify URL required!" )
46
+ if glb_arguments .Reporter . Gotify . Enabled {
47
+ if len (glb_arguments .Reporter . Gotify . URL ) == 0 {
48
+ logger .Fatal ().Msg ("Gotify Enabled . Gotify URL required!" )
76
49
}
77
- if len (glb_arguments .GotifyToken ) == 0 {
78
- logger .Fatal ().Msg ("Gotify enabled . Gotify APP token required!" )
50
+ if len (glb_arguments .Reporter . Gotify . Token ) == 0 {
51
+ logger .Fatal ().Msg ("Gotify Enabled . Gotify APP token required!" )
79
52
}
80
53
}
81
- if glb_arguments .Mail {
82
- if len (glb_arguments .MailUser ) == 0 {
83
- logger .Fatal ().Msg ("E-Mail notification enabled . SMTP username required!" )
54
+ if glb_arguments .Reporter . Mail . Enabled {
55
+ if len (glb_arguments .Reporter . Mail . User ) == 0 {
56
+ logger .Fatal ().Msg ("E-Mail notification Enabled . SMTP username required!" )
84
57
}
85
- if len (glb_arguments .MailTo ) == 0 {
86
- logger .Fatal ().Msg ("E-Mail notification enabled . Recipient address required!" )
58
+ if len (glb_arguments .Reporter . Mail . To ) == 0 {
59
+ logger .Fatal ().Msg ("E-Mail notification Enabled . Recipient address required!" )
87
60
}
88
- if len (glb_arguments .MailFrom ) == 0 {
89
- glb_arguments .MailFrom = glb_arguments .MailUser
61
+ if len (glb_arguments .Reporter . Mail . From ) == 0 {
62
+ glb_arguments .Reporter . Mail . From = glb_arguments .Reporter . Mail . User
90
63
}
91
- if len (glb_arguments .MailPassword ) == 0 {
92
- logger .Fatal ().Msg ("E-Mail notification enabled . SMTP Password required!" )
64
+ if len (glb_arguments .Reporter . Mail . Password ) == 0 {
65
+ logger .Fatal ().Msg ("E-Mail notification Enabled . SMTP Password required!" )
93
66
}
94
- if len (glb_arguments .MailHost ) == 0 {
95
- logger .Fatal ().Msg ("E-Mail notification enabled . SMTP host address required!" )
67
+ if len (glb_arguments .Reporter . Mail . Host ) == 0 {
68
+ logger .Fatal ().Msg ("E-Mail notification Enabled . SMTP host address required!" )
96
69
}
97
70
}
98
- if glb_arguments .Mattermost {
99
- if len (glb_arguments .MattermostURL ) == 0 {
100
- logger .Fatal ().Msg ("Mattermost enabled . Mattermost URL required!" )
71
+ if glb_arguments .Reporter . Mattermost . Enabled {
72
+ if len (glb_arguments .Reporter . Mattermost . URL ) == 0 {
73
+ logger .Fatal ().Msg ("Mattermost Enabled . Mattermost URL required!" )
101
74
}
102
75
}
103
76
}
@@ -152,16 +125,33 @@ func main() {
152
125
}
153
126
}
154
127
128
+ func loadConfig () {
129
+ configFile , err := filepath .Abs ("./config.yml" )
130
+
131
+ if err != nil {
132
+ logger .Fatal ().Err (err ).Msg ("Failed to set config file path" )
133
+ }
134
+
135
+ buf , err := os .ReadFile (configFile )
136
+ if err != nil {
137
+ logger .Fatal ().Err (err ).Msg ("Failed to read config file" )
138
+ }
139
+
140
+ err = yaml .Unmarshal (buf , & glb_arguments )
141
+ if err != nil {
142
+ logger .Fatal ().Err (err ).Msg ("Failed to parse config file" )
143
+ }
144
+ }
145
+
155
146
func parseArgs () {
156
- parser := arg .MustParse (& glb_arguments )
157
147
158
148
// Parse (include) filters
159
149
glb_arguments .Filter = make (map [string ][]string )
160
150
161
- for _ , filter := range glb_arguments .FilterStrings {
151
+ for _ , filter := range glb_arguments .Options . FilterStrings {
162
152
pos := strings .Index (filter , "=" )
163
153
if pos == - 1 {
164
- parser . Fail ("each filter should be of the form key=value" )
154
+ logger . Fatal (). Msg ("each filter should be of the form key=value" )
165
155
}
166
156
key := filter [:pos ]
167
157
val := filter [pos + 1 :]
@@ -171,29 +161,29 @@ func parseArgs() {
171
161
// Parse exclude filters
172
162
glb_arguments .Exclude = make (map [string ][]string )
173
163
174
- for _ , exclude := range glb_arguments .ExcludeStrings {
164
+ for _ , exclude := range glb_arguments .Options . ExcludeStrings {
175
165
pos := strings .Index (exclude , "=" )
176
166
if pos == - 1 {
177
- parser . Fail ("each filter should be of the form key=value" )
167
+ logger . Fatal (). Msg ("each filter should be of the form key=value" )
178
168
}
179
169
//trim whitespaces
180
170
key := strings .TrimSpace (exclude [:pos ])
181
171
val := exclude [pos + 1 :]
182
172
glb_arguments .Exclude [key ] = append (glb_arguments .Exclude [key ], val )
183
173
}
184
174
185
- //Parse enabled reportes
175
+ //Parse Enabled reportes
186
176
187
- if glb_arguments .Gotify {
177
+ if glb_arguments .Reporter . Gotify . Enabled {
188
178
glb_arguments .Reporters = append (glb_arguments .Reporters , "Gotify" )
189
179
}
190
- if glb_arguments .Mattermost {
180
+ if glb_arguments .Reporter . Mattermost . Enabled {
191
181
glb_arguments .Reporters = append (glb_arguments .Reporters , "Mattermost" )
192
182
}
193
- if glb_arguments .Pushover {
183
+ if glb_arguments .Reporter . Pushover . Enabled {
194
184
glb_arguments .Reporters = append (glb_arguments .Reporters , "Pushover" )
195
185
}
196
- if glb_arguments .Mail {
186
+ if glb_arguments .Reporter . Mail . Enabled {
197
187
glb_arguments .Reporters = append (glb_arguments .Reporters , "Mail" )
198
188
}
199
189
0 commit comments