1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/cli/exec.go
Tim Voronov 5620be211c
Next (#214)
* Renamed DOCUMENT to PAGE

* Added PageLoadParams

* Added PageLoadParams

* Renamed LoadPageParams -> PageLoadParams

* Added support for context.Done() (#201)

* Bug/#189 operators precedence (#202)

* Fixed math operators precedence

* Fixed logical operators precedence

* Fixed array operator

* Added support for parentheses to enforce a different operator evaluation order

* Feature/#200 drivers (#209)

* Added new interfaces

* Renamed dynamic to cdp driver

* Renamed drivers

* Added ELEMENT_EXISTS function (#210)

* Renamed back PAGE to DOCUMENT (#211)

* Added Getter and Setter interfaces
2018-12-21 23:14:41 -05:00

93 lines
1.4 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, err := opts.WithContext(context.Background())
if err != nil {
fmt.Println("Failed to register HTML drivers")
fmt.Println(err)
os.Exit(1)
return
}
ctx, cancel := context.WithCancel(ctx)
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.WithLog(l),
runtime.WithLogLevel(logging.DebugLevel),
runtime.WithParams(opts.Params),
)
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())
}
}