-
Notifications
You must be signed in to change notification settings - Fork 0
/
walker.go
55 lines (44 loc) · 1.14 KB
/
walker.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
package walker
import (
"fmt"
"github.com/pelletier/go-toml"
)
type Redirect struct {
Host string
Path string
Destination string
}
func Walk(tree *toml.Tree) ([]Redirect, error) {
globalRedirects := make([]Redirect, 0)
hostRedirects := make([]Redirect, 0)
for _, key := range tree.Keys() {
if destination, ok := tree.Get(key).(string); ok {
globalRedirects = append(globalRedirects, Redirect{
Path: key,
Destination: destination,
})
} else {
subtree := tree.GetPath([]string{key}).(*toml.Tree)
var err error
hostRedirects, err = walkWithHost(subtree, key, hostRedirects)
if err != nil {
return nil, err
}
}
}
return append(hostRedirects, globalRedirects...), nil
}
func walkWithHost(tree *toml.Tree, host string, redirects []Redirect) ([]Redirect, error) {
for _, path := range tree.Keys() {
destination, ok := tree.Get(path).(string)
if !ok {
return nil, fmt.Errorf("key \"%v\" in host \"%v\" is not a string", path, host)
}
redirects = append(redirects, Redirect{
Host: host,
Path: path,
Destination: destination,
})
}
return redirects, nil
}