2018-09-18 22:42:38 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-23 08:05:05 +02:00
|
|
|
"bufio"
|
2018-09-18 22:42:38 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/MontFerret/ferret/cmd/cli/app"
|
|
|
|
"github.com/MontFerret/ferret/pkg/browser"
|
2018-09-23 08:05:05 +02:00
|
|
|
"io/ioutil"
|
2018-09-18 22:42:38 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Version string
|
|
|
|
|
|
|
|
var (
|
|
|
|
help = flag.Bool(
|
|
|
|
"help",
|
|
|
|
false,
|
|
|
|
"show this list",
|
|
|
|
)
|
|
|
|
|
|
|
|
version = flag.Bool(
|
|
|
|
"version",
|
|
|
|
false,
|
|
|
|
"show REPL version",
|
|
|
|
)
|
|
|
|
|
|
|
|
conn = flag.String(
|
|
|
|
"cdp",
|
2018-09-19 03:41:16 +02:00
|
|
|
"http://127.0.0.1:9222",
|
2018-09-23 08:05:05 +02:00
|
|
|
"set CDP address",
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
launchBrowser = flag.Bool(
|
|
|
|
"cdp-launch",
|
|
|
|
false,
|
|
|
|
"launch Chrome",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *help {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if *version {
|
|
|
|
fmt.Println(Version)
|
|
|
|
os.Exit(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cdpConn := *conn
|
|
|
|
|
|
|
|
if cdpConn == "" && *launchBrowser {
|
|
|
|
opts := make([]browser.Option, 0, 2)
|
|
|
|
|
|
|
|
//if *noUserData {
|
|
|
|
// opts = append(opts, browser.WithoutUserDataDir())
|
|
|
|
//}
|
|
|
|
|
|
|
|
// TODO: Make it optional.
|
|
|
|
opts = append(opts, browser.WithoutUserDataDir())
|
|
|
|
|
|
|
|
// we need to launch Chrome instance
|
|
|
|
b, err := browser.Launch(opts...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(fmt.Sprintf("Failed to launch browser:"))
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
cdpConn = b.DebuggingAddress()
|
|
|
|
|
|
|
|
defer b.Close()
|
|
|
|
}
|
|
|
|
|
2018-09-23 08:05:05 +02:00
|
|
|
opts := app.Options{
|
|
|
|
cdpConn,
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-09-23 08:05:05 +02:00
|
|
|
|
|
|
|
// 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)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|