diff options
| author | Loyalsoldier <[email protected]> | 2024-10-24 06:01:54 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2024-10-24 06:01:54 +0800 |
| commit | 40e0ad6895d62ad12ea1b1da4293dbcc05ea1d29 (patch) | |
| tree | a3b3310824baa53ff4d7dad417a90a769386269d /plugin/special | |
| parent | 4cf50641990c7df79b1a03438dd9273532182e2f (diff) | |
Feat: support to exclude specified lists when output
Diffstat (limited to 'plugin/special')
| -rw-r--r-- | plugin/special/stdout.go | 58 |
1 files changed, 46 insertions, 12 deletions
diff --git a/plugin/special/stdout.go b/plugin/special/stdout.go index b0f9320a..8c57cb58 100644 --- a/plugin/special/stdout.go +++ b/plugin/special/stdout.go @@ -5,6 +5,7 @@ import ( "errors" "io" "os" + "slices" "strings" "github.com/Loyalsoldier/geoip/lib" @@ -27,6 +28,7 @@ func init() { func newStdout(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { var tmp struct { Want []string `json:"wantedList"` + Exclude []string `json:"excludedList"` OnlyIPType lib.IPType `json:"onlyIPType"` } @@ -36,19 +38,12 @@ func newStdout(action lib.Action, data json.RawMessage) (lib.OutputConverter, er } } - // Filter want list - wantList := make(map[string]bool) - for _, want := range tmp.Want { - if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { - wantList[want] = true - } - } - return &stdout{ Type: typeStdout, Action: action, Description: descStdout, - Want: wantList, + Want: tmp.Want, + Exclude: tmp.Exclude, OnlyIPType: tmp.OnlyIPType, }, nil } @@ -57,7 +52,8 @@ type stdout struct { Type string Action lib.Action Description string - Want map[string]bool + Want []string + Exclude []string OnlyIPType lib.IPType } @@ -74,8 +70,9 @@ func (s *stdout) GetDescription() string { } func (s *stdout) Output(container lib.Container) error { - for entry := range container.Loop() { - if len(s.Want) > 0 && !s.Want[entry.GetName()] { + for _, name := range s.filterAndSortList(container) { + entry, found := container.GetEntry(name) + if !found { continue } @@ -83,6 +80,7 @@ func (s *stdout) Output(container lib.Container) error { if err != nil { continue } + for _, cidr := range cidrList { io.WriteString(os.Stdout, cidr+"\n") } @@ -91,6 +89,42 @@ func (s *stdout) Output(container lib.Container) error { return nil } +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 != "" { + excludeMap[exclude] = true + } + } + + wantList := make([]string, 0, len(s.Want)) + for _, want := range s.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] { + wantList = append(wantList, want) + } + } + + if len(wantList) > 0 { + // Sort the list + slices.Sort(wantList) + return wantList + } + + list := make([]string, 0, 300) + for entry := range container.Loop() { + name := entry.GetName() + if excludeMap[name] { + continue + } + list = append(list, name) + } + + // Sort the list + slices.Sort(list) + + return list +} + func (s *stdout) generateCIDRList(entry *lib.Entry) ([]string, error) { var entryList []string var err error |
