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
|
|
|
"github.com/chzyer/readline"
|
2018-09-28 06:28:33 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-09-18 22:42:38 +02:00
|
|
|
"strings"
|
2018-09-28 06:28:33 +02:00
|
|
|
"syscall"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
2018-09-23 08:05:05 +02:00
|
|
|
func Repl(version string, opts Options) {
|
2018-09-18 22:42:38 +02:00
|
|
|
ferret := compiler.New()
|
|
|
|
|
|
|
|
fmt.Printf("Welcome to Ferret REPL %s\n", version)
|
|
|
|
fmt.Println("Please use `exit` or `Ctrl-D` to exit this program.")
|
|
|
|
|
|
|
|
rl, err := readline.NewEx(&readline.Config{
|
|
|
|
Prompt: "> ",
|
|
|
|
InterruptPrompt: "^C",
|
|
|
|
EOFPrompt: "exit",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rl.Close()
|
|
|
|
|
|
|
|
var commands []string
|
2018-09-23 08:28:26 +02:00
|
|
|
var multiline bool
|
2018-09-23 08:05:05 +02:00
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
timer := NewTimer()
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
for {
|
|
|
|
line, err := rl.Readline()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-23 08:28:26 +02:00
|
|
|
if strings.HasPrefix(line, "%") {
|
|
|
|
line = line[1:]
|
|
|
|
|
|
|
|
multiline = !multiline
|
|
|
|
}
|
|
|
|
|
|
|
|
if multiline {
|
|
|
|
commands = append(commands, line)
|
2018-09-18 22:42:38 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
commands = append(commands, line)
|
2018-09-23 08:28:26 +02:00
|
|
|
query := strings.TrimSpace(strings.Join(commands, "\n"))
|
2018-09-18 22:42:38 +02:00
|
|
|
commands = make([]string, 0, 10)
|
|
|
|
|
2018-09-23 08:28:26 +02:00
|
|
|
if query == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
program, err := ferret.Compile(query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to parse the query")
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Start()
|
|
|
|
|
2018-09-23 08:05:05 +02:00
|
|
|
out, err := program.Run(
|
2018-09-28 06:28:33 +02:00
|
|
|
ctx,
|
2018-09-23 08:05:05 +02:00
|
|
|
runtime.WithBrowser(opts.Cdp),
|
2018-09-28 06:28:33 +02:00
|
|
|
runtime.WithLog(l),
|
|
|
|
runtime.WithLogLevel(logging.DebugLevel),
|
2018-09-29 03:04:16 +02:00
|
|
|
runtime.WithParams(opts.Params),
|
2018-09-23 08:05:05 +02:00
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
fmt.Println(timer.Print())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to execute the query")
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
}
|
|
|
|
}
|