-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
170 lines (152 loc) · 4.09 KB
/
config.go
File metadata and controls
170 lines (152 loc) · 4.09 KB
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
package main
import (
"io/ioutil"
"log"
"os"
"os/user"
"path"
"github.com/spf13/pflag"
yaml "gopkg.in/yaml.v2"
)
type Configuration struct {
Verbose int
Organization string
Domain string
HardenizeRoot string
HardenizeUser string
HardenizePasswd string
HardenizeWebUser string
HardenizeWebPasswd string
HardenizeWebRoot string
}
func getConfig() *Configuration {
var config Configuration
var conffilename string
// define and parse command line arguments
pflag.StringVar(&conffilename, "conf", "", "Filename to read configuration from")
pflag.CountVarP(&config.Verbose, "verbose", "v", "print more information while running")
pflag.StringVarP(&config.Organization, "org", "o", "", "Organisation ID")
pflag.StringVarP(&config.Domain, "domain", "d", "", "Domain to show detailed results")
pflag.Parse()
var confFromFile *Configuration
if conffilename != "" {
var err error
confFromFile, err = readConfigFile(conffilename)
if err != nil {
panic(err)
}
}
defaultConfig := readDefaultConfigFiles()
return checkConfiguration(joinConfig(defaultConfig, joinConfig(confFromFile, &config)))
}
func readConfigFile(filename string) (*Configuration, error) {
source, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
config := &Configuration{}
err = yaml.Unmarshal(source, config)
if err != nil {
return nil, err
}
if len(config.Organization) > 0 {
log.Fatal("Organisation can only be given at command line")
}
return config, nil
}
func readDefaultConfigFiles() (config *Configuration) {
// config in user home directory
usr, err := user.Current()
if err != nil {
panic(err)
}
fileconfig, err := readConfigFile(path.Join(usr.HomeDir, ".hardblame"))
if err != nil && !os.IsNotExist(err) {
panic(err)
}
config = joinConfig(config, fileconfig)
// done
return
}
func joinConfig(oldConf *Configuration, newConf *Configuration) (config *Configuration) {
if oldConf == nil && newConf == nil {
return nil
}
if oldConf != nil && newConf == nil {
return oldConf
}
if oldConf == nil && newConf != nil {
return newConf
}
// we have two configs, join them
config = &Configuration{}
config.Verbose = newConf.Verbose
if newConf.Organization != "" {
config.Organization = newConf.Organization
} else {
config.Organization = oldConf.Organization
}
if newConf.Domain != "" {
config.Domain = newConf.Domain
} else {
config.Domain = oldConf.Domain
}
if newConf.HardenizeRoot != "" {
config.HardenizeRoot = newConf.HardenizeRoot
} else {
config.HardenizeRoot = oldConf.HardenizeRoot
}
if newConf.HardenizeUser != "" {
config.HardenizeUser = newConf.HardenizeUser
} else {
config.HardenizeUser = oldConf.HardenizeUser
}
if newConf.HardenizePasswd != "" {
config.HardenizePasswd = newConf.HardenizePasswd
} else {
config.HardenizePasswd = oldConf.HardenizePasswd
}
if newConf.HardenizeWebUser != "" {
config.HardenizeWebUser = newConf.HardenizeWebUser
} else {
config.HardenizeWebUser = oldConf.HardenizeWebUser
}
if newConf.HardenizeWebPasswd != "" {
config.HardenizeWebPasswd = newConf.HardenizeWebPasswd
} else {
config.HardenizeWebPasswd = oldConf.HardenizeWebPasswd
}
if newConf.HardenizeWebRoot != "" {
config.HardenizeWebRoot = newConf.HardenizeWebRoot
} else {
config.HardenizeWebRoot = oldConf.HardenizeWebRoot
}
// Done
return config
}
func checkConfiguration(config *Configuration) *Configuration {
if len(config.Organization) == 0 {
log.Fatal("Organization must be given.")
}
// Hardenize Config
if len(config.HardenizeRoot) == 0 {
log.Fatal("Hardenize root url must be given.")
}
if len(config.HardenizeUser) == 0 {
log.Fatal("Hardenize API user must be given.")
}
if len(config.HardenizePasswd) == 0 {
log.Fatal("Hardenize API password must be given.")
}
if len(config.HardenizeWebUser) == 0 {
log.Fatal("Hardenize web user must be given.")
}
if len(config.HardenizeWebPasswd) == 0 {
log.Fatal("Hardenize web password must be given.")
}
if len(config.HardenizeWebRoot) == 0 {
log.Fatal("Hardenize web root url must be given.")
}
// Done
return config
}