-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
109 lines (94 loc) · 3.08 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package mmdbmeld
import (
"os"
"gopkg.in/yaml.v3"
)
// Config is the geoip build config.
type Config struct {
Databases []DatabaseConfig `yaml:"databases"`
Defaults DefaultConfig `yaml:"defaults"`
}
// DatabaseConfig holds the config for building one database.
type DatabaseConfig struct {
Name string `yaml:"name"`
MMDB MMDBConfig `yaml:"mmdb"`
Types map[string]string `yaml:"types"`
Inputs []DatabaseInput `yaml:"inputs"`
Output string `yaml:"output"`
Optimize Optimizations `yaml:"optimize"`
Merge MergeConfig `yaml:"merge"`
}
// DefaultConfig holds a subset of DatabaseConfig fields to be used as a default config.
type DefaultConfig struct {
Types map[string]string `yaml:"types"`
Optimize Optimizations `yaml:"optimize"`
Merge MergeConfig `yaml:"merge"`
}
// MMDBConfig holds mmdb specific config.
type MMDBConfig struct {
IPVersion int `yaml:"ipVersion"`
RecordSize int `yaml:"recordSize"`
}
// DatabaseInput holds database input config.
type DatabaseInput struct {
File string `yaml:"file"`
Fields []string `yaml:"fields"`
FieldMap map[string]string `yaml:"fieldMap"`
}
// Optimizations holds optimization config.
type Optimizations struct {
FloatDecimals int `yaml:"floatDecimals"`
ForceIPVersion *bool `yaml:"forceIPVersion"`
MaxPrefix int `yaml:"maxPrefix"`
}
// ForceIPVersionEnabled reports whether ForceIPVersion is set and true.
func (o Optimizations) ForceIPVersionEnabled() bool {
return o.ForceIPVersion != nil && *o.ForceIPVersion
}
// MergeConfig holds merge configuration.
type MergeConfig struct {
AlwaysReplace bool `yaml:"alwaysReplace"`
MergeArrays bool `yaml:"mergeArrays"`
ConditionalResets []ConditionalResetConfig `yaml:"conditionalResets"`
}
// ConditionalResetConfig defines a conditional reset merge config.
type ConditionalResetConfig struct {
IfChanged []string `yaml:"ifChanged"`
Reset []string `yaml:"reset"`
}
// LoadConfig loads a configuration file.
func LoadConfig(filePath string) (*Config, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
config := &Config{}
return config, yaml.Unmarshal(data, config)
}
// ApplyTo applies the default config to the given database config.
func (d DefaultConfig) ApplyTo(c *DatabaseConfig) {
// Add all missing default types.
if c.Types == nil {
c.Types = make(map[string]string)
}
for k, v := range d.Types {
_, ok := c.Types[k]
if !ok {
c.Types[k] = v
}
}
// Apply Optimizations.
if c.Optimize.FloatDecimals == 0 && d.Optimize.FloatDecimals != 0 {
c.Optimize.FloatDecimals = d.Optimize.FloatDecimals
}
if c.Optimize.ForceIPVersion == nil && d.Optimize.ForceIPVersion != nil {
c.Optimize.ForceIPVersion = d.Optimize.ForceIPVersion
}
if c.Optimize.MaxPrefix == 0 && d.Optimize.MaxPrefix != 0 {
c.Optimize.MaxPrefix = d.Optimize.MaxPrefix
}
// Apply Merge Config.
if len(c.Merge.ConditionalResets) == 0 && len(d.Merge.ConditionalResets) != 0 {
c.Merge.ConditionalResets = d.Merge.ConditionalResets
}
}