/
options.go
95 lines (76 loc) · 1.55 KB
/
options.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
package assets
import (
"fmt"
"strconv"
"strings"
"gnd.la/util/stringutil"
)
type Options map[string]string
func ParseOptions(options string) (Options, error) {
values, err := stringutil.SplitFields(options, ",")
if err != nil {
return nil, fmt.Errorf("error parsing asset options: %s", err)
}
opts := make(Options)
for _, v := range values {
eq := strings.IndexByte(v, '=')
if eq < 0 {
opts[v] = ""
} else {
opts[v[:eq]] = v[eq+1:]
}
}
return opts, nil
}
func (o Options) BoolOpt(key string) bool {
_, ok := o[key]
return ok
}
func (o Options) IntOpt(key string) (int, error) {
v, ok := o[key]
if !ok {
return 0, nil
}
return strconv.Atoi(v)
}
func (o Options) StringOpt(key string) string {
return o[key]
}
func (o Options) String() string {
var values []string
for k, v := range o {
values = append(values, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(values, ",")
}
// Common options
func (o Options) Debug() bool {
return o.BoolOpt("debug")
}
func (o Options) NoDebug() bool {
return o.BoolOpt("nodebug")
}
func (o Options) Top() bool {
return o.BoolOpt("top")
}
func (o Options) Bottom() bool {
return o.BoolOpt("bottom")
}
func (o Options) Async() bool {
return o.BoolOpt("async")
}
func (o Options) Defer() bool {
return o.BoolOpt("defer")
}
func (o Options) Bundle() bool {
return o.BoolOpt("bundle")
}
func (o Options) Bundable() bool {
return o.BoolOpt("bundable")
}
func (o Options) Cdn() bool {
return o.BoolOpt("cdn")
}
func (o Options) Priority() (int, error) {
return o.IntOpt("priority")
}