summaryrefslogtreecommitdiff
path: root/plugin/plaintext/common_out.go
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <[email protected]>2026-03-09 06:35:47 +0000
committercopilot-swe-agent[bot] <[email protected]>2026-03-09 06:35:47 +0000
commit2600825c50d7e7f2b4427674f1aefca270bcca27 (patch)
tree18f216c6af28fd3975a0665d75ccf7b5c0078cb6 /plugin/plaintext/common_out.go
parent5a14eb90f575b983aa407341af91aa7654e65f12 (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/common_out.go')
-rw-r--r--plugin/plaintext/common_out.go134
1 files changed, 96 insertions, 38 deletions
diff --git a/plugin/plaintext/common_out.go b/plugin/plaintext/common_out.go
index cc7ba41e..e508fa9d 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 text_out 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 := &text_out{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(t)
+ }
+ }
+
+ return t
+}
+
+func WithOutputDir(dir string, iType string) lib.OutputOption {
+ return func(t 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
+ }
+ }
+
+ t.(*text_out).OutputDir = dir
+ }
+}
+
+func WithOutputExtension(ext string) lib.OutputOption {
+ return func(t lib.OutputConverter) {
+ ext = strings.TrimSpace(ext)
+ if ext == "" {
+ ext = ".txt"
+ }
+
+ t.(*text_out).OutputExt = ext
+ }
+}
+
+func WithOutputWantedList(lists []string) lib.OutputOption {
+ return func(t lib.OutputConverter) {
+ t.(*text_out).Want = lists
+ }
+}
+
+func WithOutputExcludedList(lists []string) lib.OutputOption {
+ return func(t lib.OutputConverter) {
+ t.(*text_out).Exclude = lists
+ }
+}
+
+func WithOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
+ return func(t lib.OutputConverter) {
+ t.(*text_out).OnlyIPType = onlyIPType
+ }
+}
+
+func WithAddPrefixInLine(prefix string) lib.OutputOption {
+ return func(t lib.OutputConverter) {
+ t.(*text_out).AddPrefixInLine = prefix
+ }
+}
+
+func WithAddSuffixInLine(suffix string) lib.OutputOption {
+ return func(t lib.OutputConverter) {
+ t.(*text_out).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,19 @@ 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,
+ WithOutputDir(tmp.OutputDir, iType),
+ WithOutputExtension(tmp.OutputExt),
+ WithOutputWantedList(tmp.Want),
+ WithOutputExcludedList(tmp.Exclude),
+ WithOutputOnlyIPType(tmp.OnlyIPType),
+ WithAddPrefixInLine(tmp.AddPrefixInLine),
+ WithAddSuffixInLine(tmp.AddSuffixInLine),
+ ), nil
}
-func (t *TextOut) marshalBytes(entry *lib.Entry) ([]byte, error) {
+func (t *text_out) marshalBytes(entry *lib.Entry) ([]byte, error) {
entryCidr, err := entry.MarshalText(lib.GetIgnoreIPType(t.OnlyIPType))
if err != nil {
return nil, err
@@ -108,7 +166,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 *text_out) marshalBytesForTextOut(buf *bytes.Buffer, entryCidr []string) error {
for _, cidr := range entryCidr {
if t.AddPrefixInLine != "" {
buf.WriteString(t.AddPrefixInLine)
@@ -122,7 +180,7 @@ func (t *TextOut) marshalBytesForTextOut(buf *bytes.Buffer, entryCidr []string)
return nil
}
-func (t *TextOut) marshalBytesForClashRuleSetClassicalOut(buf *bytes.Buffer, entryCidr []string) error {
+func (t *text_out) marshalBytesForClashRuleSetClassicalOut(buf *bytes.Buffer, entryCidr []string) error {
buf.WriteString("payload:\n")
for _, cidr := range entryCidr {
ip, _, err := net.ParseCIDR(cidr)
@@ -141,7 +199,7 @@ func (t *TextOut) marshalBytesForClashRuleSetClassicalOut(buf *bytes.Buffer, ent
return nil
}
-func (t *TextOut) marshalBytesForClashRuleSetIPCIDROut(buf *bytes.Buffer, entryCidr []string) error {
+func (t *text_out) marshalBytesForClashRuleSetIPCIDROut(buf *bytes.Buffer, entryCidr []string) error {
buf.WriteString("payload:\n")
for _, cidr := range entryCidr {
buf.WriteString(" - '")
@@ -152,7 +210,7 @@ func (t *TextOut) marshalBytesForClashRuleSetIPCIDROut(buf *bytes.Buffer, entryC
return nil
}
-func (t *TextOut) marshalBytesForSurgeRuleSetOut(buf *bytes.Buffer, entryCidr []string) error {
+func (t *text_out) marshalBytesForSurgeRuleSetOut(buf *bytes.Buffer, entryCidr []string) error {
for _, cidr := range entryCidr {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
@@ -173,7 +231,7 @@ func (t *TextOut) marshalBytesForSurgeRuleSetOut(buf *bytes.Buffer, entryCidr []
return nil
}
-func (t *TextOut) writeFile(filename string, data []byte) error {
+func (t *text_out) writeFile(filename string, data []byte) error {
if err := os.MkdirAll(t.OutputDir, 0755); err != nil {
return err
}