-
Notifications
You must be signed in to change notification settings - Fork 2
/
wayang.go
144 lines (118 loc) · 2.92 KB
/
wayang.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
package wayang
import (
"context"
"fmt"
"log"
"os"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/cdp"
"github.com/go-rod/rod/lib/defaults"
"github.com/go-rod/rod/lib/launcher"
"github.com/ysmood/kit"
)
func NewRemoteRunner(client *cdp.Client) *Runner {
ctx, cancel := context.WithCancel(context.Background())
browser := rod.New().Context(ctx, cancel).Client(client).Connect()
page := browser.Page("")
logger := log.New(os.Stdout, "", log.LstdFlags)
return &Runner{
B: browser,
P: page,
ENV: map[string]interface{}{},
Context: ctx,
Canceller: cancel,
Logger: logger,
program: Program{},
}
}
func NewRunner() *Runner {
u := defaults.URL
if defaults.Remote {
if u == "" {
u = "ws://127.0.0.1:9222"
}
return NewRemoteRunner(launcher.NewRemote(u).Client())
}
if u == "" {
var err error
u, err = launcher.New().LaunchE()
kit.E(err)
}
return NewRemoteRunner(cdp.New(u))
}
func (parent *Runner) RunProgram(program Program) (interface{}, *RuntimeError) {
parent.program = program
var res interface{}
for i, action := range parent.program.Steps {
source := fmt.Sprintf("root[%d]", i)
res = parent.runAction(action, source)
if err, ok := res.(RuntimeError); ok {
return nil, &err
}
}
return res, nil
}
func RunProgram(program Program) (interface{}, *RuntimeError) {
return NewRunner().RunProgram(program)
}
func RunActions(actions []Action) (interface{}, *RuntimeError) {
return RunProgram(Program{
Steps: actions,
})
}
func (parent *Runner) RunActions(actions []Action) (interface{}, *RuntimeError) {
return parent.RunProgram(Program{
Steps: actions,
})
}
func RunAction(action Action) (interface{}, *RuntimeError) {
return RunProgram(Program{
Steps: []Action{action},
})
}
func (parent *Runner) RunAction(action Action) (interface{}, *RuntimeError) {
return parent.RunProgram(Program{
Steps: []Action{action},
})
}
func (parent *Runner) Close() {
parent.B.Close()
parent.Canceller()
}
func (parent *Runner) Info(message interface{}) {
parent.Logger.Printf(`level=info msg=%v`, message)
}
func (parent *Runner) Error(message interface{}) {
parent.Logger.Printf(`level=error msg=%s`, message)
}
func (re *RuntimeError) Action() Action {
return re.action
}
func (re *RuntimeError) Source() string {
return re.source
}
func (re *RuntimeError) ErrorRaw() interface{} {
return re.err
}
func (re *RuntimeError) Error() string {
return fmt.Sprintln(re.err)
}
func (re *RuntimeError) Dump() string {
return kit.Sdump(re)
}
func (re *RuntimeError) Log() {
msg := kit.Sdump(re.err)
re.parent.Logger.Printf(`level="error" msg="%s"`, msg)
}
func (re *RuntimeError) Print() {
fmt.Println(kit.Sdump(re.err))
}
func (re *RuntimeError) Stack() string {
return string(re.stack)
}
func (re *RuntimeError) LogStack() {
re.parent.Logger.Printf(`level="error" msg="%s"`, string(re.stack))
}
func (re *RuntimeError) PrintStack() {
fmt.Println(string(re.stack))
}