This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
103 lines (88 loc) · 2.12 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
package fate
import (
"time"
)
var (
globalConf = defaultConfig()
)
type config struct {
// DefaultP is the default error probability when tempting fate.
DefaultP float64
// PackageP is the per package error probability when tempting fate"
PackageP map[string]float64
// OfficeHours allows limiting errors to office hours.
OfficeHours officeHours
}
type officeHours struct {
Enabled bool
Location *time.Location
HourStart int
HourEnd int
}
// SetConfig sets the global config defined by the functional options.
// It returns an undo function mostly used for testing.
// Note configuring and using Fate instances is advised over global config.
func SetConfig(opts ...option) func() {
conf := defaultConfig()
for _, opt := range opts {
opt(conf)
}
undo := globalConf
globalConf = conf
return func() {
globalConf = undo
}
}
func cloneConfig(conf *config) *config {
c := *conf
c.PackageP = make(map[string]float64)
for key, value := range conf.PackageP {
c.PackageP[key] = value
}
return &c
}
// defaultConfig defines the default config.
func defaultConfig() *config {
return &config{
DefaultP: 1.0 / 1e6,
PackageP: make(map[string]float64),
OfficeHours: officeHours{
Enabled: true,
Location: time.UTC,
HourStart: 9,
HourEnd: 17,
},
}
}
type option func(*config)
// WithDefaultP allows defining the default error probability when tempting fate.
func WithDefaultP(p float64) option {
return func(c *config) {
c.DefaultP = p
}
}
// WithPackageP allows defining the per package error probability when tempting fate.
func WithPackageP(p map[string]float64) option {
return func(c *config) {
c.PackageP = p
}
}
// WithOfficeHours allows limiting errors to office hours.
func WithOfficeHours(loc *time.Location, hourStart, hourEnd int) option {
return func(c *config) {
c.OfficeHours = officeHours{
Enabled: true,
Location: loc,
HourStart: hourStart,
HourEnd: hourEnd,
}
}
}
// WithoutOfficeHours allows disabling limiting of errors to office hours.
func WithoutOfficeHours() option {
return func(c *config) {
c.OfficeHours = officeHours{
Enabled: false,
}
}
}