From 7990c308cbd10c7628b74115a445842efbfa54fb Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Thu, 1 Apr 2021 17:01:48 -0400 Subject: [PATCH 1/4] Removed old CLI --- .github/workflows/release.yml | 38 ------- .goreleaser.yml | 46 -------- Makefile | 27 +---- cli/autocompleter.go | 50 --------- cli/browser/browser.go | 72 ------------- cli/browser/flags.go | 124 --------------------- cli/browser/helpers.go | 42 -------- cli/browser/launcher.go | 124 --------------------- cli/browser/options.go | 85 --------------- cli/exec.go | 85 --------------- cli/logger.go | 14 --- cli/options.go | 50 --------- cli/repl.go | 149 ------------------------- cli/timer.go | 27 ----- e2e/cli.go | 145 +++++++++++++++++++++++++ ferret.go | 48 +++++++++ main.go | 197 ---------------------------------- pkg/drivers/container.go | 58 ++++++++++ 18 files changed, 255 insertions(+), 1126 deletions(-) delete mode 100644 .github/workflows/release.yml delete mode 100644 .goreleaser.yml delete mode 100644 cli/autocompleter.go delete mode 100644 cli/browser/browser.go delete mode 100644 cli/browser/flags.go delete mode 100644 cli/browser/helpers.go delete mode 100644 cli/browser/launcher.go delete mode 100644 cli/browser/options.go delete mode 100644 cli/exec.go delete mode 100644 cli/logger.go delete mode 100644 cli/options.go delete mode 100644 cli/repl.go delete mode 100644 cli/timer.go create mode 100644 e2e/cli.go create mode 100644 ferret.go delete mode 100644 main.go create mode 100644 pkg/drivers/container.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 004fe671..00000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: Release - -on: - release: - types: - - created - -jobs: - goreleaser: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.16 - - name: Set up ANTLR - env: - ANTLR_VERSION: 4.9 - run: | - sudo curl -o /usr/local/lib/antlr-${ANTLR_VERSION}-complete.jar https://www.antlr.org/download/antlr-${ANTLR_VERSION}-complete.jar - export CLASSPATH=".:/usr/local/lib/antlr-${ANTLR_VERSION}-complete.jar:$CLASSPATH" - mkdir $HOME/antlr-bin - echo -e '#!/bin/bash\njava -jar /usr/local/lib/antlr-4.9-complete.jar "$@"' > $HOME/antlr-bin/antlr - echo -e '#!/bin/bash\njava org.antlr.v4.gui.TestRig "$@"' > $HOME/antlr-bin/grun - chmod +x $HOME/antlr-bin/* - sudo ln -s $HOME/antlr-bin/antlr /usr/local/bin/antlr - antlr - - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v2 - with: - version: latest - args: release --rm-dist - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yml b/.goreleaser.yml deleted file mode 100644 index cd6a0a79..00000000 --- a/.goreleaser.yml +++ /dev/null @@ -1,46 +0,0 @@ -# This is an example goreleaser.yaml file with some sane defaults. -# Make sure to check the documentation at http://goreleaser.com -before: - hooks: - - make - -builds: -- env: - - CGO_ENABLED=0 - goos: - - linux - - darwin - - windows - goarch: - - amd64 - - arm64 - ldflags: - - -s -w -X main.version={{ .Version }} - -archives: - - name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' - replacements: - darwin: darwin - linux: linux - windows: windows - 386: i386 - amd64: x86_64 - # Can be used to change the archive formats for specific GOOSs. - # Most common use case is to archive as zip on Windows. - # Default is empty. - format_overrides: - - goos: windows - format: zip - -checksum: - name_template: '{{ .ProjectName }}_checksums.txt' - -snapshot: - name_template: "{{ .Tag }}-next" - -changelog: - sort: asc - filters: - exclude: - - '^docs:' - - '^test:' diff --git a/Makefile b/Makefile index 6b3241e4..0350e187 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,7 @@ .PHONY: build install compile test e2e doc fmt lint vet release -export GOPATH -export GO111MODULE=on - -VERSION ?= $(shell git describe --tags --always --dirty) -RELEASE_VERSION ?= $(version) DIR_BIN = ./bin DIR_PKG = ./pkg -DIR_CLI = ./cli DIR_E2E = ./e2e default: build @@ -19,8 +13,7 @@ install: compile: go build -race -v -o ${DIR_BIN}/ferret \ - -ldflags "-X main.version=${VERSION}" \ - ./main.go + ${DIR_E2E}/cli.go test: go test -race ${DIR_PKG}/... @@ -30,7 +23,7 @@ cover: curl -s https://codecov.io/bash | bash e2e: - lab --timeout=120 --times=5 --concurrency=1 --wait=http://127.0.0.1:9222/json/version --runtime=bin://./bin/ferret --files=./e2e/tests --cdn=./e2e/pages/dynamic --cdn=./e2e/pages/static + lab --timeout=120 --attempts=5 --concurrency=1 --wait=http://127.0.0.1:9222/json/version --runtime=bin://./bin/ferret --files=./e2e/tests --cdn=./e2e/pages/dynamic --cdn=./e2e/pages/static bench: go test -run=XXX -bench=. ${DIR_PKG}/... @@ -43,7 +36,7 @@ doc: # http://golang.org/cmd/go/#hdr-Run_gofmt_on_package_sources fmt: - go fmt ${DIR_CLI}/... ${DIR_PKG}/... + go fmt ${DIR_PKG}/... # https://github.com/mgechev/revive # go get github.com/mgechev/revive @@ -53,16 +46,4 @@ lint: # http://godoc.org/code.google.com/p/go.tools/cmd/vet # go get code.google.com/p/go.tools/cmd/vet vet: - go vet ${DIR_CLI}/... ${DIR_PKG}/... - -release: -ifeq ($(RELEASE_VERSION), ) - $(error "Release version is required (version=x)") -else ifeq ($(GITHUB_TOKEN), ) - $(error "GitHub token is required (GITHUB_TOKEN)") -else - rm -rf ./dist && \ - git tag -a v$(RELEASE_VERSION) -m "New $(RELEASE_VERSION) version" && \ - git push origin v$(RELEASE_VERSION) && \ - goreleaser -endif + go vet ${DIR_PKG}/... diff --git a/cli/autocompleter.go b/cli/autocompleter.go deleted file mode 100644 index 27c59f94..00000000 --- a/cli/autocompleter.go +++ /dev/null @@ -1,50 +0,0 @@ -package cli - -import ( - "strings" - - "github.com/derekparker/trie" -) - -// AutoCompleter autocompletes queries -// into the REPL. -// Implements AutoCompleter interface from -// github.com/chzyer/readline -type AutoCompleter struct { - coreFuncs *trie.Trie -} - -func NewAutoCompleter(functions []string) *AutoCompleter { - coreFuncs := trie.New() - - for _, function := range functions { - coreFuncs.Add(function, function) - } - - return &AutoCompleter{ - coreFuncs: coreFuncs, - } -} - -// Do implements method of AutoCompleter interface -func (ac *AutoCompleter) Do(line []rune, pos int) (newLine [][]rune, length int) { - lineStr := string(line) - tokens := strings.Split(lineStr, " ") - token := tokens[len(tokens)-1] - - // if remove this check, than - // on any empty string will return - // all available functions - if token == "" { - return newLine, pos - } - - for _, fn := range ac.coreFuncs.PrefixSearch(token) { - // cuts a piece of word that is already written - // in the repl - withoutPre := []rune(fn)[len(token):] - newLine = append(newLine, withoutPre) - } - - return newLine, pos -} diff --git a/cli/browser/browser.go b/cli/browser/browser.go deleted file mode 100644 index b352106c..00000000 --- a/cli/browser/browser.go +++ /dev/null @@ -1,72 +0,0 @@ -package browser - -import ( - "os" - "os/exec" - "runtime" - - "github.com/pkg/errors" -) - -type Browser struct { - cmd *exec.Cmd - flags Flags -} - -func (b *Browser) Flags() Flags { - return b.flags -} - -func (b *Browser) DebuggingAddress() string { - if !b.Flags().Has("remote-debugging-address") { - b.Flags().Set("remote-debugging-address", "http://0.0.0.0:9222") - } - - value, _ := b.Flags().Get("remote-debugging-address") - - return value.(string) -} - -func (b *Browser) DebuggingPort() int { - if !b.Flags().Has("remote-debugging-port") { - b.Flags().Set("remote-debugging-port", 9222) - } - - value, _ := b.Flags().Get("remote-debugging-port") - - return value.(int) -} - -func (b *Browser) Close() error { - var err error - - if runtime.GOOS != goosWindows { - err = b.cmd.Process.Signal(os.Interrupt) - } else { - err = b.cmd.Process.Kill() - } - - if err != nil { - return err - } - - _, err = b.cmd.Process.Wait() - - if err != nil { - return errors.Wrap(err, "error waiting for process exit, result unknown") - } - - tmpDir, err := b.flags.GetString("user-data-dir") - - if err != nil { - return nil - } - - err = os.RemoveAll(tmpDir) - - if err != nil { - return err - } - - return nil -} diff --git a/cli/browser/flags.go b/cli/browser/flags.go deleted file mode 100644 index 7288e4d1..00000000 --- a/cli/browser/flags.go +++ /dev/null @@ -1,124 +0,0 @@ -package browser - -import ( - "fmt" - "sort" - "strings" - - "github.com/pkg/errors" -) - -type Flags map[string]interface{} - -func (flags Flags) Get(arg string) (interface{}, error) { - var values interface{} - var err error - - if !flags.Has(arg) { - err = errors.Errorf("The specified argument '%s' does not exist", arg) - } else { - values = flags[arg] - } - - return values, err -} - -func (flags Flags) GetString(arg string) (string, error) { - found, err := flags.Get(arg) - - if err != nil { - return "", err - } - - str, ok := found.(string) - - if ok { - return str, nil - } - - return "", nil -} - -func (flags Flags) GetInt(arg string) (int, error) { - found, err := flags.Get(arg) - - if err != nil { - return 0, err - } - - num, ok := found.(int) - - if ok { - return num, nil - } - - return 0, nil -} - -func (flags Flags) Has(arg string) bool { - _, exists := flags[arg] - - return exists -} - -func (flags Flags) List() []string { - orderedFlags := make([]string, 0, 10) - - for arg := range flags { - orderedFlags = append(orderedFlags, arg) - } - - sort.Strings(orderedFlags) - - list := make([]string, len(orderedFlags)) - - for i, arg := range orderedFlags { - val, err := flags.Get(arg) - - if err != nil { - continue - } - - switch v := val.(type) { - case int: - arg = fmt.Sprintf("--%s=%d", arg, v) - case string: - arg = fmt.Sprintf("--%s=%s", arg, v) - default: - arg = fmt.Sprintf("--%s", arg) - } - - list[i] = arg - } - - return list -} - -func (flags Flags) Set(arg string, value interface{}) (err error) { - if value == nil { - if _, ok := flags[arg]; !ok { - flags[arg] = nil - } - } - - if value != nil { - switch value.(type) { - case int: - flags[arg] = value - case string: - flags[arg] = value - default: - return errors.Errorf("Invalid data type '%T' for argument %s: %+v", value, arg, value) - } - } - - return nil -} - -func (flags Flags) SetN(arg string) (err error) { - return flags.Set(arg, nil) -} - -func (flags Flags) String() string { - return strings.Join(flags.List(), " ") -} diff --git a/cli/browser/helpers.go b/cli/browser/helpers.go deleted file mode 100644 index 818b82d1..00000000 --- a/cli/browser/helpers.go +++ /dev/null @@ -1,42 +0,0 @@ -package browser - -import ( - "os" - "os/exec" - "runtime" -) - -func resolveExecutablePath() (path string) { - - switch runtime.GOOS { - case goosDarwin: - for _, c := range []string{ - "/Applications/Google Chrome Canary.app", - "/Applications/Google Chrome.app", - } { - // MacOS apps are actually folders - info, err := os.Stat(c) - if err == nil && info.IsDir() { - path = c - break - } - } - - case goosLinux: - for _, c := range []string{ - "headless_shell", - "chromium", - "google-chrome-beta", - "google-chrome-unstable", - "google-chrome-stable"} { - if _, err := exec.LookPath(c); err == nil { - path = c - break - } - } - - case goosWindows: - } - - return -} diff --git a/cli/browser/launcher.go b/cli/browser/launcher.go deleted file mode 100644 index c2a5acc3..00000000 --- a/cli/browser/launcher.go +++ /dev/null @@ -1,124 +0,0 @@ -package browser - -import ( - "io/ioutil" - "os" - "os/exec" - "path/filepath" - "runtime" - - "github.com/pkg/errors" -) - -func Launch(setters ...Option) (*Browser, error) { - opts := &Options{ - headless: false, - debuggingAddress: "0.0.0.0", - debuggingPort: 9222, - } - - for _, setter := range setters { - setter(opts) - } - - var flags Flags - - if !opts.ignoreDefaultArgs { - flags = DefaultFlags() - } else { - flags = Flags{} - } - - flags.Set("remote-debugging-port", opts.debuggingPort) - - if opts.devtools { - flags.SetN("auto-open-devtools-for-tabs") - } - - if opts.headless { - flags.SetN("headless") - flags.SetN("hide-scrollbars") - flags.SetN("mute-audio") - } - - if runtime.GOOS == goosWindows { - flags.SetN("disable-gpu") - } - - temporaryUserDataDir := opts.userDataDir - - if temporaryUserDataDir == "" && !opts.noUserDataDir { - dirName, err := ioutil.TempDir(os.TempDir(), "ferret_dev_profile-") - - if err != nil { - return nil, err - } - - temporaryUserDataDir = dirName - } - - workDir := filepath.Join(os.TempDir(), "ferret-chrome") - - err := os.MkdirAll(workDir, 0700) - - if err != nil { - return nil, errors.Errorf("cannot create working directory '%s'", workDir) - } - - if temporaryUserDataDir != "" { - flags.Set("user-data-dir", temporaryUserDataDir) - } - - chromeExecutable := opts.executablePath - - if chromeExecutable == "" { - chromeExecutable = resolveExecutablePath() - - if chromeExecutable == "" { - return nil, errors.New("Chrome not found") - } - } - - execArgs := []string{chromeExecutable, "--args"} - execArgs = append(execArgs, flags.List()...) - - cmd := exec.Command("open", execArgs...) - cmd.Dir = workDir - - err = cmd.Start() - - if err != nil { - return nil, err - } - - if opts.dumpio { - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - } - - return &Browser{cmd, flags}, nil -} - -func DefaultFlags() Flags { - return Flags{ - "disable-background-networking": nil, - "disable-background-timer-throttling": nil, - "disable-breakpad": nil, - "disable-client-side-phishing-detection": nil, - "disable-default-apps": nil, - "disable-dev-shm-usage": nil, - "disable-extensions": nil, - "disable-features=site-per-process": nil, - "disable-hang-monitor": nil, - "disable-popup-blocking": nil, - "disable-prompt-on-repost": nil, - "disable-sync": nil, - "disable-translate": nil, - "metrics-recording-only": nil, - "no-first-run": nil, - "safebrowsing-disable-auto-update": nil, - "enable-automation": nil, - "password-store=basic": nil, - "use-mock-keychain": nil, - } -} diff --git a/cli/browser/options.go b/cli/browser/options.go deleted file mode 100644 index a714e6c9..00000000 --- a/cli/browser/options.go +++ /dev/null @@ -1,85 +0,0 @@ -package browser - -type ( - Option func(opts *Options) - - Options struct { - debuggingPort int - debuggingAddress string - ignoreDefaultArgs bool - executablePath string - ignoreHTTPSErrors bool - slowMo bool - dumpio bool - headless bool - devtools bool - userDataDir string - noUserDataDir bool - } -) - -const ( - goosWindows = "windows" - goosLinux = "linux" - goosDarwin = "darwin" -) - -func WithoutDefaultArgs() Option { - return func(opts *Options) { - opts.ignoreDefaultArgs = true - } -} - -func WithCustomInstallation(executablePath string) Option { - return func(opts *Options) { - opts.executablePath = executablePath - } -} - -func WithIgnoredHTTPSErrors() Option { - return func(opts *Options) { - opts.ignoreHTTPSErrors = true - } -} - -func WithSlowMo() Option { - return func(opts *Options) { - opts.slowMo = true - } -} - -func WithIO() Option { - return func(opts *Options) { - opts.dumpio = true - } -} - -func WithHeadless() Option { - return func(opts *Options) { - opts.headless = true - } -} - -func WithDevtools() Option { - return func(opts *Options) { - opts.devtools = true - } -} - -func WithDebugginPort(num int) Option { - return func(opts *Options) { - opts.debuggingPort = num - } -} - -func WithUserDataDir(str string) Option { - return func(opts *Options) { - opts.userDataDir = str - } -} - -func WithoutUserDataDir() Option { - return func(opts *Options) { - opts.noUserDataDir = true - } -} diff --git a/cli/exec.go b/cli/exec.go deleted file mode 100644 index c625ff8d..00000000 --- a/cli/exec.go +++ /dev/null @@ -1,85 +0,0 @@ -package cli - -import ( - "context" - "fmt" - "io/ioutil" - "os" - "os/signal" - - "github.com/MontFerret/ferret/pkg/compiler" - "github.com/MontFerret/ferret/pkg/runtime" - "github.com/MontFerret/ferret/pkg/runtime/logging" -) - -func ExecFile(pathToFile string, opts Options) { - query, err := ioutil.ReadFile(pathToFile) - - if err != nil { - fmt.Fprintln(os.Stderr, 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.Fprintln(os.Stderr, "Failed to compile the query") - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - return - } - - l := NewLogger() - - ctx, cancel := opts.WithContext(context.Background()) - - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - signal.Notify(c, os.Kill) - - 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.Fprintln(os.Stderr, "Failed to execute the query") - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - return - } - - fmt.Println(string(out)) - - if opts.ShowTime { - fmt.Println(timer.Print()) - } -} diff --git a/cli/logger.go b/cli/logger.go deleted file mode 100644 index 6ff6c084..00000000 --- a/cli/logger.go +++ /dev/null @@ -1,14 +0,0 @@ -package cli - -import ( - "github.com/natefinch/lumberjack" -) - -func NewLogger() *lumberjack.Logger { - l := &lumberjack.Logger{ - Filename: "./ferret.log", - MaxSize: 100, - } - - return l -} diff --git a/cli/options.go b/cli/options.go deleted file mode 100644 index 9804bd81..00000000 --- a/cli/options.go +++ /dev/null @@ -1,50 +0,0 @@ -package cli - -import ( - "context" - - "github.com/MontFerret/ferret/pkg/drivers" - "github.com/MontFerret/ferret/pkg/drivers/cdp" - "github.com/MontFerret/ferret/pkg/drivers/http" -) - -type Options struct { - Cdp string - Params map[string]interface{} - Proxy string - UserAgent string - ShowTime bool - KeepCookies bool -} - -func (opts Options) WithContext(ctx context.Context) (context.Context, context.CancelFunc) { - httpDriver := http.NewDriver( - http.WithProxy(opts.Proxy), - http.WithUserAgent(opts.UserAgent), - ) - - ctx = drivers.WithContext( - ctx, - httpDriver, - drivers.AsDefault(), - ) - - cdpOpts := []cdp.Option{ - cdp.WithAddress(opts.Cdp), - cdp.WithProxy(opts.Proxy), - cdp.WithUserAgent(opts.UserAgent), - } - - if opts.KeepCookies { - cdpOpts = append(cdpOpts, cdp.WithKeepCookies()) - } - - cdpDriver := cdp.NewDriver(cdpOpts...) - - ctx = drivers.WithContext( - ctx, - cdpDriver, - ) - - return context.WithCancel(ctx) -} diff --git a/cli/repl.go b/cli/repl.go deleted file mode 100644 index cd50472e..00000000 --- a/cli/repl.go +++ /dev/null @@ -1,149 +0,0 @@ -package cli - -import ( - "context" - "fmt" - "os" - "os/signal" - "strings" - - "github.com/MontFerret/ferret/pkg/parser/fql" - - "github.com/MontFerret/ferret/pkg/compiler" - "github.com/MontFerret/ferret/pkg/runtime" - "github.com/MontFerret/ferret/pkg/runtime/logging" - "github.com/chzyer/readline" -) - -func Repl(version string, opts Options) { - 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", - AutoComplete: NewAutoCompleter( - append( - fqlLiterals(), - ferret.RegisteredFunctions()..., - )), - }) - - if err != nil { - panic(err) - } - - defer rl.Close() - - var commands []string - var multiline bool - - var timer *Timer - - if opts.ShowTime { - timer = NewTimer() - } - - l := NewLogger() - - ctx, cancel := opts.WithContext(context.Background()) - - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - - exit := func() { - cancel() - l.Close() - } - - go func() { - for { - <-c - exit() - } - }() - - for { - line, err := rl.Readline() - - if err != nil { - break - } - - line = strings.TrimSpace(line) - - if line == "" { - continue - } - - if strings.HasPrefix(line, "%") { - line = line[1:] - - multiline = !multiline - } - - if multiline { - commands = append(commands, line) - continue - } - - commands = append(commands, line) - query := strings.TrimSpace(strings.Join(commands, "\n")) - commands = make([]string, 0, 10) - - if query == "" { - continue - } - - if query == "exit" { - exit() - os.Exit(0) - return - } - - program, err := ferret.Compile(query) - - if err != nil { - fmt.Println("Failed to parse the query") - fmt.Println(err) - continue - } - - if opts.ShowTime { - timer.Start() - } - - out, err := program.Run( - ctx, - runtime.WithLog(l), - runtime.WithLogLevel(logging.DebugLevel), - runtime.WithParams(opts.Params), - ) - - if err != nil { - fmt.Println("Failed to execute the query") - fmt.Println(err) - continue - } - - fmt.Println(string(out)) - - if opts.ShowTime { - timer.Stop() - fmt.Println(timer.Print()) - } - } -} - -func fqlLiterals() (literals []string) { - lns := fql.NewFqlLexer(nil).LiteralNames - - for _, ln := range lns { - literals = append(literals, strings.Trim(ln, "'")) - } - - return -} diff --git a/cli/timer.go b/cli/timer.go deleted file mode 100644 index d56f952e..00000000 --- a/cli/timer.go +++ /dev/null @@ -1,27 +0,0 @@ -package cli - -import ( - "fmt" - "time" -) - -type Timer struct { - start time.Time - duration time.Duration -} - -func NewTimer() *Timer { - return &Timer{} -} - -func (t *Timer) Start() { - t.start = time.Now() -} - -func (t *Timer) Stop() { - t.duration = time.Since(t.start) -} - -func (t *Timer) Print() string { - return fmt.Sprintf("%f seconds", t.duration.Seconds()) -} diff --git a/e2e/cli.go b/e2e/cli.go new file mode 100644 index 00000000..c2dda925 --- /dev/null +++ b/e2e/cli.go @@ -0,0 +1,145 @@ +package main + +import ( + "bufio" + "context" + "encoding/json" + "flag" + "fmt" + "github.com/MontFerret/ferret/pkg/runtime" + "github.com/MontFerret/ferret/pkg/runtime/core" + "io/ioutil" + "os" + "strings" + + "github.com/MontFerret/ferret" + "github.com/MontFerret/ferret/pkg/drivers" + "github.com/MontFerret/ferret/pkg/drivers/cdp" + "github.com/MontFerret/ferret/pkg/drivers/http" +) + +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.SplitN(entry, ":", 2) + + 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 +} + +var ( + conn = flag.String( + "cdp", + "", + "set CDP address", + ) +) + +func main() { + var params Params + + flag.Var( + ¶ms, + "param", + `query parameter (--param=foo:\"bar\", --param=id:1)`, + ) + + flag.Parse() + + var query string + + 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) + + b, err := ioutil.ReadAll(std) + + if err != nil { + fmt.Println(err) + os.Exit(1) + return + } + + query = string(b) + } else if flag.NArg() > 0 { + content, err := os.ReadFile(flag.Arg(0)) + + if err != nil { + fmt.Println(err) + os.Exit(1) + return + } + + query = string(content) + } else { + fmt.Println(flag.NArg()) + fmt.Println("Missed file") + os.Exit(1) + } + + p, err := params.ToMap() + + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if err := execFile(query, p); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func execFile(query string, params map[string]interface{}) error { + ctx := drivers.WithContext( + context.Background(), + http.NewDriver(), + drivers.AsDefault(), + ) + + ctx = drivers.WithContext( + ctx, + cdp.NewDriver(cdp.WithAddress(*conn)), + ) + + i := ferret.New() + out, err := i.Exec(ctx, query, runtime.WithParams(params)) + + if err != nil { + return err + } + + fmt.Println(string(out)) + + return nil +} diff --git a/ferret.go b/ferret.go new file mode 100644 index 00000000..7f151fc2 --- /dev/null +++ b/ferret.go @@ -0,0 +1,48 @@ +package ferret + +import ( + "context" + + "github.com/MontFerret/ferret/pkg/compiler" + "github.com/MontFerret/ferret/pkg/drivers" + "github.com/MontFerret/ferret/pkg/runtime" + "github.com/MontFerret/ferret/pkg/runtime/core" +) + +type Instance struct { + compiler *compiler.Compiler + drivers *drivers.Container +} + +func New() *Instance { + return &Instance{ + compiler: compiler.New(), + drivers: drivers.NewContainer(), + } +} + +func (i *Instance) Functions() core.Namespace { + return i.compiler +} + +func (i *Instance) Drivers() *drivers.Container { + return i.drivers +} + +func (i *Instance) Compile(query string) (*runtime.Program, error) { + return i.compiler.Compile(query) +} + +func (i *Instance) Exec(ctx context.Context, query string, opts ...runtime.Option) ([]byte, error) { + p, err := i.Compile(query) + + if err != nil { + return nil, err + } + + for _, drv := range i.drivers.RegisteredDrivers() { + ctx = drivers.WithContext(ctx, drv) + } + + return p.Run(ctx, opts...) +} diff --git a/main.go b/main.go deleted file mode 100644 index f876d0ee..00000000 --- a/main.go +++ /dev/null @@ -1,197 +0,0 @@ -package main - -import ( - "bufio" - "encoding/json" - "flag" - "fmt" - "io/ioutil" - "os" - "strings" - - "github.com/MontFerret/ferret/cli" - "github.com/MontFerret/ferret/cli/browser" - "github.com/MontFerret/ferret/pkg/runtime/core" -) - -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.SplitN(entry, ":", 2) - - 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 -} - -var ( - version string - - conn = flag.String( - "cdp", - "", - "set CDP address", - ) - - launchBrowser = flag.Bool( - "cdp-launch", - false, - "launch Chrome", - ) - - cdpKeepCookies = flag.Bool( - "cdp-keep-cookies", - false, - "keep cookies between queries (i.e. do not open tabs in incognito mode)", - ) - - proxyAddress = flag.String( - "proxy", - "", - "address of proxy server to use (only applicable to static pages, proxy for dynamic pages controlled by Chrome)", - ) - - userAgent = flag.String( - "user-agent", - "", - "set custom user agent. '*' triggers UA generation", - ) - - showTime = flag.Bool( - "time", - false, - "show how much time was taken to execute a query", - ) - - showVersion = flag.Bool( - "version", - false, - "show REPL version", - ) - - help = flag.Bool( - "help", - false, - "show this list", - ) -) - -func main() { - var params Params - - flag.Var( - ¶ms, - "param", - `query parameter (--param=foo:\"bar\", --param=id:1)`, - ) - - flag.Parse() - - if *help { - flag.PrintDefaults() - os.Exit(0) - return - } - - if *showVersion { - 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() - } - - p, err := params.ToMap() - - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - opts := cli.Options{ - Cdp: cdpConn, - Params: p, - Proxy: *proxyAddress, - UserAgent: *userAgent, - ShowTime: *showTime, - KeepCookies: *cdpKeepCookies, - } - - 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) - - b, err := ioutil.ReadAll(std) - - if err != nil { - fmt.Println(err) - os.Exit(1) - return - } - - cli.Exec(string(b), opts) - return - } - - // filename was passed - if flag.NArg() > 0 { - cli.ExecFile(flag.Arg(0), opts) - return - } - - // nothing was passed, run REPL - cli.Repl(version, opts) -} diff --git a/pkg/drivers/container.go b/pkg/drivers/container.go new file mode 100644 index 00000000..b24c383a --- /dev/null +++ b/pkg/drivers/container.go @@ -0,0 +1,58 @@ +package drivers + +import ( + "github.com/MontFerret/ferret/pkg/runtime/core" +) + +type Container struct { + drivers map[string]Driver +} + +func NewContainer() *Container { + return &Container{ + drivers: map[string]Driver{}, + } +} + +func (c *Container) HasDriver(name string) bool { + _, exists := c.drivers[name] + + return exists +} + +func (c *Container) RegisterDriver(drv Driver) error { + if drv == nil { + return core.Error(core.ErrMissedArgument, "driver") + } + + name := drv.Name() + _, exists := c.drivers[name] + + if exists { + return core.Errorf(core.ErrNotUnique, "driver: %s", name) + } + + c.drivers[name] = drv + + return nil +} + +func (c *Container) RegisteredDriver(name string) (Driver, bool) { + found, exists := c.drivers[name] + + return found, exists +} + +func (c *Container) RegisteredDrivers() []Driver { + res := make([]Driver, 0, len(c.drivers)) + + for _, drv := range c.drivers { + res = append(res, drv) + } + + return res +} + +func (c *Container) RemoveDriver(name string) { + delete(c.drivers, name) +} From 90befdb0491f7d1df402c03eabe109ae3ccd47b4 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Thu, 1 Apr 2021 17:15:07 -0400 Subject: [PATCH 2/4] Updated cli --- e2e/cli.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/e2e/cli.go b/e2e/cli.go index c2dda925..e40cd30f 100644 --- a/e2e/cli.go +++ b/e2e/cli.go @@ -6,8 +6,6 @@ import ( "encoding/json" "flag" "fmt" - "github.com/MontFerret/ferret/pkg/runtime" - "github.com/MontFerret/ferret/pkg/runtime/core" "io/ioutil" "os" "strings" @@ -16,6 +14,8 @@ import ( "github.com/MontFerret/ferret/pkg/drivers" "github.com/MontFerret/ferret/pkg/drivers/cdp" "github.com/MontFerret/ferret/pkg/drivers/http" + "github.com/MontFerret/ferret/pkg/runtime" + "github.com/MontFerret/ferret/pkg/runtime/core" ) type Params []string @@ -92,7 +92,8 @@ func main() { query = string(b) } else if flag.NArg() > 0 { - content, err := os.ReadFile(flag.Arg(0)) + // backward compatibility + content, err := ioutil.ReadFile(flag.Arg(0)) if err != nil { fmt.Println(err) From 5911fc1e8572597a916f034ef251bd75d056676d Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Thu, 1 Apr 2021 17:31:33 -0400 Subject: [PATCH 3/4] Updated deps --- .env | 1 - go.mod | 19 ++---- go.sum | 62 ++++++++----------- install.sh | 171 ----------------------------------------------------- 4 files changed, 30 insertions(+), 223 deletions(-) delete mode 100644 .env delete mode 100644 install.sh diff --git a/.env b/.env deleted file mode 100644 index 9ef949a1..00000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -ADDR=8080 \ No newline at end of file diff --git a/go.mod b/go.mod index af4b21e0..229d37fb 100644 --- a/go.mod +++ b/go.mod @@ -3,28 +3,19 @@ module github.com/MontFerret/ferret go 1.13 require ( - github.com/BurntSushi/toml v0.3.1 // indirect - github.com/PuerkitoBio/goquery v1.6.0 + github.com/PuerkitoBio/goquery v1.6.1 github.com/antchfx/htmlquery v1.2.3 github.com/antchfx/xpath v1.1.11 github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0 - github.com/chzyer/logex v1.1.10 // indirect - github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e - github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect github.com/corpix/uarand v0.1.1 - github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9 github.com/gobwas/glob v0.2.3 github.com/gorilla/css v1.0.0 - github.com/mafredri/cdp v0.30.0 - github.com/natefinch/lumberjack v2.0.0+incompatible + github.com/mafredri/cdp v0.31.0 github.com/pkg/errors v0.9.1 - github.com/rs/zerolog v1.19.0 + github.com/rs/zerolog v1.21.0 github.com/sethgrid/pester v1.1.0 github.com/smartystreets/goconvey v1.6.4 github.com/wI2L/jettison v0.7.1 - golang.org/x/net v0.0.0-20200421231249-e086a090c8fd - golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a - golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect - golang.org/x/text v0.3.2 // indirect - gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect + golang.org/x/net v0.0.0-20201021035429-f5854403a974 + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c ) diff --git a/go.sum b/go.sum index 20700fa2..f7ee3e7c 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,8 @@ -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Masterminds/glide v0.13.2/go.mod h1:STyF5vcenH/rUqTEv+/hBXlSTo7KYwg2oc2f4tzPWic= github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/Masterminds/vcs v1.13.0/go.mod h1:N09YCmOQr6RLxC6UNHzuVwAdodYbbnycGHSmwVJjcKA= -github.com/PuerkitoBio/goquery v1.6.0 h1:j7taAbelrdcsOlGeMenZxc2AWXD5fieT1/znArdnx94= -github.com/PuerkitoBio/goquery v1.6.0/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= +github.com/PuerkitoBio/goquery v1.6.1 h1:FgjbQZKl5HTmcn4sKBgvx8vv63nhyhIpv7lJpFGCWpk= +github.com/PuerkitoBio/goquery v1.6.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/antchfx/htmlquery v1.2.3 h1:sP3NFDneHx2stfNXCKbhHFo8XgNjCACnU/4AO5gWz6M= @@ -14,12 +12,6 @@ github.com/antchfx/xpath v1.1.11 h1:WOFtK8TVAjLm3lbgqeP0arlHpvCEeTANeWZ/csPpJkQ= github.com/antchfx/xpath v1.1.11/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs= github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0 h1:j7MyDjg6pb7A2ziow17FDZ2Oj5vGnJsLyDmjpN4Jkcg= github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y= -github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= @@ -29,14 +21,12 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9 h1:G765iDCq7bP5opdrPkXk+4V3yfkgV9iGFuheWZ/X/zY= -github.com/derekparker/trie v0.0.0-20200317170641-1fdf38b7b0e9/go.mod h1:D6ICZm05D9VN1n/8iOtBxLpXtoGp6HDFUJ1RNVieOSE= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/google/go-cmp v0.4.1 h1:/exdXoGamhu5ONeUJH0deniYLWYvQwW66yvlfiiKTu0= -github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= @@ -52,8 +42,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/mafredri/cdp v0.30.0 h1:Lvcwjajq6wB6Uk8dYeCLrF26LG85rUdpMxgrwdEvU0o= -github.com/mafredri/cdp v0.30.0/go.mod h1:71D84qPmWUvBWYj24Zp+U69mrUof4o8qL2X1fQJ/lHc= +github.com/mafredri/cdp v0.31.0 h1:Vd+uCnvBWYsitQRuB/Oxx7S83wfx/ZpeDa4JpSclI6s= +github.com/mafredri/cdp v0.31.0/go.mod h1:YTCwLXkZSa18SGSIxCPMOGZcUJODZSNlAhiMqbyxWJg= github.com/mafredri/go-lint v0.0.0-20180911205320-920981dfc79e/go.mod h1:k/zdyxI3q6dup24o8xpYjJKTCf2F7rfxLp6w/efTiWs= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -62,19 +52,16 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM= -github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk= github.com/ngdinhtoan/glide-cleanup v0.2.0/go.mod h1:UQzsmiDOb8YV3nOsCxK/c9zPpCZVNoHScRE3EO9pVMM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= -github.com/rs/zerolog v1.19.0 h1:hYz4ZVdUgjXTBUmrkrw55j1nHx68LfOKIQk5IYtyScg= -github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= +github.com/rs/zerolog v1.21.0 h1:Q3vdXlfLNT+OftyBHsU0Y445MD+8m8axjKgf2si0QcM= +github.com/rs/zerolog v1.21.0/go.mod h1:ZPhntP/xmq1nnND05hhpAh2QMhSsA4UN3MGZ6O2J3hM= github.com/segmentio/encoding v0.1.10 h1:0b8dva47cSuNQR5ZcU3d0pfi9EnPpSK6q7y5ZGEW36Q= github.com/segmentio/encoding v0.1.10/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw= github.com/sethgrid/pester v1.1.0 h1:IyEAVvwSUPjs2ACFZkBe5N59BBUpSIkQ71Hr6cM5A+w= @@ -89,44 +76,45 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/wI2L/jettison v0.7.1 h1:XNq/WvSOAiJhFww9F5JZZcBZtKFL2Y/9WHHEHLDq9TE= github.com/wI2L/jettison v0.7.1/go.mod h1:dj49nOP41M7x6Jql62BqqF/+nW+XJgBaWzJR0hd6M84= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200421231249-e086a090c8fd h1:QPwSajcTUrFriMF1nJ3XzgoqakqQEsnZf9LdXdi2nkI= golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o= -golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8= -golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200601175630-2caf76543d99/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= -gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/install.sh b/install.sh deleted file mode 100644 index c0e9368e..00000000 --- a/install.sh +++ /dev/null @@ -1,171 +0,0 @@ -#!/bin/bash - -defaultLocation="/usr/local/bin" -defaultVersion="latest" -location=${LAB_LOCATION:-$defaultLocation} -version=${LAB_VERSION:-$defaultVersion} - -# Copyright MontFerret Team 2020 -version=$(curl -sI https://github.com/MontFerret/ferret/releases/latest | awk '{print tolower($0)}' | grep location: | awk -F"/" '{ printf "%s", $NF }' | tr -d '\r') -echo "Installing Ferret $version to $location" - -if [ ! $version ]; then - echo "Failed while attempting to install ferret-cli. Please manually install:" - echo "" - echo "1. Open your web browser and go to https://github.com/MontFerret/ferret/releases" - echo "2. Download the latest release for your platform." - echo "3. chmod +x ./ferret" - echo "4. mv ./ferret $location" - exit 1 -fi - -hasCli() { - has=$(which ferret) - - if [ "$?" = "0" ]; then - echo - echo "You already have the ferret!" - export n=5 - echo "Overwriting in $n seconds... Press Control+C to cancel." - echo - sleep $n - fi - - hasCurl=$(which curl) - - if [ "$?" = "1" ]; then - echo "You need curl to use this script." - exit 1 - fi - - hasTar=$(which tar) - - if [ "$?" = "1" ]; then - echo "You need tar to use this script." - exit 1 - fi -} - -checkHash(){ - sha_cmd="sha256sum" - - if [ ! -x "$(command -v $sha_cmd)" ]; then - sha_cmd="shasum -a 256" - fi - - if [ -x "$(command -v $sha_cmd)" ]; then - - (cd $targetDir && curl -sSL $baseUrl/ferret_checksums.txt | $sha_cmd -c >/dev/null) - if [ "$?" != "0" ]; then - # rm $targetFile - echo "Binary checksum didn't match. Exiting" - exit 1 - fi - fi -} - -getPackage() { - uname=$(uname) - userid=$(id -u) - - platform="" - case $uname in - "Darwin") - platform="_darwin" - ;; - "Linux") - platform="_linux" - ;; - esac - - uname=$(uname -m) - arch="" - case $uname in - "x86_64") - arch="_x86_64" - ;; - esac - case $uname in - "aarch64") - arch="_arm64" - ;; - esac - - if [ "$arch" = "" ]; then - echo "$arch is not supported. Exiting" - exit 1 - fi - - suffix=$platform$arch - targetDir="/tmp/ferret$suffix" - - if [ "$userid" != "0" ]; then - targetDir="$(pwd)/ferret$suffix" - fi - - if [ ! -d $targetDir ]; then - mkdir $targetDir - fi - - targetFile="$targetDir/ferret" - - if [ -e $targetFile ]; then - rm $targetFile - fi - - echo - - if [ $location = $defaultLocation ]; then - if [ "$userid" != "0" ]; then - echo - echo "=========================================================" - echo "== As the script was run as a non-root user the ==" - echo "== following commands may need to be run manually ==" - echo "=========================================================" - echo - echo " sudo cp $targetFile $location/ferret" - echo " rm -rf $targetDir" - echo - - exit 1 - fi - fi - - if [ ! -d $location ]; then - mkdir $location - fi - - baseUrl=https://github.com/MontFerret/ferret/releases/download/$version - url=$baseUrl/ferret$suffix.tar.gz - echo "Downloading package $url as $targetFile" - - curl -sSL $url | tar xz -C $targetDir - - if [ "$?" != "0" ]; then - echo "Failed to download file" - exit 1 - fi - - # checkHash - - chmod +x $targetFile - - echo "Download complete." - echo - echo "Attempting to move $targetFile to $location" - - mv $targetFile "$location/ferret" - - if [ "$?" = "0" ]; then - echo "New version of ferret installed to $location" - fi - - if [ -d $targetDir ]; then - rm -rf $targetDir - fi - - "$location/ferret" --version -} - -hasCli -getPackage From 47626445298928a0836268353703e3a5275346df Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Thu, 8 Apr 2021 18:45:32 -0400 Subject: [PATCH 4/4] Updates after code review --- e2e/cli.go | 2 -- ferret.go | 2 +- pkg/drivers/container.go | 16 ++++++++-------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/e2e/cli.go b/e2e/cli.go index e40cd30f..a2e4b742 100644 --- a/e2e/cli.go +++ b/e2e/cli.go @@ -87,7 +87,6 @@ func main() { if err != nil { fmt.Println(err) os.Exit(1) - return } query = string(b) @@ -98,7 +97,6 @@ func main() { if err != nil { fmt.Println(err) os.Exit(1) - return } query = string(content) diff --git a/ferret.go b/ferret.go index 7f151fc2..5577476f 100644 --- a/ferret.go +++ b/ferret.go @@ -40,7 +40,7 @@ func (i *Instance) Exec(ctx context.Context, query string, opts ...runtime.Optio return nil, err } - for _, drv := range i.drivers.RegisteredDrivers() { + for _, drv := range i.drivers.GetAll() { ctx = drivers.WithContext(ctx, drv) } diff --git a/pkg/drivers/container.go b/pkg/drivers/container.go index b24c383a..0a6bff19 100644 --- a/pkg/drivers/container.go +++ b/pkg/drivers/container.go @@ -14,13 +14,13 @@ func NewContainer() *Container { } } -func (c *Container) HasDriver(name string) bool { +func (c *Container) Has(name string) bool { _, exists := c.drivers[name] return exists } -func (c *Container) RegisterDriver(drv Driver) error { +func (c *Container) Register(drv Driver) error { if drv == nil { return core.Error(core.ErrMissedArgument, "driver") } @@ -37,13 +37,17 @@ func (c *Container) RegisterDriver(drv Driver) error { return nil } -func (c *Container) RegisteredDriver(name string) (Driver, bool) { +func (c *Container) Remove(name string) { + delete(c.drivers, name) +} + +func (c *Container) Get(name string) (Driver, bool) { found, exists := c.drivers[name] return found, exists } -func (c *Container) RegisteredDrivers() []Driver { +func (c *Container) GetAll() []Driver { res := make([]Driver, 0, len(c.drivers)) for _, drv := range c.drivers { @@ -52,7 +56,3 @@ func (c *Container) RegisteredDrivers() []Driver { return res } - -func (c *Container) RemoveDriver(name string) { - delete(c.drivers, name) -}