Skip to content

Commit

Permalink
code(*/*) - add input field
Browse files Browse the repository at this point in the history
  • Loading branch information
PxyUp committed Feb 1, 2024
1 parent d4fe27b commit 6262998
Show file tree
Hide file tree
Showing 39 changed files with 376 additions and 254 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
},
},
},
}, nil, nil, nil)
}, nil, nil, nil, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -129,6 +129,7 @@ go run cmd/cli/main.go --path=./examples/cli/config_cli.json
5. **--omit-error-pretty** - bool[false] - Provide pure value if pretty is invalid
6. **--plugins** - string[""] - [path for plugins for Fitter](https://github.com/PxyUp/fitter/blob/master/examples/plugin/README.md)
7. **--log-level** - enum["info", "error", "debug", "fatal"] - set log level(only if verbose set to true)
8. **--input** - string[""] - specify input value for [formatting](#placeholder-list). Examples: `--input=\""124"\"` `--input=124` `--input='{"test": 5}'`

```bash
./fitter_cli_${VERSION} --path=./examples/cli/config_cli.json --copy=true
Expand Down Expand Up @@ -263,7 +264,7 @@ type plugin struct {
Name string `json:"name" yaml:"name"`
}

func (pl *plugin) Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error) {
func (pl *plugin) Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error) {
return []byte(fmt.Sprintf(`{"name": "%s"}`, pl.Name)), nil
}

Expand Down Expand Up @@ -1076,6 +1077,7 @@ Examples:
6. {{{RefName=SomeName json.path}}} - get [reference](#references) value by name and extract value by json path. [Example](https://github.com/PxyUp/fitter/blob/master/examples/cli/config_ref.json#L67)
7. {{{FromEnv=ENV_KEY}}} - get value from environment variable
8. {{{FromExp=fRes + 5 + fIndex}}} - get value from the [expression](https://github.com/expr-lang/expr). [Predefined values](#predefined-values)
9. {{{FromInput=.}}} || {{{FromInput=json.path}}} - get value from input of trigger or library

Examples:
```text
Expand Down
5 changes: 4 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"flag"
"fmt"
"github.com/PxyUp/fitter/lib"
"github.com/PxyUp/fitter/pkg/builder"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/plugins/store"
"github.com/atotto/clipboard"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
"log"
"os"
Expand Down Expand Up @@ -50,6 +52,7 @@ func main() {
omitPrettyErrorFlag := flag.Bool("omit-error-pretty", false, "Provide pure value if pretty is invalid")
pluginsFlag := flag.String("plugins", "", "Provide plugins folder")
logLevel := flag.String("log-level", "info", "Level for logger")
inputFlag := flag.String("input", "", "Input for model")
flag.Parse()

log := logger.Null
Expand All @@ -66,7 +69,7 @@ func main() {
}

cfg := getConfig(*filePath)
res, err := lib.Parse(cfg.Item, cfg.Limits, cfg.References, log)
res, err := lib.Parse(cfg.Item, cfg.Limits, cfg.References, builder.PureString(gjson.Parse(*inputFlag).String()), log)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
Expand Down
3 changes: 2 additions & 1 deletion examples/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/PxyUp/fitter/lib"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"log"
"net/http"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func main() {
},
},
},
}, nil, nil, nil)
}, nil, nil, nil, logger.Null)
if err != nil {
log.Fatal(err)
}
Expand Down
Binary file modified examples/plugin/connector.so
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/plugin/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type plugin struct {
Name string `json:"name" yaml:"name"`
}

func (pl *plugin) Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error) {
func (pl *plugin) Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error) {
return []byte(fmt.Sprintf(`{"name": "%s"}`, pl.Name)), nil
}

Expand Down
Binary file modified examples/plugin/hardcoder.so
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/plugin/hardcoder/hardcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type plugin struct {
Name string `json:"name" yaml:"name"`
}

func (pl *plugin) Format(parsedValue builder.Jsonable, field *config.PluginFieldConfig, logger logger.Logger, index *uint32) builder.Jsonable {
func (pl *plugin) Format(parsedValue builder.Jsonable, field *config.PluginFieldConfig, logger logger.Logger, index *uint32, input builder.Jsonable) builder.Jsonable {
if field.Config != nil {
err := json.Unmarshal(field.Config, pl)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions lib/lib.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package lib

import (
"github.com/PxyUp/fitter/pkg/builder"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/parser"
"github.com/PxyUp/fitter/pkg/registry"
"github.com/google/uuid"
)

func Parse(item *config.Item, limits *config.Limits, refMap config.RefMap, log logger.Logger) (*parser.ParseResult, error) {
func Parse(item *config.Item, limits *config.Limits, refMap config.RefMap, input builder.Jsonable, log logger.Logger) (*parser.ParseResult, error) {
cfg := &config.CliItem{
Item: item,
Limits: limits,
Expand All @@ -19,5 +20,5 @@ func Parse(item *config.Item, limits *config.Limits, refMap config.RefMap, log l
if log == nil {
log = logger.Null
}
return registry.FromItem(cfg, log).Get(name).Process()
return registry.FromItem(cfg, log).Get(name).Process(input)
}
6 changes: 3 additions & 3 deletions pkg/connectors/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (c *browserConnector) WithLogger(logger logger.Logger) *browserConnector {
return c
}

func (c *browserConnector) Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error) {
formattedURL := utils.Format(c.url, parsedValue, index)
func (c *browserConnector) Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error) {
formattedURL := utils.Format(c.url, parsedValue, index, input)

if formattedURL == "" {
return nil, errEmpty
Expand All @@ -42,7 +42,7 @@ func (c *browserConnector) Get(parsedValue builder.Jsonable, index *uint32) ([]b
}

if c.cfg.Playwright != nil {
return getFromPlaywright(formattedURL, c.cfg.Playwright, parsedValue, index, c.logger.With("emulator", "playwright"))
return getFromPlaywright(formattedURL, c.cfg.Playwright, parsedValue, index, input, c.logger.With("emulator", "playwright"))
}

return nil, nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/connectors/connectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ var (
)

type Connector interface {
Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error)
Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error)
}

type attemptsConnector struct {
original Connector
attempts uint32
}

func (r *attemptsConnector) Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error) {
func (r *attemptsConnector) Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error) {
if r.attempts <= 0 {
return r.original.Get(parsedValue, index)
return r.original.Get(parsedValue, index, input)
}

for i := 0; i < int(r.attempts); i++ {
resp, err := r.original.Get(parsedValue, index)
resp, err := r.original.Get(parsedValue, index, input)
if err != nil || len(resp) == 0 {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/connectors/from_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type fileConnector struct {
logger logger.Logger
}

func (j *fileConnector) Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error) {
file, err := os.Open(utils.Format(j.cfg.Path, parsedValue, index))
func (j *fileConnector) Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error) {
file, err := os.Open(utils.Format(j.cfg.Path, parsedValue, index, input))
if err != nil {
j.logger.Errorw("cant open file", "error", err.Error())
return nil, err
Expand All @@ -34,7 +34,7 @@ func (j *fileConnector) Get(parsedValue builder.Jsonable, index *uint32) ([]byte
return body, nil
}

return []byte(utils.Format(string(body), parsedValue, index)), nil
return []byte(utils.Format(string(body), parsedValue, index, input)), nil
}

func NewFile(cfg *config.FileConnectorConfig) *fileConnector {
Expand Down
10 changes: 5 additions & 5 deletions pkg/connectors/playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
errNoDriver = errors.New("empty playwright driver")
)

func getFromPlaywright(url string, cfg *config.PlaywrightConfig, parsedValue builder.Jsonable, index *uint32, logger logger.Logger) ([]byte, error) {
func getFromPlaywright(url string, cfg *config.PlaywrightConfig, parsedValue builder.Jsonable, index *uint32, input builder.Jsonable, logger logger.Logger) ([]byte, error) {
if instanceLimit := limitter.PlaywrightLimiter(); instanceLimit != nil {
errInstance := instanceLimit.Acquire(ctx, 1)
if errInstance != nil {
Expand Down Expand Up @@ -91,13 +91,13 @@ func getFromPlaywright(url string, cfg *config.PlaywrightConfig, parsedValue bui
if cfg.Proxy != nil {
proxy := &playwright.BrowserTypeLaunchOptionsProxy{}
if cfg.Proxy.Server != "" {
proxy.Server = utils.String(utils.Format(cfg.Proxy.Server, parsedValue, index))
proxy.Server = utils.String(utils.Format(cfg.Proxy.Server, parsedValue, index, input))
}
if cfg.Proxy.Username != "" {
proxy.Username = utils.String(utils.Format(cfg.Proxy.Username, parsedValue, index))
proxy.Username = utils.String(utils.Format(cfg.Proxy.Username, parsedValue, index, input))
}
if cfg.Proxy.Password != "" {
proxy.Password = utils.String(utils.Format(cfg.Proxy.Password, parsedValue, index))
proxy.Password = utils.String(utils.Format(cfg.Proxy.Password, parsedValue, index, input))
}
logger.Debugw("set proxy", "server", cfg.Proxy.Server, "username", cfg.Proxy.Username, "password", cfg.Proxy.Password)
opts = append(opts, playwright.BrowserTypeLaunchOptions{
Expand Down Expand Up @@ -170,7 +170,7 @@ func getFromPlaywright(url string, cfg *config.PlaywrightConfig, parsedValue bui
}

if cfg.PreRunScript != "" {
_, err = page.Evaluate(utils.Format(cfg.PreRunScript, parsedValue, index))
_, err = page.Evaluate(utils.Format(cfg.PreRunScript, parsedValue, index, input))
if err != nil {
logger.Errorw("could execute script on page", "error", err.Error(), "script", cfg.PreRunScript)
return
Expand Down
22 changes: 11 additions & 11 deletions pkg/connectors/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func (api *apiConnector) WithLogger(logger logger.Logger) *apiConnector {
return api
}

func (api *apiConnector) GetWithHeaders(parsedValue builder.Jsonable, index *uint32) (http.Header, []byte, error) {
return api.get(parsedValue, index)
func (api *apiConnector) GetWithHeaders(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) (http.Header, []byte, error) {
return api.get(parsedValue, index, input)
}

func (api *apiConnector) get(parsedValue builder.Jsonable, index *uint32) (http.Header, []byte, error) {
formattedBody := utils.Format(api.cfg.Body, parsedValue, index)
formattedURL := utils.Format(api.url, parsedValue, index)
func (api *apiConnector) get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) (http.Header, []byte, error) {
formattedBody := utils.Format(api.cfg.Body, parsedValue, index, input)
formattedURL := utils.Format(api.url, parsedValue, index, input)

if formattedURL == "" {
return nil, nil, errEmpty
Expand All @@ -89,7 +89,7 @@ func (api *apiConnector) get(parsedValue builder.Jsonable, index *uint32) (http.
}

for k, v := range api.cfg.Headers {
req.Header.Add(k, utils.Format(v, parsedValue, index))
req.Header.Add(k, utils.Format(v, parsedValue, index, input))
}

client := http_client.GetDefaultClient()
Expand All @@ -98,17 +98,17 @@ func (api *apiConnector) get(parsedValue builder.Jsonable, index *uint32) (http.
}

if api.cfg.Proxy != nil {
proxyUrl, errProxy := url.Parse(utils.Format(api.cfg.Proxy.Server, parsedValue, index))
proxyUrl, errProxy := url.Parse(utils.Format(api.cfg.Proxy.Server, parsedValue, index, input))
if errProxy != nil {
api.logger.Errorw("unable to create proxy", "error", errProxy.Error())
return nil, nil, err
}

if api.cfg.Proxy.Username != "" {
if api.cfg.Proxy.Password != "" {
proxyUrl.User = url.UserPassword(utils.Format(api.cfg.Proxy.Username, parsedValue, index), utils.Format(api.cfg.Proxy.Password, parsedValue, index))
proxyUrl.User = url.UserPassword(utils.Format(api.cfg.Proxy.Username, parsedValue, index, input), utils.Format(api.cfg.Proxy.Password, parsedValue, index, input))
} else {
proxyUrl.User = url.User(utils.Format(api.cfg.Proxy.Username, parsedValue, index))
proxyUrl.User = url.User(utils.Format(api.cfg.Proxy.Username, parsedValue, index, input))
}
}
api.logger.Debugw("set proxy", "server", api.cfg.Proxy.Server, "username", api.cfg.Proxy.Username, "password", api.cfg.Proxy.Password)
Expand Down Expand Up @@ -150,7 +150,7 @@ func (api *apiConnector) get(parsedValue builder.Jsonable, index *uint32) (http.
return resp.Header, bytes, nil
}

func (api *apiConnector) Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error) {
_, body, err := api.get(parsedValue, index)
func (api *apiConnector) Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error) {
_, body, err := api.get(parsedValue, index, input)
return body, err
}
6 changes: 3 additions & 3 deletions pkg/connectors/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type staticConnector struct {
logger logger.Logger
}

func (j *staticConnector) Get(parsedValue builder.Jsonable, index *uint32) ([]byte, error) {
func (j *staticConnector) Get(parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) ([]byte, error) {
if len(j.cfg.Raw) != 0 {
return []byte(utils.Format(string(j.cfg.Raw), parsedValue, index)), nil
return []byte(utils.Format(string(j.cfg.Raw), parsedValue, index, input)), nil
}
return []byte(utils.Format(j.cfg.Value, parsedValue, index)), nil
return []byte(utils.Format(j.cfg.Value, parsedValue, index, input)), nil
}

func NewStatic(cfg *config.StaticConnectorConfig) *staticConnector {
Expand Down
4 changes: 2 additions & 2 deletions pkg/notifier/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func (h *httpNotifier) notify(record *singleRecord) error {
return err
}

req, err := http.NewRequest(h.cfg.Method, utils.Format(h.cfg.Url, nil, nil), bytes.NewReader(bb))
req, err := http.NewRequest(h.cfg.Method, utils.Format(h.cfg.Url, nil, nil, nil), bytes.NewReader(bb))
if err != nil {
h.logger.Errorw("cant create request", "error", err.Error())
return err
}

for k, v := range h.cfg.Headers {
req.Header.Add(k, utils.Format(v, nil, nil))
req.Header.Add(k, utils.Format(v, nil, nil, nil))
}

if h.cfg.Timeout > 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func ShouldInform(cfg *config.NotifierConfig, result builder.Jsonable) (bool, er
return true, nil
}

out, err := utils.ProcessExpression(cfg.Expression, result, nil)
out, err := utils.ProcessExpression(cfg.Expression, result, nil, nil)
if err != nil {
return false, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/notifier/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (r *redisNotifier) notify(record *singleRecord) error {
r.logger.Errorw("cant marshal message", "error", errMars.Error())
return errMars
}
errSend := r.redisClient.Publish(ctx, utils.Format(r.cfg.Channel, nil, nil), msg).Err()
errSend := r.redisClient.Publish(ctx, utils.Format(r.cfg.Channel, nil, nil, nil), msg).Err()
if errSend != nil {
r.logger.Errorw("cant send message", "error", errSend.Error())
return errSend
Expand All @@ -54,8 +54,8 @@ func NewRedis(name string, cfg *config.RedisNotifierConfig) *redisNotifier {
name: name,
cfg: cfg,
redisClient: redis.NewClient(&redis.Options{
Addr: utils.Format(cfg.Addr, nil, nil),
Password: utils.Format(cfg.Password, nil, nil),
Addr: utils.Format(cfg.Addr, nil, nil, nil),
Password: utils.Format(cfg.Password, nil, nil, nil),
DB: cfg.DB,
}),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/notifier/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
)

func NewTelegramBot(name string, cfg *config.TelegramBotConfig) (*telegramBot, error) {
botApi, err := tgbotapi.NewBotAPI(utils.Format(cfg.Token, nil, nil))
botApi, err := tgbotapi.NewBotAPI(utils.Format(cfg.Token, nil, nil, nil))
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/parser/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
)

type Engine interface {
Get(model *config.Model, parsedValue builder.Jsonable, index *uint32) (*ParseResult, error)
Get(model *config.Model, parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) (*ParseResult, error)
}

type engine struct {
Expand All @@ -33,21 +33,21 @@ type engine struct {
type null struct {
}

func (n *null) Get(model *config.Model, parsedValue builder.Jsonable, index *uint32) (*ParseResult, error) {
func (n *null) Get(model *config.Model, parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) (*ParseResult, error) {
return nil, errInvalid
}

func (e *engine) Get(model *config.Model, parsedValue builder.Jsonable, index *uint32) (*ParseResult, error) {
func (e *engine) Get(model *config.Model, parsedValue builder.Jsonable, index *uint32, input builder.Jsonable) (*ParseResult, error) {
if model == nil {
return nil, errMissingModelConfig
}
body, err := e.connector.Get(parsedValue, index)
body, err := e.connector.Get(parsedValue, index, input)
if err != nil {
e.logger.Errorw("connector return error during fetch data", "error", err.Error())
return nil, err
}
e.logger.Debugw("connector answer", "content", string(body))
return e.parser(body, e.logger).Parse(model)
return e.parser(body, e.logger).Parse(model, input)
}

func NewEngine(cfg *config.ConnectorConfig, logger logger.Logger) Engine {
Expand Down
Loading

0 comments on commit 6262998

Please sign in to comment.