diff options
| author | copilot-swe-agent[bot] <[email protected]> | 2026-03-09 06:35:47 +0000 |
|---|---|---|
| committer | copilot-swe-agent[bot] <[email protected]> | 2026-03-09 06:35:47 +0000 |
| commit | 2600825c50d7e7f2b4427674f1aefca270bcca27 (patch) | |
| tree | 18f216c6af28fd3975a0665d75ccf7b5c0078cb6 /plugin/plaintext/text_in.go | |
| parent | 5a14eb90f575b983aa407341af91aa7654e65f12 (diff) | |
Refactor: all plugin subdirectories use option patterncopilot/modify-plugin-files-subdirectories
Apply the same functional options pattern from plugin/singbox to:
- plugin/mihomo (mrs_in.go, mrs_out.go)
- plugin/plaintext (text_in.go, common_in.go, common_out.go, text_out.go, clash_in.go, clash_out.go, json_in.go, surge_in.go, surge_out.go)
- plugin/maxmind (all input/output files)
- plugin/v2ray (dat_in.go, dat_out.go)
- plugin/special (cutter.go, lookup.go, private.go, stdin.go, stdout.go)
- lookup.go and merge.go updated to use new constructors
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/plaintext/text_in.go')
| -rw-r--r-- | plugin/plaintext/text_in.go | 128 |
1 files changed, 94 insertions, 34 deletions
diff --git a/plugin/plaintext/text_in.go b/plugin/plaintext/text_in.go index a75b1b12..40572e2e 100644 --- a/plugin/plaintext/text_in.go +++ b/plugin/plaintext/text_in.go @@ -19,14 +19,86 @@ 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, &text_in{ 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 := &text_in{ + Type: iType, + Action: action, + Description: iDesc, + } + + for _, opt := range opts { + if opt != nil { + opt(t) + } + } + + return t +} + +func WithNameAndURI(name, uri string) lib.InputOption { + return func(t lib.InputConverter) { + t.(*text_in).Name = strings.TrimSpace(name) + t.(*text_in).URI = strings.TrimSpace(uri) + } +} + +func WithIPOrCIDR(ipOrCIDR []string) lib.InputOption { + return func(t lib.InputConverter) { + t.(*text_in).IPOrCIDR = ipOrCIDR + } +} + +func WithInputDir(dir string) lib.InputOption { + return func(t lib.InputConverter) { + t.(*text_in).InputDir = strings.TrimSpace(dir) + } +} + +func WithInputWantedList(lists []string) lib.InputOption { + return func(t lib.InputConverter) { + wantList := make(map[string]bool) + for _, want := range lists { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + + t.(*text_in).Want = wantList + } +} + +func WithInputOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(t lib.InputConverter) { + t.(*text_in).OnlyIPType = onlyIPType + } +} + +func WithJSONPath(jsonPath []string) lib.InputOption { + return func(t lib.InputConverter) { + t.(*text_in).JSONPath = jsonPath + } +} + +func WithRemovePrefixesInLine(prefixes []string) lib.InputOption { + return func(t lib.InputConverter) { + t.(*text_in).RemovePrefixesInLine = prefixes + } +} + +func WithRemoveSuffixesInLine(suffixes []string) lib.InputOption { + return func(t lib.InputConverter) { + t.(*text_in).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"` @@ -69,44 +141,32 @@ func newTextIn(iType string, iDesc string, action lib.Action, data json.RawMessa 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, + WithNameAndURI(tmp.Name, tmp.URI), + WithIPOrCIDR(tmp.IPOrCIDR), + WithInputDir(tmp.InputDir), + WithInputWantedList(tmp.Want), + WithInputOnlyIPType(tmp.OnlyIPType), + WithJSONPath(tmp.JSONPath), + WithRemovePrefixesInLine(tmp.RemovePrefixesInLine), + WithRemoveSuffixesInLine(tmp.RemoveSuffixesInLine), + ), nil } -func (t *TextIn) GetType() string { +func (t *text_in) GetType() string { return t.Type } -func (t *TextIn) GetAction() lib.Action { +func (t *text_in) GetAction() lib.Action { return t.Action } -func (t *TextIn) GetDescription() string { +func (t *text_in) GetDescription() string { return t.Description } -func (t *TextIn) Input(container lib.Container) (lib.Container, error) { +func (t *text_in) Input(container lib.Container) (lib.Container, error) { entries := make(map[string]*lib.Entry) var err error @@ -162,7 +222,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 *text_in) 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 +241,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 *text_in) walkLocalFile(path, name string, entries map[string]*lib.Entry) error { entryName := "" name = strings.TrimSpace(name) if name != "" { @@ -225,7 +285,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 *text_in) walkRemoteFile(url, name string, entries map[string]*lib.Entry) error { resp, err := http.Get(url) if err != nil { return err @@ -252,7 +312,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 *text_in) appendIPOrCIDR(ipOrCIDR []string, name string, entries map[string]*lib.Entry) error { name = strings.ToUpper(name) entry, found := entries[name] |
