-
Notifications
You must be signed in to change notification settings - Fork 23
/
fix.go
50 lines (42 loc) · 886 Bytes
/
fix.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
package fix
import (
"path/filepath"
"sync"
"github.com/BurntSushi/toml"
"github.com/gobuffalo/packd"
"github.com/pkg/errors"
)
var scenes = map[string]Scenario{}
var moot = &sync.RWMutex{}
func Init(box packd.Walkable) error {
err := box.Walk(func(path string, file packd.File) error {
if filepath.Ext(path) != ".toml" {
return nil
}
x, err := render(file)
if err != nil {
return errors.Wrap(errors.WithStack(err), path)
}
sc := Scenarios{}
_, err = toml.Decode(x, &sc)
if err != nil {
return errors.WithStack(err)
}
moot.Lock()
for _, s := range sc.Scenarios {
scenes[s.Name] = s
}
moot.Unlock()
return nil
})
return err
}
func Find(name string) (Scenario, error) {
moot.RLock()
s, ok := scenes[name]
moot.RUnlock()
if !ok {
return Scenario{}, errors.Errorf("could not find a scenario named %q", name)
}
return s, nil
}