2018-09-27 04:03:06 +02:00
|
|
|
package main
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
import (
|
2018-09-23 08:05:05 +02:00
|
|
|
"bufio"
|
2018-09-29 03:04:16 +02:00
|
|
|
"encoding/json"
|
2018-09-18 22:42:38 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-09-23 08:05:05 +02:00
|
|
|
"io/ioutil"
|
2018-09-18 22:42:38 +02:00
|
|
|
"os"
|
2018-09-29 03:04:16 +02:00
|
|
|
"strings"
|
2019-03-03 04:34:29 +02:00
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/cli"
|
|
|
|
"github.com/MontFerret/ferret/cli/browser"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
type Params []string
|
|
|
|
|
|
|
|
func (p *Params) String() string {
|
|
|
|
return "[" + strings.Join(*p, ",") + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Params) Set(value string) error {
|
|
|
|
*p = append(*p, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Params) ToMap() (map[string]interface{}, error) {
|
|
|
|
res := make(map[string]interface{})
|
|
|
|
|
|
|
|
for _, entry := range *p {
|
|
|
|
pair := strings.Split(entry, ":")
|
|
|
|
|
|
|
|
if len(pair) < 2 {
|
|
|
|
return nil, core.Error(core.ErrInvalidArgument, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value interface{}
|
|
|
|
key := pair[0]
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(pair[1]), &value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(pair[1])
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
var (
|
2018-10-14 03:36:53 +02:00
|
|
|
version string
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
conn = flag.String(
|
|
|
|
"cdp",
|
2019-03-03 04:34:29 +02:00
|
|
|
"",
|
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",
|
|
|
|
)
|
2018-10-08 03:23:36 +02:00
|
|
|
|
|
|
|
proxyAddress = flag.String(
|
|
|
|
"proxy",
|
|
|
|
"",
|
2018-10-08 04:56:01 +02:00
|
|
|
"address of proxy server to use (only applicable to static pages, proxy for dynamic pages controlled by Chrome)",
|
2018-10-08 03:23:36 +02:00
|
|
|
)
|
2018-10-08 04:18:57 +02:00
|
|
|
|
|
|
|
userAgent = flag.String(
|
|
|
|
"user-agent",
|
|
|
|
"",
|
|
|
|
"set custom user agent. '*' triggers UA generation",
|
|
|
|
)
|
2018-10-08 04:56:01 +02:00
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
showTime = flag.Bool(
|
|
|
|
"time",
|
|
|
|
false,
|
|
|
|
"show how much time was taken to execute a query",
|
|
|
|
)
|
|
|
|
|
2018-10-14 03:36:53 +02:00
|
|
|
showVersion = flag.Bool(
|
2018-10-08 04:56:01 +02:00
|
|
|
"version",
|
|
|
|
false,
|
|
|
|
"show REPL version",
|
|
|
|
)
|
|
|
|
|
|
|
|
help = flag.Bool(
|
|
|
|
"help",
|
|
|
|
false,
|
|
|
|
"show this list",
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2018-09-29 03:04:16 +02:00
|
|
|
var params Params
|
|
|
|
|
|
|
|
flag.Var(
|
|
|
|
¶ms,
|
|
|
|
"param",
|
|
|
|
`query parameter (--param=foo:\"bar\", --param=id:1)`,
|
|
|
|
)
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *help {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-14 03:36:53 +02:00
|
|
|
if *showVersion {
|
|
|
|
fmt.Println(version)
|
2018-09-18 22:42:38 +02:00
|
|
|
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-29 03:04:16 +02:00
|
|
|
p, err := params.ToMap()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-09-26 03:49:42 +02:00
|
|
|
opts := cli.Options{
|
2018-10-08 04:18:57 +02:00
|
|
|
Cdp: cdpConn,
|
|
|
|
Params: p,
|
|
|
|
Proxy: *proxyAddress,
|
|
|
|
UserAgent: *userAgent,
|
2018-10-11 18:39:03 +02:00
|
|
|
ShowTime: *showTime,
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-09-23 08:05:05 +02:00
|
|
|
|
2018-09-23 08:28:26 +02:00
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
|
|
|
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
// check whether the app is getting a query via standard input
|
|
|
|
std := bufio.NewReader(os.Stdin)
|
2018-09-23 08:05:05 +02:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(std)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-26 03:49:42 +02:00
|
|
|
cli.Exec(string(b), opts)
|
2018-09-23 08:05:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// filename was passed
|
|
|
|
if flag.NArg() > 0 {
|
2018-09-26 03:49:42 +02:00
|
|
|
cli.ExecFile(flag.Arg(0), opts)
|
2018-09-23 08:05:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing was passed, run REPL
|
2018-10-14 03:36:53 +02:00
|
|
|
cli.Repl(version, opts)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|