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/cutter.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/cutter.go')
| -rw-r--r-- | plugin/special/cutter.go | 94 |
1 files changed, 60 insertions, 34 deletions
diff --git a/plugin/special/cutter.go b/plugin/special/cutter.go index 509cddbb..589fc3f2 100644 --- a/plugin/special/cutter.go +++ b/plugin/special/cutter.go @@ -3,6 +3,7 @@ package special import ( "encoding/json" "fmt" + "log" "strings" "github.com/Loyalsoldier/geoip/lib" @@ -15,14 +16,61 @@ const ( func init() { lib.RegisterInputConfigCreator(TypeCutter, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - return newCutter(action, data) + return NewCutterFromBytes(action, data) }) - lib.RegisterInputConverter(TypeCutter, &Cutter{ + lib.RegisterInputConverter(TypeCutter, &cutter{ Description: DescCutter, }) } -func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { +type cutter struct { + Type string + Action lib.Action + Description string + Want map[string]bool + OnlyIPType lib.IPType +} + +func NewCutter(action lib.Action, opts ...lib.InputOption) lib.InputConverter { + c := &cutter{ + Type: TypeCutter, + Action: action, + Description: DescCutter, + } + + for _, opt := range opts { + if opt != nil { + opt(c) + } + } + + return c +} + +func WithCutterWantedList(lists []string) lib.InputOption { + return func(s lib.InputConverter) { + wantList := make(map[string]bool) + for _, want := range lists { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + + if len(wantList) == 0 { + log.Fatalf("❌ [type %s] wantedList must be specified", TypeCutter) + } + + s.(*cutter).Want = wantList + } +} + +func WithCutterOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(s lib.InputConverter) { + s.(*cutter).OnlyIPType = onlyIPType + } +} + +func NewCutterFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) { var tmp struct { Want []string `json:"wantedList"` OnlyIPType lib.IPType `json:"onlyIPType"` @@ -38,48 +86,26 @@ func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, err return nil, fmt.Errorf("❌ [type %s] only supports `remove` action", TypeCutter) } - // Filter want list - wantList := make(map[string]bool) - for _, want := range tmp.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true - } - } - - if len(wantList) == 0 { - return nil, fmt.Errorf("❌ [type %s] wantedList must be specified", TypeCutter) - } - - return &Cutter{ - Type: TypeCutter, - Action: action, - Description: DescCutter, - Want: wantList, - OnlyIPType: tmp.OnlyIPType, - }, nil -} - -type Cutter struct { - Type string - Action lib.Action - Description string - Want map[string]bool - OnlyIPType lib.IPType + return NewCutter( + action, + WithCutterWantedList(tmp.Want), + WithCutterOnlyIPType(tmp.OnlyIPType), + ), nil } -func (c *Cutter) GetType() string { +func (c *cutter) GetType() string { return c.Type } -func (c *Cutter) GetAction() lib.Action { +func (c *cutter) GetAction() lib.Action { return c.Action } -func (c *Cutter) GetDescription() string { +func (c *cutter) GetDescription() string { return c.Description } -func (c *Cutter) Input(container lib.Container) (lib.Container, error) { +func (c *cutter) Input(container lib.Container) (lib.Container, error) { ignoreIPType := lib.GetIgnoreIPType(c.OnlyIPType) for entry := range container.Loop() { |
