diff options
| author | anthropic-code-agent[bot] <[email protected]> | 2026-04-28 18:08:20 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-28 18:08:20 +0000 |
| commit | fbc2f20cdfc138b0146fcc61bcb7ed2c1776a076 (patch) | |
| tree | fada69f06625575fbda4a0533dd95316d7d0d2b6 /plugin/special/stdout.go | |
| parent | 6fbcfeba5b73e4ac50ab0257040103cadf524ea7 (diff) | |
Complete functional options pattern refactoring for special pluginclaude/refactor-plugins-functional-options
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/0483b641-15da-4630-a3ad-53f8d1c65fe9
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/special/stdout.go')
| -rw-r--r-- | plugin/special/stdout.go | 68 |
1 files changed, 50 insertions, 18 deletions
diff --git a/plugin/special/stdout.go b/plugin/special/stdout.go index 614deaac..76edde3c 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{ + 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(s lib.OutputConverter) { + s.(*stdout).Want = lists + } +} + +func WithStdoutExcludedList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*stdout).Exclude = lists + } +} + +func WithStdoutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*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,17 +72,15 @@ 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 { +type stdout struct { Type string Action lib.Action Description string @@ -57,19 +89,19 @@ type Stdout struct { OnlyIPType lib.IPType } -func (s *Stdout) GetType() string { +func (s *stdout) GetType() string { return s.Type } -func (s *Stdout) GetAction() lib.Action { +func (s *stdout) GetAction() lib.Action { return s.Action } -func (s *Stdout) GetDescription() string { +func (s *stdout) GetDescription() string { return s.Description } -func (s *Stdout) Output(container lib.Container) error { +func (s *stdout) Output(container lib.Container) error { for _, name := range s.filterAndSortList(container) { entry, found := container.GetEntry(name) if !found { @@ -89,7 +121,7 @@ func (s *Stdout) Output(container lib.Container) error { return nil } -func (s *Stdout) filterAndSortList(container lib.Container) []string { +func (s *stdout) filterAndSortList(container lib.Container) []string { excludeMap := make(map[string]bool) for _, exclude := range s.Exclude { if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" { @@ -125,7 +157,7 @@ func (s *Stdout) filterAndSortList(container lib.Container) []string { return list } -func (s *Stdout) generateCIDRList(entry *lib.Entry) ([]string, error) { +func (s *stdout) generateCIDRList(entry *lib.Entry) ([]string, error) { entryList, err := entry.MarshalText(lib.GetIgnoreIPType(s.OnlyIPType)) if err != nil { return nil, err |
