-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconfigGlobal.go
220 lines (183 loc) · 5.82 KB
/
configGlobal.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright © 2018 Jeff Coffler <[email protected]>
//
// 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 (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/spf13/viper"
)
var (
// Location of duplicacy binary
duplicacyPath string
// Directory for lock files
globalLockDir string
// Directory for log files
globalLogDir string
// Number of log files to retain
globalLogFileCount int
// Notification publishers
onStartNotifiers []Notifier
onSkipNotifiers []Notifier
onSuccessNotifiers []Notifier
onFailureNotifiers []Notifier
)
// loadGlobalConfig reads in config file and ENV variables if set.
func loadGlobalConfig(storageDir string, cfgFile string) error {
var err error
// Read in (or set) global environment variables
if err = setGlobalConfigVariables(storageDir, cfgFile); err != nil {
return err
}
//
// Validate global environment variables
//
// Don't validate path when running unit tests (invalid path is okay for testing)
if _, err = exec.LookPath(duplicacyPath); err != nil && !runningUnitTests {
return err
}
if err = verifyPathExists(globalLockDir); err != nil {
return err
}
os.Mkdir(globalLogDir, 0755)
if err = verifyPathExists(globalLogDir); err != nil {
return err
}
if globalLogFileCount < 2 {
err = errors.New("logfilecount must have at least two log files saved")
fmt.Fprintln(os.Stderr, "Error:", err)
}
return nil
}
// Read configuration file or set reasonable defaults if none
func setGlobalConfigVariables(storageDir string, cfgFile string) error {
// Reset config to prevent invalid state in case it's called multiple times
viper.Reset()
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search config in home directory with name "duplicacy-util" (without extension).
viper.AddConfigPath(storageDir)
viper.SetConfigName("duplicacy-util")
}
viper.AutomaticEnv() // read in environment variables that match
// Set some defaults that we can depend on
duplicacyPath = "duplicacy"
globalLockDir = storageDir
globalLogDir = filepath.Join(storageDir, "log")
globalLogFileCount = 5
onStartNotifiers = []Notifier{}
onSkipNotifiers = []Notifier{}
onSuccessNotifiers = []Notifier{}
onFailureNotifiers = []Notifier{}
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
default:
return err
case viper.ConfigFileNotFoundError:
// No configuration file is okay unless we specifically asked for a named file
if cfgFile != "" {
return err
}
return nil
}
}
logMessage(nil, fmt.Sprint("Using global config: ", viper.ConfigFileUsed()))
if configStr := viper.GetString("duplicacypath"); configStr != "" {
duplicacyPath = configStr
}
if configStr := viper.GetString("lockdirectory"); configStr != "" {
globalLockDir = configStr
}
if configStr := viper.GetString("logdirectory"); configStr != "" {
globalLogDir = configStr
}
if configInt := viper.GetInt("logfilecount"); configInt != 0 {
globalLogFileCount = configInt
}
var err error
// Configure notifiers for onStart notification
if configSlice := viper.GetStringSlice("notifications.onStart"); len(configSlice) > 0 {
onStartNotifiers, err = configureNotificationChannel(configSlice, "onStart")
if err != nil {
return err
}
}
// Configure notifiers for onSkip notification
if configSlice := viper.GetStringSlice("notifications.onSkip"); len(configSlice) > 0 {
onSkipNotifiers, err = configureNotificationChannel(configSlice, "onSkip")
if err != nil {
return err
}
}
// Configure notifiers for onSuccess notification
if configSlice := viper.GetStringSlice("notifications.onSuccess"); len(configSlice) > 0 {
onSuccessNotifiers, err = configureNotificationChannel(configSlice, "onSuccess")
if err != nil {
return err
}
}
// Configure notifiers for onFailure notification
if configSlice := viper.GetStringSlice("notifications.onFailure"); len(configSlice) > 0 {
onFailureNotifiers, err = configureNotificationChannel(configSlice, "onFailure")
if err != nil {
return err
}
}
if testNotificationsFlag && len(onStartNotifiers) == 0 && len(onSkipNotifiers) == 0 && len(onSuccessNotifiers) == 0 && len(onFailureNotifiers) == 0 {
return errors.New("No notifiers are configured: Testing notifiers is not valid")
}
return nil
}
func configureNotificationChannel(channels []string, notificationType string) ([]Notifier, error) {
notifiers := []Notifier{}
for _, channel := range channels {
if channel == "email" {
emailNotifier, err := NewEmailNotifier()
if err != nil {
return nil, err
}
if isUniqueNotifier(emailNotifier, notifiers) {
notifiers = append(notifiers, emailNotifier)
}
continue
}
// Return error if invalid notification channel is provided
return nil, fmt.Errorf("Invalid notification channel \"%s\" provided for %sNotifier", channel, notificationType)
}
return notifiers, nil
}
func verifyPathExists(path string) error {
var err error
if _, err = os.Stat(path); err != nil {
return err
}
return nil
}
func hasFailureNotifier() bool {
return len(onFailureNotifiers) > 0
}
// Checks (by type) if notifier is unique
func isUniqueNotifier(notifier Notifier, collection []Notifier) bool {
for _, existing := range collection {
if notifier.(Notifier) == existing.(Notifier) {
return false
}
}
return true
}