mirror of
https://github.com/MontFerret/ferret.git
synced 2026-06-20 01:17:53 +02:00
#18 Added possiblitiy to read query string from the stdin
This commit is contained in:
+10
-10
@@ -9,7 +9,7 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func Exec(pathToFile, cdpConn string) {
|
||||
func ExecFile(pathToFile string, opts Options) {
|
||||
query, err := ioutil.ReadFile(pathToFile)
|
||||
|
||||
if err != nil {
|
||||
@@ -18,9 +18,13 @@ func Exec(pathToFile, cdpConn string) {
|
||||
return
|
||||
}
|
||||
|
||||
Exec(string(query), opts)
|
||||
}
|
||||
|
||||
func Exec(query string, opts Options) {
|
||||
ferret := compiler.New()
|
||||
|
||||
prog, err := ferret.Compile(string(query))
|
||||
prog, err := ferret.Compile(query)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Failed to compile the query")
|
||||
@@ -29,14 +33,10 @@ func Exec(pathToFile, cdpConn string) {
|
||||
return
|
||||
}
|
||||
|
||||
timer := NewTimer()
|
||||
timer.Start()
|
||||
|
||||
out, err := prog.Run(context.Background(), runtime.WithBrowser(cdpConn))
|
||||
|
||||
timer.Stop()
|
||||
|
||||
fmt.Println(timer.Print())
|
||||
out, err := prog.Run(
|
||||
context.Background(),
|
||||
runtime.WithBrowser(opts.Cdp),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Failed to execute the query")
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package app
|
||||
|
||||
type Options struct {
|
||||
Cdp string
|
||||
}
|
||||
+6
-2
@@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Repl(version, cdpConn string) {
|
||||
func Repl(version string, opts Options) {
|
||||
ferret := compiler.New()
|
||||
|
||||
fmt.Printf("Welcome to Ferret REPL %s\n", version)
|
||||
@@ -28,6 +28,7 @@ func Repl(version, cdpConn string) {
|
||||
defer rl.Close()
|
||||
|
||||
var commands []string
|
||||
|
||||
timer := NewTimer()
|
||||
|
||||
for {
|
||||
@@ -63,7 +64,10 @@ func Repl(version, cdpConn string) {
|
||||
|
||||
timer.Start()
|
||||
|
||||
out, err := program.Run(context.Background(), runtime.WithBrowser(cdpConn))
|
||||
out, err := program.Run(
|
||||
context.Background(),
|
||||
runtime.WithBrowser(opts.Cdp),
|
||||
)
|
||||
|
||||
timer.Stop()
|
||||
fmt.Println(timer.Print())
|
||||
|
||||
+30
-13
@@ -1,10 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/MontFerret/ferret/cmd/cli/app"
|
||||
"github.com/MontFerret/ferret/pkg/browser"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -26,7 +28,7 @@ var (
|
||||
conn = flag.String(
|
||||
"cdp",
|
||||
"http://127.0.0.1:9222",
|
||||
"Chrome DevTools Protocol address",
|
||||
"set CDP address",
|
||||
)
|
||||
|
||||
launchBrowser = flag.Bool(
|
||||
@@ -34,12 +36,6 @@ var (
|
||||
false,
|
||||
"launch Chrome",
|
||||
)
|
||||
|
||||
noUserData = flag.Bool(
|
||||
"no-user-data",
|
||||
false,
|
||||
"do not create a separate location for browser sessions",
|
||||
)
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -83,11 +79,32 @@ func main() {
|
||||
defer b.Close()
|
||||
}
|
||||
|
||||
// no files to execute
|
||||
// run REPL
|
||||
if flag.NArg() == 0 {
|
||||
app.Repl(Version, cdpConn)
|
||||
} else {
|
||||
app.Exec(flag.Arg(0), cdpConn)
|
||||
opts := app.Options{
|
||||
cdpConn,
|
||||
}
|
||||
|
||||
// check whether the app is getting a query via standard input
|
||||
std := bufio.NewReader(os.Stdin)
|
||||
|
||||
if std.Size() > 0 {
|
||||
b, err := ioutil.ReadAll(std)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
app.Exec(string(b), opts)
|
||||
return
|
||||
}
|
||||
|
||||
// filename was passed
|
||||
if flag.NArg() > 0 {
|
||||
app.ExecFile(flag.Arg(0), opts)
|
||||
return
|
||||
}
|
||||
|
||||
// nothing was passed, run REPL
|
||||
app.Repl(Version, opts)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type (
|
||||
|
||||
func newOptions() *Options {
|
||||
return &Options{
|
||||
cdp: "",
|
||||
cdp: "http://127.0.0.1:9222",
|
||||
variables: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user