forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.go
251 lines (199 loc) Β· 5.08 KB
/
script.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package provider
import (
"context"
"errors"
"fmt"
"math"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/jq"
"github.com/evcc-io/evcc/util/request"
"github.com/itchyny/gojq"
"github.com/kballard/go-shellquote"
)
// Script implements shell script-based providers and setters
type Script struct {
log *util.Logger
script string
timeout time.Duration
cache time.Duration
updated time.Time
val string
err error
re *regexp.Regexp
jq *gojq.Query
scale float64
}
func init() {
registry.Add("script", NewScriptProviderFromConfig)
}
// NewScriptProviderFromConfig creates a script provider.
func NewScriptProviderFromConfig(other map[string]interface{}) (Provider, error) {
cc := struct {
Cmd string
Timeout time.Duration
Cache time.Duration
Regex string
Jq string
Scale float64
}{
Timeout: request.Timeout,
Scale: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
p, err := NewScriptProvider(cc.Cmd, cc.Timeout, cc.Scale, cc.Cache)
if err == nil && cc.Regex != "" {
_, err = p.WithRegex(cc.Regex)
}
if err == nil && cc.Jq != "" {
_, err = p.WithJq(cc.Jq)
}
return p, err
}
// NewScriptProvider creates a script provider.
// Script execution is aborted after given timeout.
func NewScriptProvider(script string, timeout time.Duration, scale float64, cache time.Duration) (*Script, error) {
if strings.TrimSpace(script) == "" {
return nil, errors.New("script is required")
}
s := &Script{
log: util.NewLogger("script"),
script: script,
timeout: timeout,
scale: scale,
cache: cache,
}
return s, nil
}
func (p *Script) WithRegex(regex string) (*Script, error) {
re, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("invalid regex '%s': %w", re, err)
}
p.re = re
return p, nil
}
func (p *Script) WithJq(jq string) (*Script, error) {
op, err := gojq.Parse(jq)
if err != nil {
return nil, fmt.Errorf("invalid jq query '%s': %w", jq, err)
}
p.jq = op
return p, nil
}
func (p *Script) exec(script string) (string, error) {
args, err := shellquote.Split(script)
if err != nil {
return "", err
}
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
b, err := cmd.Output()
s := strings.TrimSpace(string(b))
if err != nil {
// use STDOUT if available
var ee *exec.ExitError
if errors.As(err, &ee) {
s = strings.TrimSpace(string(ee.Stderr))
}
p.log.ERROR.Printf("%s: %s", strings.Join(args, " "), s)
return "", err
}
p.log.DEBUG.Printf("%s: %s", strings.Join(args, " "), s)
return s, nil
}
var _ StringProvider = (*Script)(nil)
// StringGetter returns string from exec result. Only STDOUT is considered.
func (p *Script) StringGetter() (func() (string, error), error) {
return func() (string, error) {
if time.Since(p.updated) > p.cache {
p.val, p.err = p.exec(p.script)
p.updated = time.Now()
if p.err == nil && p.re != nil {
m := p.re.FindStringSubmatch(p.val)
if len(m) > 1 {
p.val = m[1] // first submatch
}
}
if p.err == nil && p.jq != nil {
var v interface{}
if v, p.err = jq.Query(p.jq, []byte(p.val)); p.err == nil {
p.val = fmt.Sprintf("%v", v)
}
}
}
return p.val, p.err
}, nil
}
var _ FloatProvider = (*Script)(nil)
// FloatGetter parses float from exec result
func (p *Script) FloatGetter() (func() (float64, error), error) {
g, err := p.StringGetter()
return func() (float64, error) {
s, err := g()
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(s, 64)
if err == nil {
f *= p.scale
}
return f, err
}, err
}
var _ IntProvider = (*Script)(nil)
// IntGetter parses int64 from exec result
func (p *Script) IntGetter() (func() (int64, error), error) {
g, err := p.FloatGetter()
return func() (int64, error) {
f, err := g()
return int64(math.Round(f)), err
}, err
}
var _ BoolProvider = (*Script)(nil)
// BoolGetter parses bool from exec result. "on", "true" and 1 are considered truish.
func (p *Script) BoolGetter() (func() (bool, error), error) {
g, err := p.StringGetter()
return func() (bool, error) {
s, err := g()
if err != nil {
return false, err
}
return util.Truish(s), nil
}, err
}
var _ SetIntProvider = (*Script)(nil)
// IntSetter invokes script with parameter replaced by int value
func (p *Script) IntSetter(param string) (func(int64) error, error) {
// return func to access cached value
return func(i int64) error {
cmd, err := util.ReplaceFormatted(p.script, map[string]interface{}{
param: i,
})
if err == nil {
_, err = p.exec(cmd)
}
return err
}, nil
}
var _ SetBoolProvider = (*Script)(nil)
// BoolSetter invokes script with parameter replaced by bool value
func (p *Script) BoolSetter(param string) (func(bool) error, error) {
// return func to access cached value
return func(b bool) error {
cmd, err := util.ReplaceFormatted(p.script, map[string]interface{}{
param: b,
})
if err == nil {
_, err = p.exec(cmd)
}
return err
}, nil
}