-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconfig.go
More file actions
164 lines (135 loc) · 3.86 KB
/
Copy pathconfig.go
File metadata and controls
164 lines (135 loc) · 3.86 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package config
import (
"fmt"
"regexp"
"strings"
"text/template"
"time"
"unicode"
"unicode/utf8"
"github.com/PaesslerAG/gval"
"go.uber.org/zap"
)
// capitalize returns a string with the first character uppercased.
func capitalize(s string) string {
if s == "" {
return s
}
r, size := utf8.DecodeRuneInString(s)
return string(unicode.ToUpper(r)) + s[size:]
}
var (
exprLang = gval.Full()
// Source: https://github.com/binwiederhier/ntfy/blob/30301c8a7ff9e54ae505daf73a7f1571e7fefae3/user/types.go#L245
allowedTopicRegex = regexp.MustCompile(`^[-_A-Za-z0-9]{1,64}$`)
// TemplateFuncs contains custom functions available in templates.
TemplateFuncs = template.FuncMap{
"split": strings.Split,
"join": strings.Join,
"trim": strings.TrimSpace,
"lower": strings.ToLower,
"upper": strings.ToUpper,
"capitalize": capitalize,
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"replace": func(old, new, s string) string { return strings.ReplaceAll(s, old, new) },
"printf": fmt.Sprintf,
}
)
type Template template.Template
type Expression struct {
Text string
Evaluable gval.Evaluable
}
type Templates struct {
Title *Template `yaml:"title"`
Description *Template `yaml:"description"`
Labels *Template `yaml:"labels"`
Headers map[string]*Template `yaml:"headers"`
}
type Tag struct {
Tag string `yaml:"tag"`
Condition *Expression `yaml:"condition"`
}
type BasicAuth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type StringExpression struct {
Text string
Expression *Expression
}
type Notification struct {
Topic StringExpression `yaml:"topic"`
Priority *StringExpression `yaml:"priority"`
Tags []*Tag `yaml:"tags"`
Templates *Templates `yaml:"templates"`
}
type NtfyAuth struct {
BasicAuth *BasicAuth `yaml:"basic"`
Token *string `yaml:"token"`
}
type Ntfy struct {
BaseURL string `yaml:"baseurl"`
Timeout time.Duration `yaml:"timeout"`
Auth *NtfyAuth `yaml:"auth"`
Notification Notification `yaml:"notification"`
Async bool `yaml:"async"`
}
type HTTP struct {
Addr string `yaml:"addr"`
Auth *BasicAuth `yaml:"auth"`
}
type Config struct {
HTTP *HTTP `yaml:"http"`
Ntfy *Ntfy `yaml:"ntfy"`
Log *zap.Config `yaml:"log"`
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (t *Template) UnmarshalText(text []byte) error {
s := strings.TrimSpace(string(text))
tmpl, err := template.New("").Funcs(TemplateFuncs).Parse(s)
if err != nil {
return err
}
*t = Template(*tmpl)
return nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *Expression) UnmarshalText(text []byte) error {
s := strings.TrimSpace(string(text))
evaluable, err := exprLang.NewEvaluable(s)
if err != nil {
return fmt.Errorf("bad expression: %w", err)
}
*e = Expression{
Text: s,
Evaluable: evaluable,
}
return nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *StringExpression) UnmarshalText(text []byte) error {
s := strings.TrimSpace(string(text))
se := StringExpression{Text: s}
if isExpression(s) {
var expr Expression
if err := expr.UnmarshalText(text); err != nil {
return err
}
se.Expression = &expr
}
*e = se
return nil
}
// Valid reports whether this basic authentication configuration is considered
// valid. Returns false if it is nil, or if the username or password is empty.
func (a *BasicAuth) Valid() bool {
return a != nil && a.Username != "" && a.Password != ""
}
// isExpression reports whether the given string is likely to be an expression by
// checking whether it'd be a valid topic.
func isExpression(s string) bool {
return !allowedTopicRegex.Match([]byte(s))
}