2018-09-18 22:42:38 +02:00
|
|
|
package runtime
|
|
|
|
|
2018-09-28 06:28:33 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2018-09-29 03:04:16 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2018-10-08 03:23:36 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/env"
|
2018-09-28 06:28:33 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/logging"
|
2018-09-29 03:04:16 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2018-09-28 06:28:33 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
type (
|
|
|
|
Options struct {
|
2018-09-29 03:04:16 +02:00
|
|
|
proxy string
|
|
|
|
cdp string
|
|
|
|
params map[string]core.Value
|
|
|
|
logging *logging.Options
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Option func(*Options)
|
|
|
|
)
|
|
|
|
|
|
|
|
func newOptions() *Options {
|
|
|
|
return &Options{
|
2018-09-29 03:04:16 +02:00
|
|
|
cdp: "http://0.0.0.0:9222",
|
|
|
|
params: make(map[string]core.Value),
|
|
|
|
logging: &logging.Options{
|
|
|
|
Writer: os.Stdout,
|
|
|
|
Level: logging.ErrorLevel,
|
|
|
|
},
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithParam(name string, value interface{}) Option {
|
|
|
|
return func(options *Options) {
|
2018-09-29 03:04:16 +02:00
|
|
|
options.params[name] = values.Parse(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithParams(params map[string]interface{}) Option {
|
|
|
|
return func(options *Options) {
|
|
|
|
for name, value := range params {
|
|
|
|
options.params[name] = values.Parse(value)
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBrowser(address string) Option {
|
|
|
|
return func(options *Options) {
|
|
|
|
options.cdp = address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithProxy(address string) Option {
|
|
|
|
return func(options *Options) {
|
|
|
|
options.proxy = address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:28:33 +02:00
|
|
|
func WithLog(writer io.Writer) Option {
|
|
|
|
return func(options *Options) {
|
2018-09-29 03:04:16 +02:00
|
|
|
options.logging.Writer = writer
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLogLevel(lvl logging.Level) Option {
|
|
|
|
return func(options *Options) {
|
2018-09-29 03:04:16 +02:00
|
|
|
options.logging.Level = lvl
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
func (opts *Options) withContext(parent context.Context) context.Context {
|
2018-09-29 03:04:16 +02:00
|
|
|
ctx := core.ParamsWith(parent, opts.params)
|
|
|
|
ctx = logging.WithContext(ctx, opts.logging)
|
2018-10-08 03:23:36 +02:00
|
|
|
ctx = env.WithContext(ctx, env.Environment{
|
|
|
|
CDPAddress: opts.cdp,
|
|
|
|
ProxyAddress: opts.proxy,
|
|
|
|
})
|
2018-09-28 06:28:33 +02:00
|
|
|
|
|
|
|
return ctx
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|