-
Notifications
You must be signed in to change notification settings - Fork 3
/
configuration.go
157 lines (122 loc) · 4.16 KB
/
configuration.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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
type ConfigurationFileVersionDetect struct {
Version string
}
type ConfigurationFile struct {
Version string
HttpLogDirectory string `yaml:"httpLogDirectory"`
Sites []ConfigurationFileSite
}
type ConfigurationFileSite struct {
Host string
ApiHost string
Site string
Username string
Password string
Udmp bool
ValidateCertificate bool `yaml:"validateCertificate"`
}
var (
flagShowHelp bool
flagVerboseOutput bool
flagConfigurationFilename string
flagConfigurationType string
flagConfigurationFileVersion string
)
func initConfiguration() {
flag.BoolVar(&flagShowHelp, "help", false, "Show help")
flag.BoolVar(&flagVerboseOutput, "verbose", false, "Show verbose output")
flag.StringVar(&flagConfigurationFilename, "config", "conf/configuration.yaml", "Path to the configuration file to load")
flag.StringVar(&flagConfigurationType, "config-type", "yaml", "The type of configuration file <yaml|json>")
flag.StringVar(&flagConfigurationFileVersion, "config-version", "detect", "The version of the configuration file")
}
func processConfiguration() (*ConfigurationFile, error) {
flag.Parse()
if flagShowHelp {
flag.Usage()
os.Exit(0)
}
configurationFileBody, err := os.ReadFile(flagConfigurationFilename)
if err != nil {
return nil, fmt.Errorf("%s %s", "Error reading configuration file:", err)
}
var cf ConfigurationFile
switch {
case flagConfigurationType == "yaml":
if flagConfigurationFileVersion == "detect" {
var cfvd ConfigurationFileVersionDetect
err = yaml.Unmarshal(configurationFileBody, &cfvd)
if err != nil {
return nil, fmt.Errorf("%s %s", "Error decoding configuration YAML for version detection:", err)
}
flagConfigurationFileVersion = cfvd.Version
}
switch {
case flagConfigurationFileVersion == "0.2":
err = processConfigurationYaml02(configurationFileBody, &cf)
case flagConfigurationFileVersion == "0.1":
return nil, fmt.Errorf("%s %s", "YAML does not support configuration file verion 0.1:", flagConfigurationFileVersion)
default:
return nil, fmt.Errorf("%s %s", "Unsupported YAML configuration file version:", flagConfigurationFileVersion)
}
if err != nil {
return nil, fmt.Errorf("%s %s", "Error decoding configuration YAML:", err)
}
case flagConfigurationType == "json":
if flagConfigurationFileVersion == "detect" {
var cfvd ConfigurationFileVersionDetect
err = json.Unmarshal(configurationFileBody, &cfvd)
if err != nil {
return nil, fmt.Errorf("%s %s", "Error decoding configuration JSON for version detection:", err)
}
flagConfigurationFileVersion = cfvd.Version
}
switch {
case flagConfigurationFileVersion == "0.2":
err = processConfigurationJson02(configurationFileBody, &cf)
case flagConfigurationFileVersion == "0.1":
err = processConfigurationJson01(configurationFileBody, &cf)
default:
return nil, fmt.Errorf("%s %s", "Unsupported JSON configuration file version:", flagConfigurationFileVersion)
}
if err != nil {
return nil, fmt.Errorf("%s %s", "Error decoding configuration JSON:", err)
}
}
if cf.HttpLogDirectory != "" {
_, err := os.Stat(cf.HttpLogDirectory)
if err != nil {
if os.IsNotExist(err) {
err := os.Mkdir(cf.HttpLogDirectory, 0755)
if err != nil {
return nil, fmt.Errorf("%s %s", "Error creating HTTP log directory:", err)
}
} else {
return nil, fmt.Errorf("%s %s", "Error STATing HTTP log directory:", err)
}
}
}
return &cf, nil
}
func processConfigurationYaml02(configurationFileBody []byte, cf *ConfigurationFile) error {
return yaml.Unmarshal(configurationFileBody, &cf)
}
func processConfigurationJson02(configurationFileBody []byte, cf *ConfigurationFile) error {
return json.Unmarshal(configurationFileBody, &cf)
}
func processConfigurationJson01(configurationFileBody []byte, cf *ConfigurationFile) error {
var cfs ConfigurationFileSite
err := json.Unmarshal(configurationFileBody, &cfs)
if err != nil {
return err
}
cf.Sites = []ConfigurationFileSite{cfs}
return nil
}