-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.go
57 lines (50 loc) · 1.26 KB
/
parsing.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
package main
import (
"errors"
"fmt"
"log"
"os"
)
var parsers = map[string]Parser{}
func init() {
parsers["json"] = JsonParser{}
parsers["yaml"] = YamlParser{}
}
func Validate(whenthen *WhenThen) error {
if whenthen.When.Method == "" || whenthen.When.URL == "" || whenthen.Then.Status == 0 {
return errors.New("a whenthen requires at least a url, a method and a response status to work")
}
return nil
}
func ParseAndStoreWhenThens(configuration *Configuration, storage Store) error {
log.Println("loading config from", configuration.WhenThen)
if configuration.WhenThen == "" {
log.Println("no configuration, starting empty")
return nil
}
for _, parser := range parsers {
file, err := os.Open(configuration.WhenThen)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
items, err := parser.Parse(file)
if err != nil {
log.Println("parsing with ", parser, "failed")
log.Println(err)
continue
}
for _, item := range items {
if err := Validate(item); err != nil {
return err
}
if key, err := storage.Store(*item); err != nil {
return fmt.Errorf("could not store whenthen for %s: %v", key, err)
}
}
return nil
}
return errors.New("SORRY! no parser could parse contents of whenthen file.")
}