summaryrefslogtreecommitdiff
path: root/plugin/plaintext
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <[email protected]>2026-04-28 18:24:00 +0000
committerGitHub <[email protected]>2026-04-28 18:24:00 +0000
commit8834c0be63ee88e0ad23fe621d5f5fe344b32089 (patch)
tree49029d2fd927e8832d05420d1bf1cf850aa22325 /plugin/plaintext
parent4f125e579472e5ed87fd052ef68ab80f5fe679b0 (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/plaintext')
-rw-r--r--plugin/plaintext/clash_in.go8
-rw-r--r--plugin/plaintext/clash_out.go8
-rw-r--r--plugin/plaintext/common_in.go18
-rw-r--r--plugin/plaintext/common_out.go136
-rw-r--r--plugin/plaintext/json_in.go4
-rw-r--r--plugin/plaintext/surge_in.go4
-rw-r--r--plugin/plaintext/surge_out.go4
-rw-r--r--plugin/plaintext/text_in.go182
-rw-r--r--plugin/plaintext/text_out.go14
9 files changed, 257 insertions, 121 deletions
diff --git a/plugin/plaintext/clash_in.go b/plugin/plaintext/clash_in.go
index be77cc62..e22156ee 100644
--- a/plugin/plaintext/clash_in.go
+++ b/plugin/plaintext/clash_in.go
@@ -21,16 +21,16 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeClashRuleSetClassicalIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newTextIn(TypeClashRuleSetClassicalIn, DescClashRuleSetClassicalIn, action, data)
+ return NewTextInFromBytes(TypeClashRuleSetClassicalIn, DescClashRuleSetClassicalIn, action, data)
})
- lib.RegisterInputConverter(TypeClashRuleSetClassicalIn, &TextIn{
+ lib.RegisterInputConverter(TypeClashRuleSetClassicalIn, &textIn{
Description: DescClashRuleSetClassicalIn,
})
lib.RegisterInputConfigCreator(TypeClashRuleSetIPCIDRIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newTextIn(TypeClashRuleSetIPCIDRIn, DescClashRuleSetIPCIDRIn, action, data)
+ return NewTextInFromBytes(TypeClashRuleSetIPCIDRIn, DescClashRuleSetIPCIDRIn, action, data)
})
- lib.RegisterInputConverter(TypeClashRuleSetIPCIDRIn, &TextIn{
+ lib.RegisterInputConverter(TypeClashRuleSetIPCIDRIn, &textIn{
Description: DescClashRuleSetIPCIDRIn,
})
}
diff --git a/plugin/plaintext/clash_out.go b/plugin/plaintext/clash_out.go
index e7a97f1b..06dffa04 100644
--- a/plugin/plaintext/clash_out.go
+++ b/plugin/plaintext/clash_out.go
@@ -21,16 +21,16 @@ const (
func init() {
lib.RegisterOutputConfigCreator(TypeClashRuleSetClassicalOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newTextOut(TypeClashRuleSetClassicalOut, DescClashRuleSetClassicalOut, action, data)
+ return NewTextOutFromBytes(TypeClashRuleSetClassicalOut, DescClashRuleSetClassicalOut, action, data)
})
- lib.RegisterOutputConverter(TypeClashRuleSetClassicalOut, &TextOut{
+ lib.RegisterOutputConverter(TypeClashRuleSetClassicalOut, &textOut{
Description: DescClashRuleSetClassicalOut,
})
lib.RegisterOutputConfigCreator(TypeClashRuleSetIPCIDROut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newTextOut(TypeClashRuleSetIPCIDROut, DescClashRuleSetIPCIDROut, action, data)
+ return NewTextOutFromBytes(TypeClashRuleSetIPCIDROut, DescClashRuleSetIPCIDROut, action, data)
})
- lib.RegisterOutputConverter(TypeClashRuleSetIPCIDROut, &TextOut{
+ lib.RegisterOutputConverter(TypeClashRuleSetIPCIDROut, &textOut{
Description: DescClashRuleSetIPCIDROut,
})
}
diff --git a/plugin/plaintext/common_in.go b/plugin/plaintext/common_in.go
index f8253519..4f7ea016 100644
--- a/plugin/plaintext/common_in.go
+++ b/plugin/plaintext/common_in.go
@@ -11,7 +11,7 @@ import (
"gopkg.in/yaml.v2"
)
-type TextIn struct {
+type textIn struct {
Type string
Action lib.Action
Description string
@@ -27,7 +27,7 @@ type TextIn struct {
RemoveSuffixesInLine []string
}
-func (t *TextIn) scanFile(reader io.Reader, entry *lib.Entry) error {
+func (t *textIn) scanFile(reader io.Reader, entry *lib.Entry) error {
var err error
switch t.Type {
case TypeTextIn:
@@ -47,7 +47,7 @@ func (t *TextIn) scanFile(reader io.Reader, entry *lib.Entry) error {
return err
}
-func (t *TextIn) scanFileForTextIn(reader io.Reader, entry *lib.Entry) error {
+func (t *textIn) scanFileForTextIn(reader io.Reader, entry *lib.Entry) error {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
@@ -83,7 +83,7 @@ func (t *TextIn) scanFileForTextIn(reader io.Reader, entry *lib.Entry) error {
return nil
}
-func (t *TextIn) readClashRuleSetYAMLFile(reader io.Reader) ([]string, error) {
+func (t *textIn) readClashRuleSetYAMLFile(reader io.Reader) ([]string, error) {
var payload struct {
Payload []string `yaml:"payload"`
}
@@ -100,7 +100,7 @@ func (t *TextIn) readClashRuleSetYAMLFile(reader io.Reader) ([]string, error) {
return payload.Payload, nil
}
-func (t *TextIn) scanFileForClashIPCIDRRuleSetIn(reader io.Reader, entry *lib.Entry) error {
+func (t *textIn) scanFileForClashIPCIDRRuleSetIn(reader io.Reader, entry *lib.Entry) error {
payload, err := t.readClashRuleSetYAMLFile(reader)
if err != nil {
return err
@@ -119,7 +119,7 @@ func (t *TextIn) scanFileForClashIPCIDRRuleSetIn(reader io.Reader, entry *lib.En
return nil
}
-func (t *TextIn) scanFileForClashClassicalRuleSetIn(reader io.Reader, entry *lib.Entry) error {
+func (t *textIn) scanFileForClashClassicalRuleSetIn(reader io.Reader, entry *lib.Entry) error {
payload, err := t.readClashRuleSetYAMLFile(reader)
if err != nil {
return err
@@ -154,7 +154,7 @@ func (t *TextIn) scanFileForClashClassicalRuleSetIn(reader io.Reader, entry *lib
return nil
}
-func (t *TextIn) scanFileForSurgeRuleSetIn(reader io.Reader, entry *lib.Entry) error {
+func (t *textIn) scanFileForSurgeRuleSetIn(reader io.Reader, entry *lib.Entry) error {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
@@ -193,7 +193,7 @@ func (t *TextIn) scanFileForSurgeRuleSetIn(reader io.Reader, entry *lib.Entry) e
return nil
}
-func (t *TextIn) scanFileForJSONIn(reader io.Reader, entry *lib.Entry) error {
+func (t *textIn) scanFileForJSONIn(reader io.Reader, entry *lib.Entry) error {
data, err := io.ReadAll(reader)
if err != nil {
return err
@@ -217,7 +217,7 @@ func (t *TextIn) scanFileForJSONIn(reader io.Reader, entry *lib.Entry) error {
return nil
}
-func (t *TextIn) processJSONResult(result gjson.Result, entry *lib.Entry) error {
+func (t *textIn) processJSONResult(result gjson.Result, entry *lib.Entry) error {
switch {
case !result.Exists():
return fmt.Errorf("invaild IP address or CIDR (value not exist), please check your specified JSON path or JSON source")
diff --git a/plugin/plaintext/common_out.go b/plugin/plaintext/common_out.go
index cc7ba41e..c6588c51 100644
--- a/plugin/plaintext/common_out.go
+++ b/plugin/plaintext/common_out.go
@@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
+ "strings"
"github.com/Loyalsoldier/geoip/lib"
)
@@ -18,7 +19,7 @@ var (
defaultOutputDirForSurgeRuleSetOut = filepath.Join("./", "output", "surge")
)
-type TextOut struct {
+type textOut struct {
Type string
Action lib.Action
Description string
@@ -32,7 +33,84 @@ type TextOut struct {
AddSuffixInLine string
}
-func newTextOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
+func NewTextOut(iType string, iDesc string, action lib.Action, opts ...lib.OutputOption) lib.OutputConverter {
+ t := &textOut{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(t)
+ }
+ }
+
+ return t
+}
+
+func WithTextOutOutputDir(iType, dir string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" {
+ switch iType {
+ case TypeTextOut:
+ dir = defaultOutputDirForTextOut
+ case TypeClashRuleSetClassicalOut:
+ dir = defaultOutputDirForClashRuleSetClassicalOut
+ case TypeClashRuleSetIPCIDROut:
+ dir = defaultOutputDirForClashRuleSetIPCIDROut
+ case TypeSurgeRuleSetOut:
+ dir = defaultOutputDirForSurgeRuleSetOut
+ }
+ }
+
+ s.(*textOut).OutputDir = dir
+ }
+}
+
+func WithTextOutOutputExt(ext string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ ext = strings.TrimSpace(ext)
+ if ext == "" {
+ ext = ".txt"
+ }
+
+ s.(*textOut).OutputExt = ext
+ }
+}
+
+func WithTextOutWantedList(lists []string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*textOut).Want = lists
+ }
+}
+
+func WithTextOutExcludedList(lists []string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*textOut).Exclude = lists
+ }
+}
+
+func WithTextOutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*textOut).OnlyIPType = onlyIPType
+ }
+}
+
+func WithTextOutAddPrefixInLine(prefix string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*textOut).AddPrefixInLine = prefix
+ }
+}
+
+func WithTextOutAddSuffixInLine(suffix string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*textOut).AddSuffixInLine = suffix
+ }
+}
+
+func NewTextOutFromBytes(iType string, iDesc string, action lib.Action, data []byte) (lib.OutputConverter, error) {
var tmp struct {
OutputDir string `json:"outputDir"`
OutputExt string `json:"outputExtension"`
@@ -50,39 +128,21 @@ func newTextOut(iType string, iDesc string, action lib.Action, data json.RawMess
}
}
- if tmp.OutputDir == "" {
- switch iType {
- case TypeTextOut:
- tmp.OutputDir = defaultOutputDirForTextOut
- case TypeClashRuleSetClassicalOut:
- tmp.OutputDir = defaultOutputDirForClashRuleSetClassicalOut
- case TypeClashRuleSetIPCIDROut:
- tmp.OutputDir = defaultOutputDirForClashRuleSetIPCIDROut
- case TypeSurgeRuleSetOut:
- tmp.OutputDir = defaultOutputDirForSurgeRuleSetOut
- }
- }
-
- if tmp.OutputExt == "" {
- tmp.OutputExt = ".txt"
- }
-
- return &TextOut{
- Type: iType,
- Action: action,
- Description: iDesc,
- OutputDir: tmp.OutputDir,
- OutputExt: tmp.OutputExt,
- Want: tmp.Want,
- Exclude: tmp.Exclude,
- OnlyIPType: tmp.OnlyIPType,
-
- AddPrefixInLine: tmp.AddPrefixInLine,
- AddSuffixInLine: tmp.AddSuffixInLine,
- }, nil
+ return NewTextOut(
+ iType,
+ iDesc,
+ action,
+ WithTextOutOutputDir(iType, tmp.OutputDir),
+ WithTextOutOutputExt(tmp.OutputExt),
+ WithTextOutWantedList(tmp.Want),
+ WithTextOutExcludedList(tmp.Exclude),
+ WithTextOutOnlyIPType(tmp.OnlyIPType),
+ WithTextOutAddPrefixInLine(tmp.AddPrefixInLine),
+ WithTextOutAddSuffixInLine(tmp.AddSuffixInLine),
+ ), nil
}
-func (t *TextOut) marshalBytes(entry *lib.Entry) ([]byte, error) {
+func (t *textOut) marshalBytes(entry *lib.Entry) ([]byte, error) {
entryCidr, err := entry.MarshalText(lib.GetIgnoreIPType(t.OnlyIPType))
if err != nil {
return nil, err
@@ -108,7 +168,7 @@ func (t *TextOut) marshalBytes(entry *lib.Entry) ([]byte, error) {
return buf.Bytes(), nil
}
-func (t *TextOut) marshalBytesForTextOut(buf *bytes.Buffer, entryCidr []string) error {
+func (t *textOut) marshalBytesForTextOut(buf *bytes.Buffer, entryCidr []string) error {
for _, cidr := range entryCidr {
if t.AddPrefixInLine != "" {
buf.WriteString(t.AddPrefixInLine)
@@ -122,7 +182,7 @@ func (t *TextOut) marshalBytesForTextOut(buf *bytes.Buffer, entryCidr []string)
return nil
}
-func (t *TextOut) marshalBytesForClashRuleSetClassicalOut(buf *bytes.Buffer, entryCidr []string) error {
+func (t *textOut) marshalBytesForClashRuleSetClassicalOut(buf *bytes.Buffer, entryCidr []string) error {
buf.WriteString("payload:\n")
for _, cidr := range entryCidr {
ip, _, err := net.ParseCIDR(cidr)
@@ -141,7 +201,7 @@ func (t *TextOut) marshalBytesForClashRuleSetClassicalOut(buf *bytes.Buffer, ent
return nil
}
-func (t *TextOut) marshalBytesForClashRuleSetIPCIDROut(buf *bytes.Buffer, entryCidr []string) error {
+func (t *textOut) marshalBytesForClashRuleSetIPCIDROut(buf *bytes.Buffer, entryCidr []string) error {
buf.WriteString("payload:\n")
for _, cidr := range entryCidr {
buf.WriteString(" - '")
@@ -152,7 +212,7 @@ func (t *TextOut) marshalBytesForClashRuleSetIPCIDROut(buf *bytes.Buffer, entryC
return nil
}
-func (t *TextOut) marshalBytesForSurgeRuleSetOut(buf *bytes.Buffer, entryCidr []string) error {
+func (t *textOut) marshalBytesForSurgeRuleSetOut(buf *bytes.Buffer, entryCidr []string) error {
for _, cidr := range entryCidr {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
@@ -173,7 +233,7 @@ func (t *TextOut) marshalBytesForSurgeRuleSetOut(buf *bytes.Buffer, entryCidr []
return nil
}
-func (t *TextOut) writeFile(filename string, data []byte) error {
+func (t *textOut) writeFile(filename string, data []byte) error {
if err := os.MkdirAll(t.OutputDir, 0755); err != nil {
return err
}
diff --git a/plugin/plaintext/json_in.go b/plugin/plaintext/json_in.go
index a4643609..9edf221a 100644
--- a/plugin/plaintext/json_in.go
+++ b/plugin/plaintext/json_in.go
@@ -13,10 +13,10 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeJSONIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newTextIn(TypeJSONIn, DescJSONIn, action, data)
+ return NewTextInFromBytes(TypeJSONIn, DescJSONIn, action, data)
})
- lib.RegisterInputConverter(TypeJSONIn, &TextIn{
+ lib.RegisterInputConverter(TypeJSONIn, &textIn{
Description: DescJSONIn,
})
}
diff --git a/plugin/plaintext/surge_in.go b/plugin/plaintext/surge_in.go
index 7621d0a1..a757cbf7 100644
--- a/plugin/plaintext/surge_in.go
+++ b/plugin/plaintext/surge_in.go
@@ -18,9 +18,9 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeSurgeRuleSetIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newTextIn(TypeSurgeRuleSetIn, DescSurgeRuleSetIn, action, data)
+ return NewTextInFromBytes(TypeSurgeRuleSetIn, DescSurgeRuleSetIn, action, data)
})
- lib.RegisterInputConverter(TypeSurgeRuleSetIn, &TextIn{
+ lib.RegisterInputConverter(TypeSurgeRuleSetIn, &textIn{
Description: DescSurgeRuleSetIn,
})
}
diff --git a/plugin/plaintext/surge_out.go b/plugin/plaintext/surge_out.go
index 8fe62fb4..578b440b 100644
--- a/plugin/plaintext/surge_out.go
+++ b/plugin/plaintext/surge_out.go
@@ -18,9 +18,9 @@ const (
func init() {
lib.RegisterOutputConfigCreator(TypeSurgeRuleSetOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newTextOut(TypeSurgeRuleSetOut, DescSurgeRuleSetOut, action, data)
+ return NewTextOutFromBytes(TypeSurgeRuleSetOut, DescSurgeRuleSetOut, action, data)
})
- lib.RegisterOutputConverter(TypeSurgeRuleSetOut, &TextOut{
+ lib.RegisterOutputConverter(TypeSurgeRuleSetOut, &textOut{
Description: DescSurgeRuleSetOut,
})
}
diff --git a/plugin/plaintext/text_in.go b/plugin/plaintext/text_in.go
index a75b1b12..88677ad0 100644
--- a/plugin/plaintext/text_in.go
+++ b/plugin/plaintext/text_in.go
@@ -3,6 +3,7 @@ package plaintext
import (
"encoding/json"
"fmt"
+ "log"
"net/http"
"os"
"path/filepath"
@@ -19,14 +20,118 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeTextIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newTextIn(TypeTextIn, DescTextIn, action, data)
+ return NewTextInFromBytes(TypeTextIn, DescTextIn, action, data)
})
- lib.RegisterInputConverter(TypeTextIn, &TextIn{
+ lib.RegisterInputConverter(TypeTextIn, &textIn{
Description: DescTextIn,
})
}
-func newTextIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+func NewTextIn(iType string, iDesc string, action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ t := &textIn{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(t)
+ }
+ }
+
+ return t
+}
+
+func WithTextInNameAndURI(iType, name, uri string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ t := s.(*textIn)
+ name = strings.TrimSpace(name)
+ uri = strings.TrimSpace(uri)
+
+ if strings.TrimSpace(t.InputDir) == "" {
+ if name == "" {
+ log.Fatalf("❌ [type %s | action %s] missing inputDir or name", iType, t.Action)
+ }
+ if uri == "" && len(t.IPOrCIDR) == 0 {
+ log.Fatalf("❌ [type %s | action %s] missing uri or ipOrCIDR", iType, t.Action)
+ }
+ } else if name != "" || uri != "" {
+ log.Fatalf("❌ [type %s | action %s] inputDir is not allowed to be used with name or uri or ipOrCIDR", iType, t.Action)
+ }
+
+ t.Name = name
+ t.URI = uri
+ }
+}
+
+func WithTextInIPOrCIDR(iType string, ipOrCIDR []string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ t := s.(*textIn)
+ if iType != TypeTextIn && len(ipOrCIDR) > 0 {
+ log.Fatalf("❌ [type %s | action %s] ipOrCIDR is invalid for this input format", iType, t.Action)
+ }
+
+ t.IPOrCIDR = ipOrCIDR
+ }
+}
+
+func WithTextInInputDir(iType, dir string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ t := s.(*textIn)
+ dir = strings.TrimSpace(dir)
+
+ if dir != "" && (strings.TrimSpace(t.Name) != "" || strings.TrimSpace(t.URI) != "" || len(t.IPOrCIDR) > 0) {
+ log.Fatalf("❌ [type %s | action %s] inputDir is not allowed to be used with name or uri or ipOrCIDR", iType, t.Action)
+ }
+
+ t.InputDir = dir
+ }
+}
+
+func WithTextInWantedList(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
+ }
+ }
+
+ s.(*textIn).Want = wantList
+ }
+}
+
+func WithTextInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*textIn).OnlyIPType = onlyIPType
+ }
+}
+
+func WithTextInJSONPath(iType string, paths []string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ t := s.(*textIn)
+ if iType == TypeJSONIn && len(paths) == 0 {
+ log.Fatalf("❌ [type %s | action %s] missing jsonPath", iType, t.Action)
+ }
+
+ t.JSONPath = paths
+ }
+}
+
+func WithTextInRemovePrefixesInLine(prefixes []string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*textIn).RemovePrefixesInLine = prefixes
+ }
+}
+
+func WithTextInRemoveSuffixesInLine(suffixes []string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*textIn).RemoveSuffixesInLine = suffixes
+ }
+}
+
+func NewTextInFromBytes(iType string, iDesc string, action lib.Action, data []byte) (lib.InputConverter, error) {
var tmp struct {
Name string `json:"name"`
URI string `json:"uri"`
@@ -50,63 +155,34 @@ func newTextIn(iType string, iDesc string, action lib.Action, data json.RawMessa
}
}
- if iType != TypeTextIn && len(tmp.IPOrCIDR) > 0 {
- return nil, fmt.Errorf("❌ [type %s | action %s] ipOrCIDR is invalid for this input format", iType, action)
- }
-
- if iType == TypeJSONIn && len(tmp.JSONPath) == 0 {
- return nil, fmt.Errorf("❌ [type %s | action %s] missing jsonPath", iType, action)
- }
-
- if tmp.InputDir == "" {
- if tmp.Name == "" {
- return nil, fmt.Errorf("❌ [type %s | action %s] missing inputDir or name", iType, action)
- }
- if tmp.URI == "" && len(tmp.IPOrCIDR) == 0 {
- return nil, fmt.Errorf("❌ [type %s | action %s] missing uri or ipOrCIDR", iType, action)
- }
- } else if tmp.Name != "" || tmp.URI != "" || len(tmp.IPOrCIDR) > 0 {
- return nil, fmt.Errorf("❌ [type %s | action %s] inputDir is not allowed to be used with name or uri or ipOrCIDR", iType, action)
- }
-
- // 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 &TextIn{
- Type: iType,
- Action: action,
- Description: iDesc,
- Name: tmp.Name,
- URI: tmp.URI,
- IPOrCIDR: tmp.IPOrCIDR,
- InputDir: tmp.InputDir,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
-
- JSONPath: tmp.JSONPath,
- RemovePrefixesInLine: tmp.RemovePrefixesInLine,
- RemoveSuffixesInLine: tmp.RemoveSuffixesInLine,
- }, nil
+ return NewTextIn(
+ iType,
+ iDesc,
+ action,
+ WithTextInIPOrCIDR(iType, tmp.IPOrCIDR),
+ WithTextInInputDir(iType, tmp.InputDir),
+ WithTextInNameAndURI(iType, tmp.Name, tmp.URI),
+ WithTextInWantedList(tmp.Want),
+ WithTextInOnlyIPType(tmp.OnlyIPType),
+ WithTextInJSONPath(iType, tmp.JSONPath),
+ WithTextInRemovePrefixesInLine(tmp.RemovePrefixesInLine),
+ WithTextInRemoveSuffixesInLine(tmp.RemoveSuffixesInLine),
+ ), nil
}
-func (t *TextIn) GetType() string {
+func (t *textIn) GetType() string {
return t.Type
}
-func (t *TextIn) GetAction() lib.Action {
+func (t *textIn) GetAction() lib.Action {
return t.Action
}
-func (t *TextIn) GetDescription() string {
+func (t *textIn) GetDescription() string {
return t.Description
}
-func (t *TextIn) Input(container lib.Container) (lib.Container, error) {
+func (t *textIn) Input(container lib.Container) (lib.Container, error) {
entries := make(map[string]*lib.Entry)
var err error
@@ -162,7 +238,7 @@ func (t *TextIn) Input(container lib.Container) (lib.Container, error) {
return container, nil
}
-func (t *TextIn) walkDir(dir string, entries map[string]*lib.Entry) error {
+func (t *textIn) walkDir(dir string, entries map[string]*lib.Entry) error {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -181,7 +257,7 @@ func (t *TextIn) walkDir(dir string, entries map[string]*lib.Entry) error {
return err
}
-func (t *TextIn) walkLocalFile(path, name string, entries map[string]*lib.Entry) error {
+func (t *textIn) walkLocalFile(path, name string, entries map[string]*lib.Entry) error {
entryName := ""
name = strings.TrimSpace(name)
if name != "" {
@@ -225,7 +301,7 @@ func (t *TextIn) walkLocalFile(path, name string, entries map[string]*lib.Entry)
return nil
}
-func (t *TextIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error {
+func (t *textIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error {
resp, err := http.Get(url)
if err != nil {
return err
@@ -252,7 +328,7 @@ func (t *TextIn) walkRemoteFile(url, name string, entries map[string]*lib.Entry)
return nil
}
-func (t *TextIn) appendIPOrCIDR(ipOrCIDR []string, name string, entries map[string]*lib.Entry) error {
+func (t *textIn) appendIPOrCIDR(ipOrCIDR []string, name string, entries map[string]*lib.Entry) error {
name = strings.ToUpper(name)
entry, found := entries[name]
diff --git a/plugin/plaintext/text_out.go b/plugin/plaintext/text_out.go
index 06f4df63..036ccf95 100644
--- a/plugin/plaintext/text_out.go
+++ b/plugin/plaintext/text_out.go
@@ -16,26 +16,26 @@ const (
func init() {
lib.RegisterOutputConfigCreator(TypeTextOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newTextOut(TypeTextOut, DescTextOut, action, data)
+ return NewTextOutFromBytes(TypeTextOut, DescTextOut, action, data)
})
- lib.RegisterOutputConverter(TypeTextOut, &TextOut{
+ lib.RegisterOutputConverter(TypeTextOut, &textOut{
Description: DescTextOut,
})
}
-func (t *TextOut) GetType() string {
+func (t *textOut) GetType() string {
return t.Type
}
-func (t *TextOut) GetAction() lib.Action {
+func (t *textOut) GetAction() lib.Action {
return t.Action
}
-func (t *TextOut) GetDescription() string {
+func (t *textOut) GetDescription() string {
return t.Description
}
-func (t *TextOut) Output(container lib.Container) error {
+func (t *textOut) Output(container lib.Container) error {
for _, name := range t.filterAndSortList(container) {
entry, found := container.GetEntry(name)
if !found {
@@ -57,7 +57,7 @@ func (t *TextOut) Output(container lib.Container) error {
return nil
}
-func (t *TextOut) filterAndSortList(container lib.Container) []string {
+func (t *textOut) filterAndSortList(container lib.Container) []string {
excludeMap := make(map[string]bool)
for _, exclude := range t.Exclude {
if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" {