1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/runtime/options.go

87 lines
1.5 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package runtime
2018-09-28 06:28:33 +02:00
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/gofrs/uuid"
"github.com/rs/zerolog"
"io"
"os"
)
2018-09-18 22:42:38 +02:00
type (
Options struct {
proxy string
cdp string
variables map[string]interface{}
2018-09-28 06:28:33 +02:00
logWriter io.Writer
logLevel zerolog.Level
2018-09-18 22:42:38 +02:00
}
Option func(*Options)
)
func newOptions() *Options {
return &Options{
2018-09-28 06:28:33 +02:00
cdp: "http://0.0.0.0:9222",
2018-09-18 22:42:38 +02:00
variables: make(map[string]interface{}),
2018-09-28 06:28:33 +02:00
logWriter: os.Stdout,
logLevel: zerolog.ErrorLevel,
2018-09-18 22:42:38 +02:00
}
}
func WithParam(name string, value interface{}) Option {
return func(options *Options) {
options.variables[name] = value
}
}
func WithBrowser(address string) Option {
return func(options *Options) {
options.cdp = address
}
}
func WithProxy(address string) Option {
return func(options *Options) {
// TODO: add implementation
options.proxy = address
}
}
2018-09-28 06:28:33 +02:00
func WithLog(writer io.Writer) Option {
return func(options *Options) {
options.logWriter = writer
}
}
func WithLogLevel(lvl logging.Level) Option {
return func(options *Options) {
options.logLevel = zerolog.Level(lvl)
}
}
2018-09-18 22:42:38 +02:00
func (opts *Options) withContext(parent context.Context) context.Context {
2018-09-28 06:28:33 +02:00
ctx := context.WithValue(
2018-09-18 22:42:38 +02:00
parent,
"variables",
opts.variables,
)
2018-09-28 06:28:33 +02:00
id, err := uuid.NewV4()
if err != nil {
panic(err)
}
logger := zerolog.New(opts.logWriter).
With().
Str("id", id.String()).
Logger()
logger.WithLevel(opts.logLevel)
ctx = logger.WithContext(ctx)
return ctx
2018-09-18 22:42:38 +02:00
}