1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/cli/exec.go
Tim Voronov 957490efec
Feature/#19 proxy (#72)
* #19 Some work on proxy

* Fixed linter issue
2018-10-07 21:23:36 -04:00

71 lines
1.1 KiB
Go

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()
}
}()
out, err := prog.Run(
ctx,
runtime.WithBrowser(opts.Cdp),
runtime.WithLog(l),
runtime.WithLogLevel(logging.DebugLevel),
runtime.WithParams(opts.Params),
runtime.WithProxy(opts.Proxy),
)
if err != nil {
fmt.Println("Failed to execute the query")
fmt.Println(err)
os.Exit(1)
return
}
fmt.Println(string(out))
}