-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
89 lines (69 loc) · 1.93 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
package main
import (
"errors"
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type config struct {
InputPath, OutputPath, QuarantinePath string
ScanInterval int
}
const (
inputPathName string = "inputPath"
outputPathName string = "outputPath"
quarantinePathName string = "quarantinePath"
scanIntervalName string = "scanInterval"
)
var cfgFile string
func initConfig() (config, error) {
setupFlags()
err := setupViper()
if err != nil {
return config{}, err
}
return populateConfig()
}
func setupFlags() {
pflag.StringVarP(&cfgFile, "config", "c", "", "path to a configuration file to be used (default is /etc/cap/cap.toml)")
pflag.StringP(inputPathName, "i", "", "the directory containing incoming files to scan")
pflag.StringP(outputPathName, "o", "", "the directory to which safe scanned files are moved")
pflag.StringP(quarantinePathName, "q", "", "the directory to which quarantined files are moved")
pflag.IntP("scanInterval", "s", 0, "the time interval at which the scan occurs")
pflag.Parse()
}
func populateConfig() (config, error) {
conf := config{}
err := viper.Unmarshal(&conf)
if err != nil {
return conf, err
}
if conf.InputPath == "" {
return conf, errors.New("no input directory provided")
}
if conf.OutputPath == "" {
return conf, errors.New("no output directory provided")
}
if conf.QuarantinePath == "" {
return conf, errors.New("no quarantine directory provided")
}
return conf, nil
}
func setupViper() error {
viper.BindPFlags(pflag.CommandLine)
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.AddConfigPath("/etc/cap")
viper.AddConfigPath(".")
viper.SetConfigName("cap")
}
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("Fatal error config file: %w \n", err)
}
logger.Infof("Using configuration file %q", viper.ConfigFileUsed())
viper.AutomaticEnv()
return nil
}