1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/cmd/cli/exec.go

69 lines
1.1 KiB
Go
Raw Normal View History

2018-09-26 03:49:42 +02:00
package cli
2018-09-18 22:42:38 +02:00
import (
"context"
"fmt"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime"
2018-09-28 06:28:33 +02:00
"github.com/MontFerret/ferret/pkg/runtime/logging"
2018-09-18 22:42:38 +02:00
"io/ioutil"
"os"
2018-09-28 06:28:33 +02:00
"os/signal"
"syscall"
2018-09-18 22:42:38 +02:00
)
func ExecFile(pathToFile string, opts Options) {
2018-09-18 22:42:38 +02:00
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) {
2018-09-18 22:42:38 +02:00
ferret := compiler.New()
prog, err := ferret.Compile(query)
2018-09-18 22:42:38 +02:00
if err != nil {
fmt.Println("Failed to compile the query")
fmt.Println(err)
os.Exit(1)
return
}
2018-09-28 06:28:33 +02:00
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(
2018-09-28 06:28:33 +02:00
ctx,
runtime.WithBrowser(opts.Cdp),
2018-09-28 06:28:33 +02:00
runtime.WithLog(l),
runtime.WithLogLevel(logging.DebugLevel),
)
2018-09-18 22:42:38 +02:00
if err != nil {
fmt.Println("Failed to execute the query")
fmt.Println(err)
os.Exit(1)
return
}
fmt.Println(string(out))
}