-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
84 lines (66 loc) · 1.49 KB
/
global.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
package aurum
import (
"errors"
"flag"
"sync"
)
const DefaultUpdateFlagName = "update_golden_files"
type globalOptions struct {
mu sync.Mutex
initialized bool
flagSet *flag.FlagSet
flagName string
updatesEnabled bool
}
func (g *globalOptions) init(opts []InitOption) error {
g.mu.Lock()
defer g.mu.Unlock()
if g.initialized {
return errors.New("package can only be initialized once")
}
if g.flagSet == nil {
g.flagSet = flag.CommandLine
}
for _, opt := range opts {
opt.apply(g)
}
if g.flagName != "" {
g.flagSet.BoolVar(&g.updatesEnabled, g.flagName, g.updatesEnabled,
"Update golden test files in-place.")
}
g.initialized = true
return nil
}
func (g *globalOptions) checkUpdatesEnabled() bool {
g.mu.Lock()
defer g.mu.Unlock()
return g.updatesEnabled
}
var global = &globalOptions{
flagName: DefaultUpdateFlagName,
}
// Interface implemented by initialization options.
type InitOption interface {
apply(*globalOptions)
}
type withFlagName string
func (n withFlagName) apply(opt *globalOptions) {
opt.flagName = string(n)
}
// Override the flag name.
func WithFlagName(name string) InitOption {
return withFlagName(name)
}
// Initialize the package and register a command line flag. Must be called
// before parsing flags. Example usage in a test file:
//
// func init() {
// aurum.Init()
// }
//
// If not called the default values are used.
func Init(opts ...InitOption) {
if err := global.init(opts); err != nil {
panic(err)
}
}