diff options
| author | loyalsoldier <[email protected]> | 2021-08-27 18:27:16 +0800 |
|---|---|---|
| committer | loyalsoldier <[email protected]> | 2021-08-29 20:09:57 +0800 |
| commit | 85a343aca99d864c517f13cd3169ebcc910ec0d8 (patch) | |
| tree | eccfd3680d9dc6e22f265a9525dccac85902c2ab /plugin/plaintext/text_out.go | |
| parent | 2b32e8845d9e55b6c23ebb41bd0f382100094386 (diff) | |
Refactor: use plugin architecture to support multiple I/O formats
Diffstat (limited to 'plugin/plaintext/text_out.go')
| -rw-r--r-- | plugin/plaintext/text_out.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/plugin/plaintext/text_out.go b/plugin/plaintext/text_out.go new file mode 100644 index 00000000..7bdfaf42 --- /dev/null +++ b/plugin/plaintext/text_out.go @@ -0,0 +1,78 @@ +package plaintext + +import ( + "encoding/json" + "log" + "strings" + + "github.com/Loyalsoldier/geoip/lib" +) + +const ( + typeTextOut = "text" + descTextOut = "Convert data to plaintext CIDR format" +) + +func init() { + lib.RegisterOutputConfigCreator(typeTextOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { + return newTextOut(typeTextOut, action, data) + }) + lib.RegisterOutputConverter(typeTextOut, &textOut{ + Description: descTextOut, + }) +} + +func (t *textOut) GetType() string { + return t.Type +} + +func (t *textOut) GetAction() lib.Action { + return t.Action +} + +func (t *textOut) GetDescription() string { + return t.Description +} + +func (t *textOut) Output(container lib.Container) error { + // Filter want list + wantList := make(map[string]bool) + for _, want := range t.Want { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + + switch len(wantList) { + case 0: + for entry := range container.Loop() { + data, err := t.marshalBytes(entry) + if err != nil { + return err + } + filename := strings.ToLower(entry.GetName()) + ".txt" + if err := t.writeFile(filename, data); err != nil { + return err + } + } + + default: + for name := range wantList { + entry, found := container.GetEntry(name) + if !found { + log.Printf("entry %s not found", name) + continue + } + data, err := t.marshalBytes(entry) + if err != nil { + return err + } + filename := strings.ToLower(entry.GetName()) + ".txt" + if err := t.writeFile(filename, data); err != nil { + return err + } + } + } + + return nil +} |
