forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_registry.go
102 lines (83 loc) · 2.24 KB
/
config_registry.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
package evergreen
import (
"errors"
"fmt"
"sync"
"github.com/mongodb/grip"
)
// In order to add a new config section:
// 1. modify the struct in config.go to add whatever you need to add
// 2. add the struct to the ConfigSections variable below
// 3. add a copy of the struct you added in 1 to rest/model/admin.go and implement the
// conversion methods. The property name must be exactly the same in the DB/API model
// 4. add it to MockConfig in testutil/config.go for testing, if desired
var ConfigRegistry *ConfigSectionRegistry
func init() {
if err := resetRegistry(); err != nil {
panic(fmt.Sprintf("error registering config sections: %s", err.Error()))
}
}
type ConfigSectionRegistry struct {
mu sync.RWMutex
sections map[string]ConfigSection
}
func resetRegistry() error {
ConfigSections := []ConfigSection{
&AlertsConfig{},
&AmboyConfig{},
&APIConfig{},
&AuthConfig{},
&CedarConfig{},
&CloudProviders{},
&CommitQueueConfig{},
&ContainerPoolsConfig{},
&HostInitConfig{},
&HostJasperConfig{},
&JiraConfig{},
&LoggerConfig{},
&NewRelicConfig{},
&NotifyConfig{},
&RepoTrackerConfig{},
&SchedulerConfig{},
&ServiceFlags{},
&SlackConfig{},
&UIConfig{},
&Settings{},
&JIRANotificationsConfig{},
&TriggerConfig{},
&SpawnHostConfig{},
}
ConfigRegistry = newConfigSectionRegistry()
catcher := grip.NewSimpleCatcher()
for _, section := range ConfigSections {
catcher.Add(ConfigRegistry.registerSection(section.SectionId(), section))
}
return catcher.Resolve()
}
func newConfigSectionRegistry() *ConfigSectionRegistry {
return &ConfigSectionRegistry{
sections: map[string]ConfigSection{},
}
}
func (r *ConfigSectionRegistry) registerSection(id string, section ConfigSection) error {
r.mu.Lock()
defer r.mu.Unlock()
if id == "" {
return errors.New("cannot register a section with no ID")
}
if _, exists := r.sections[id]; exists {
return fmt.Errorf("section %s is already registered", id)
}
r.sections[id] = section
return nil
}
func (r *ConfigSectionRegistry) GetSections() map[string]ConfigSection {
r.mu.RLock()
defer r.mu.RUnlock()
return r.sections
}
func (r *ConfigSectionRegistry) GetSection(id string) ConfigSection {
r.mu.RLock()
defer r.mu.RUnlock()
return r.sections[id]
}