diff options
| author | copilot-swe-agent[bot] <[email protected]> | 2026-04-28 18:24:00 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-28 18:24:00 +0000 |
| commit | 8834c0be63ee88e0ad23fe621d5f5fe344b32089 (patch) | |
| tree | 49029d2fd927e8832d05420d1bf1cf850aa22325 /plugin/special/lookup.go | |
| parent | 4f125e579472e5ed87fd052ef68ab80f5fe679b0 (diff) | |
Refactor all plugins to use functional options patterncopilot/refactor-plugins-functional-options
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/e2b66c9a-3d01-490c-9b31-32109cfe4feb
Co-authored-by: Loyalsoldier <[email protected]>
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 { |
