-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat.go
113 lines (91 loc) · 2.74 KB
/
cat.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
package js
import (
"fmt"
"github.com/dop251/goja"
"github.com/shiroyk/cloudcat/plugin"
"github.com/shiroyk/cloudcat/plugin/parser"
"golang.org/x/exp/slog"
)
var attr = slog.String("type", "js")
// Cat an analyzer context
type Cat struct {
ctx *plugin.Context
BaseURL string
URL string `js:"url"`
}
// NewCat returns a new Cat instance
func NewCat(ctx *plugin.Context) *Cat {
return &Cat{ctx, ctx.BaseURL(), ctx.URL()}
}
// Log print the msg to logger
func (c *Cat) Log(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
c.ctx.Logger().Info(Format(call, vm).String(), attr)
return goja.Undefined()
}
// GetVar returns the value associated with this context for key, or nil
// if no value is associated with key.
func (c *Cat) GetVar(call goja.FunctionCall, vm *goja.Runtime) goja.Value {
return vm.ToValue(c.ctx.Value(call.Argument(0).String()))
}
// SetVar value associated with key is val.
func (c *Cat) SetVar(key string, value goja.Value) error {
v, err := Unwrap(value)
if err != nil {
return err
}
c.ctx.SetValue(key, v)
return nil
}
// ClearVar clean all values
func (c *Cat) ClearVar() {
c.ctx.ClearValue()
}
// Cancel this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
func (c *Cat) Cancel() {
c.ctx.Cancel()
}
// GetString gets the string of the content with the given arguments
func (c *Cat) GetString(key string, rule string, content any) (ret string, err error) {
str, err := ToStrings(content)
if err != nil {
return
}
if p, ok := parser.GetParser(key); ok {
return p.GetString(c.ctx, str, rule)
}
return ret, fmt.Errorf("parser %s not found", key)
}
// GetStrings gets the string of the content with the given arguments
func (c *Cat) GetStrings(key string, rule string, content any) (ret []string, err error) {
str, err := ToStrings(content)
if err != nil {
return
}
if p, ok := parser.GetParser(key); ok {
return p.GetStrings(c.ctx, str, rule)
}
return ret, fmt.Errorf("parser %s not found", key)
}
// GetElement gets the string of the content with the given arguments
func (c *Cat) GetElement(key string, rule string, content any) (ret string, err error) {
str, err := ToStrings(content)
if err != nil {
return
}
if p, ok := parser.GetParser(key); ok {
return p.GetElement(c.ctx, str, rule)
}
return ret, fmt.Errorf("parser %s not found", key)
}
// GetElements gets the string of the content with the given arguments
func (c *Cat) GetElements(key string, rule string, content any) (ret []string, err error) {
str, err := ToStrings(content)
if err != nil {
return
}
if p, ok := parser.GetParser(key); ok {
return p.GetElements(c.ctx, str, rule)
}
return ret, fmt.Errorf("parser %s not found", key)
}