diff options
| author | openai-code-agent[bot] <[email protected]> | 2026-04-28 17:42:24 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-28 17:42:24 +0000 |
| commit | 9fc06ad2d090b76132859a61a04a1ff0437cc641 (patch) | |
| tree | 473f24e7c9d8e3d17dfa708e04682f55f7bbd560 /plugin/special | |
| parent | 63f74a8e67539967b3861a9898f5593496e3a89c (diff) | |
refactor(special): use functional options for stdin/stdout/lookup/test/cutter
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/special')
| -rw-r--r-- | plugin/special/cutter.go | 62 | ||||
| -rw-r--r-- | plugin/special/lookup.go | 45 | ||||
| -rw-r--r-- | plugin/special/stdin.go | 50 | ||||
| -rw-r--r-- | plugin/special/stdout.go | 52 | ||||
| -rw-r--r-- | plugin/special/test.go | 24 |
5 files changed, 177 insertions, 56 deletions
diff --git a/plugin/special/cutter.go b/plugin/special/cutter.go index 509cddbb..bea22f0b 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,51 @@ 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{ Description: DescCutter, }) } -func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { +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(c 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) + } + c.(*Cutter).Want = wantList + } +} + +func WithCutterOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(c lib.InputConverter) { + c.(*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,25 +76,7 @@ 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 + return NewCutter(action, WithCutterWantedList(tmp.Want), WithCutterOnlyIPType(tmp.OnlyIPType)), nil } type Cutter struct { diff --git a/plugin/special/lookup.go b/plugin/special/lookup.go index 96735146..683988eb 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,46 @@ 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{ Description: DescLookup, }) } -func newLookup(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +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(c 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, c.(*Lookup).Action) + } + c.(*Lookup).Search = search + } +} + +func WithLookupSearchList(lists []string) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*Lookup).SearchList = lists + } +} + +func NewLookupFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { Search string `json:"search"` SearchList []string `json:"searchList"` @@ -42,13 +75,7 @@ func newLookup(action lib.Action, data json.RawMessage) (lib.OutputConverter, er 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 + return NewLookup(action, WithLookupSearch(tmp.Search), WithLookupSearchList(tmp.SearchList)), nil } type Lookup struct { diff --git a/plugin/special/stdin.go b/plugin/special/stdin.go index f2f9cf9c..3c816eb7 100644 --- a/plugin/special/stdin.go +++ b/plugin/special/stdin.go @@ -3,7 +3,7 @@ package special import ( "bufio" "encoding/json" - "fmt" + "log" "os" "strings" @@ -17,14 +17,46 @@ const ( func init() { lib.RegisterInputConfigCreator(TypeStdin, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - return newStdin(action, data) + return NewStdinFromBytes(action, data) }) lib.RegisterInputConverter(TypeStdin, &Stdin{ Description: DescStdin, }) } -func newStdin(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { +func NewStdin(action lib.Action, opts ...lib.InputOption) lib.InputConverter { + s := &Stdin{ + Type: TypeStdin, + Action: action, + Description: DescStdin, + } + + for _, opt := range opts { + if opt != nil { + opt(s) + } + } + + return s +} + +func WithStdinName(name string) lib.InputOption { + return func(c lib.InputConverter) { + name = strings.TrimSpace(name) + if name == "" { + log.Fatalf("❌ [type %s | action %s] missing name", TypeStdin, c.(*Stdin).Action) + } + c.(*Stdin).Name = name + } +} + +func WithStdinOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(c lib.InputConverter) { + c.(*Stdin).OnlyIPType = onlyIPType + } +} + +func NewStdinFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) { var tmp struct { Name string `json:"name"` OnlyIPType lib.IPType `json:"onlyIPType"` @@ -36,17 +68,11 @@ func newStdin(action lib.Action, data json.RawMessage) (lib.InputConverter, erro } } - if tmp.Name == "" { - return nil, fmt.Errorf("❌ [type %s | action %s] missing name", TypeStdin, action) + if action != lib.ActionAdd && action != lib.ActionRemove { + log.Fatalf("❌ [type %s | action %s] invalid action", TypeStdin, action) } - return &Stdin{ - Type: TypeStdin, - Action: action, - Description: DescStdin, - Name: tmp.Name, - OnlyIPType: tmp.OnlyIPType, - }, nil + return NewStdin(action, WithStdinName(tmp.Name), WithStdinOnlyIPType(tmp.OnlyIPType)), nil } type Stdin struct { diff --git a/plugin/special/stdout.go b/plugin/special/stdout.go index 614deaac..d94b97ff 100644 --- a/plugin/special/stdout.go +++ b/plugin/special/stdout.go @@ -18,14 +18,48 @@ const ( func init() { lib.RegisterOutputConfigCreator(TypeStdout, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { - return newStdout(action, data) + return NewStdoutFromBytes(action, data) }) lib.RegisterOutputConverter(TypeStdout, &Stdout{ Description: DescStdout, }) } -func newStdout(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +func NewStdout(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + s := &Stdout{ + Type: TypeStdout, + Action: action, + Description: DescStdout, + } + + for _, opt := range opts { + if opt != nil { + opt(s) + } + } + + return s +} + +func WithStdoutWantedList(lists []string) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*Stdout).Want = lists + } +} + +func WithStdoutExcludedList(lists []string) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*Stdout).Exclude = lists + } +} + +func WithStdoutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*Stdout).OnlyIPType = onlyIPType + } +} + +func NewStdoutFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { Want []string `json:"wantedList"` Exclude []string `json:"excludedList"` @@ -38,14 +72,12 @@ func newStdout(action lib.Action, data json.RawMessage) (lib.OutputConverter, er } } - return &Stdout{ - Type: TypeStdout, - Action: action, - Description: DescStdout, - Want: tmp.Want, - Exclude: tmp.Exclude, - OnlyIPType: tmp.OnlyIPType, - }, nil + return NewStdout( + action, + WithStdoutWantedList(tmp.Want), + WithStdoutExcludedList(tmp.Exclude), + WithStdoutOnlyIPType(tmp.OnlyIPType), + ), nil } type Stdout struct { diff --git a/plugin/special/test.go b/plugin/special/test.go index cf83ed83..18f273fa 100644 --- a/plugin/special/test.go +++ b/plugin/special/test.go @@ -2,6 +2,7 @@ package special import ( "encoding/json" + "log" "github.com/Loyalsoldier/geoip/lib" ) @@ -18,19 +19,34 @@ var testCIDRs = []string{ func init() { lib.RegisterInputConfigCreator(typeTest, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - return newTest(action, data) + return NewTestFromBytes(action, data) }) lib.RegisterInputConverter(typeTest, &test{ Description: descTest, }) } -func newTest(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - return &test{ +func NewTest(action lib.Action, opts ...lib.InputOption) lib.InputConverter { + t := &test{ Type: typeTest, Action: action, Description: descTest, - }, nil + } + + for _, opt := range opts { + if opt != nil { + opt(t) + } + } + + return t +} + +func NewTestFromBytes(action lib.Action, _ []byte) (lib.InputConverter, error) { + if action != lib.ActionAdd && action != lib.ActionRemove { + log.Fatalf("❌ [type %s | action %s] invalid action", typeTest, action) + } + return NewTest(action), nil } type test struct { |
