diff options
Diffstat (limited to 'plugin/special/lookup.go')
| -rw-r--r-- | plugin/special/lookup.go | 81 |
1 files changed, 54 insertions, 27 deletions
diff --git a/plugin/special/lookup.go b/plugin/special/lookup.go index 96735146..ddf96ecd 100644 --- a/plugin/special/lookup.go +++ b/plugin/special/lookup.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "net/netip" "slices" "strings" @@ -18,14 +19,55 @@ const ( func init() { lib.RegisterOutputConfigCreator(TypeLookup, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { - return newLookup(action, data) + return NewLookupFromBytes(action, data) }) - lib.RegisterOutputConverter(TypeLookup, &Lookup{ + lib.RegisterOutputConverter(TypeLookup, &lookup{ Description: DescLookup, }) } -func newLookup(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +type lookup struct { + Type string + Action lib.Action + Description string + Search string + SearchList []string +} + +func NewLookup(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + l := &lookup{ + Type: TypeLookup, + Action: action, + Description: DescLookup, + } + + for _, opt := range opts { + if opt != nil { + opt(l) + } + } + + return l +} + +func WithLookupSearch(search string) lib.OutputOption { + return func(s lib.OutputConverter) { + search = strings.TrimSpace(search) + if search == "" { + log.Fatalf("❌ [type %s | action %s] please specify an IP or a CIDR as search target", TypeLookup, s.(*lookup).Action) + } + + s.(*lookup).Search = search + } +} + +func WithLookupSearchList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*lookup).SearchList = lists + } +} + +func NewLookupFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { Search string `json:"search"` SearchList []string `json:"searchList"` @@ -37,41 +79,26 @@ func newLookup(action lib.Action, data json.RawMessage) (lib.OutputConverter, er } } - tmp.Search = strings.TrimSpace(tmp.Search) - if tmp.Search == "" { - return nil, fmt.Errorf("❌ [type %s | action %s] please specify an IP or a CIDR as search target", TypeLookup, action) - } - - return &Lookup{ - Type: TypeLookup, - Action: action, - Description: DescLookup, - Search: tmp.Search, - SearchList: tmp.SearchList, - }, nil -} - -type Lookup struct { - Type string - Action lib.Action - Description string - Search string - SearchList []string + return NewLookup( + action, + WithLookupSearch(tmp.Search), + WithLookupSearchList(tmp.SearchList), + ), nil } -func (l *Lookup) GetType() string { +func (l *lookup) GetType() string { return l.Type } -func (l *Lookup) GetAction() lib.Action { +func (l *lookup) GetAction() lib.Action { return l.Action } -func (l *Lookup) GetDescription() string { +func (l *lookup) GetDescription() string { return l.Description } -func (l *Lookup) Output(container lib.Container) error { +func (l *lookup) Output(container lib.Container) error { switch strings.Contains(l.Search, "/") { case true: // CIDR if _, err := netip.ParsePrefix(l.Search); err != nil { |
