forked from MontFerret/ferret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
86 lines (69 loc) · 1.34 KB
/
exec.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
package cli
import (
"context"
"fmt"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"io/ioutil"
"os"
"os/signal"
"syscall"
)
func ExecFile(pathToFile string, opts Options) {
query, err := ioutil.ReadFile(pathToFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
Exec(string(query), opts)
}
func Exec(query string, opts Options) {
ferret := compiler.New()
prog, err := ferret.Compile(query)
if err != nil {
fmt.Println("Failed to compile the query")
fmt.Println(err)
os.Exit(1)
return
}
l := NewLogger()
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
go func() {
for {
<-c
cancel()
l.Close()
}
}()
var timer *Timer
if opts.ShowTime {
timer = NewTimer()
timer.Start()
}
out, err := prog.Run(
ctx,
runtime.WithBrowser(opts.Cdp),
runtime.WithLog(l),
runtime.WithLogLevel(logging.DebugLevel),
runtime.WithParams(opts.Params),
runtime.WithProxy(opts.Proxy),
runtime.WithUserAgent(opts.UserAgent),
)
if opts.ShowTime {
timer.Stop()
}
if err != nil {
fmt.Println("Failed to execute the query")
fmt.Println(err)
os.Exit(1)
return
}
fmt.Println(string(out))
if opts.ShowTime {
fmt.Println(timer.Print())
}
}