-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
99 lines (78 loc) · 1.86 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
package awses
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/mholt/caddy"
)
var (
ErrTooManyArgs = errors.New("[awses] too many arguments provided")
ErrSingleDomain = errors.New("[awses] a single domain must be provided for the domain directive")
ErrSingleRegion = errors.New("[awses] a single region must be provided for the region directive")
ErrSingleRole = errors.New("[awses] a single role must be provided for the role directive")
)
type Config struct {
Path string
Role string
Region string
Domain string
}
func ParseConfigs(c *caddy.Controller) ([]*Config, error) {
var configs []*Config
for c.Next() {
config := &Config{}
configs = append(configs, config)
// handle args
configArgs := c.RemainingArgs()
switch len(configArgs) {
case 1:
if strings.Trim(configArgs[0], "/") == "" {
config.Path = ""
} else {
config.Path = "/" + strings.Trim(configArgs[0], "/")
}
case 0:
config.Path = ""
default:
return nil, ErrTooManyArgs
}
// handle block directives
for c.NextBlock() {
directive := c.Val()
args := c.RemainingArgs()
switch directive {
case "domain":
if len(args) != 1 {
return nil, ErrSingleDomain
}
config.Domain = args[0]
case "region":
if len(args) != 1 {
return nil, ErrSingleRegion
}
config.Region = args[0]
case "role":
if len(args) != 1 {
return nil, ErrSingleRole
}
config.Role = args[0]
default:
return nil, fmt.Errorf("[awses] invalid directive '%s'", c.Val())
}
}
}
sortedConfigs := sortableConfigs(configs)
sort.Stable(sortedConfigs)
return sortedConfigs, nil
}
type sortableConfigs []*Config
func (c sortableConfigs) Len() int {
return len(c)
}
func (c sortableConfigs) Less(i, j int) bool {
return len(c[i].Path) > len(c[j].Path)
}
func (c sortableConfigs) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}