-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
130 lines (104 loc) · 3.14 KB
/
functions.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
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
package main
import (
"fmt"
"strings"
"sync"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/sirupsen/logrus"
"gopkg.in/irc.v4"
"github.com/Luzifer/go_helpers/v2/fieldcollection"
"github.com/Luzifer/go_helpers/v2/str"
korvike "github.com/Luzifer/korvike/functions"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
var (
korvikeBlacklist = []string{"now"}
sprigBlacklist = []string{"env"}
tplFuncs = newTemplateFuncProvider()
)
type templateFuncProvider struct {
docs []plugins.TemplateFuncDocumentation
funcs map[string]plugins.TemplateFuncGetter
lock *sync.RWMutex
}
func newTemplateFuncProvider() *templateFuncProvider {
out := &templateFuncProvider{
funcs: map[string]plugins.TemplateFuncGetter{},
lock: new(sync.RWMutex),
}
return out
}
func (t *templateFuncProvider) GetFuncMap(m *irc.Message, r *plugins.Rule, fields *fieldcollection.FieldCollection) template.FuncMap {
t.lock.RLock()
defer t.lock.RUnlock()
out := make(template.FuncMap)
for n, fn := range sprig.TxtFuncMap() {
if str.StringInSlice(n, sprigBlacklist) {
continue
}
if out[n] != nil {
panic(fmt.Sprintf("duplicate function: %s (add in sprig)", n))
}
out[n] = fn
}
for n, fg := range t.funcs {
if out[n] != nil {
panic(fmt.Sprintf("duplicate function: %s (add in registration)", n))
}
out[n] = fg(m, r, fields)
}
return out
}
func (t *templateFuncProvider) GetFuncNames() []string {
var out []string
for n := range t.GetFuncMap(nil, nil, nil) {
out = append(out, n)
}
return out
}
func (t *templateFuncProvider) Register(name string, fg plugins.TemplateFuncGetter, doc ...plugins.TemplateFuncDocumentation) {
t.lock.Lock()
defer t.lock.Unlock()
if _, ok := t.funcs[name]; ok {
logrus.Fatalf("Duplicate registration of %q template function", name)
}
t.funcs[name] = fg
if len(doc) > 0 {
doc[0].Name = name
t.docs = append(t.docs, doc[0])
}
}
func init() {
// Register Korvike functions
for n, f := range korvike.GetFunctionMap() {
if str.StringInSlice(n, korvikeBlacklist) {
continue
}
tplFuncs.Register(n, plugins.GenericTemplateFunctionGetter(f))
}
tplFuncs.Register("formatDuration", plugins.GenericTemplateFunctionGetter(func(dur time.Duration, units ...string) string {
dLeft := dur
if len(units) == 0 {
return ""
}
var parts []string
for idx, div := range []time.Duration{time.Hour, time.Minute, time.Second} {
part := dLeft / div
dLeft -= part * div //nolint:durationcheck // One is static, this is fine
if len(units) <= idx || units[idx] == "" {
continue
}
parts = append(parts, fmt.Sprintf("%d %s", part, units[idx]))
}
return strings.Join(parts, ", ")
}), plugins.TemplateFuncDocumentation{
Description: "Returns a formated duration. Pass empty strings to leave out the specific duration part.",
Syntax: "formatDuration <duration> <hours> <minutes> <seconds>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ formatDuration .testDuration "hours" "minutes" "seconds" }} - {{ formatDuration .testDuration "hours" "minutes" "" }}`,
ExpectedOutput: "5 hours, 33 minutes, 12 seconds - 5 hours, 33 minutes",
},
})
}