Skip to content

Commit 3ea8192

Browse files
committed
Refactor: merge command
1 parent 7f5d3a7 commit 3ea8192

File tree

1 file changed

+40
-79
lines changed

1 file changed

+40
-79
lines changed

merge.go

Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,10 @@ import (
55
"strings"
66

77
"github.com/Loyalsoldier/geoip/lib"
8+
"github.com/Loyalsoldier/geoip/plugin/special"
89
"github.com/spf13/cobra"
910
)
1011

11-
const tempConfig = `
12-
{
13-
"input": [
14-
{
15-
"type": "stdin",
16-
"action": "add",
17-
"args": {
18-
"name": "temp"
19-
}
20-
}
21-
],
22-
"output": [
23-
{
24-
"type": "stdout",
25-
"action": "output"
26-
}
27-
]
28-
}
29-
`
30-
31-
const tempConfigWithIPv4 = `
32-
{
33-
"input": [
34-
{
35-
"type": "stdin",
36-
"action": "add",
37-
"args": {
38-
"name": "temp"
39-
}
40-
}
41-
],
42-
"output": [
43-
{
44-
"type": "stdout",
45-
"action": "output",
46-
"args": {
47-
"onlyIPType": "ipv4"
48-
}
49-
}
50-
]
51-
}
52-
`
53-
54-
const tempConfigWithIPv6 = `
55-
{
56-
"input": [
57-
{
58-
"type": "stdin",
59-
"action": "add",
60-
"args": {
61-
"name": "temp"
62-
}
63-
}
64-
],
65-
"output": [
66-
{
67-
"type": "stdout",
68-
"action": "output",
69-
"args": {
70-
"onlyIPType": "ipv6"
71-
}
72-
}
73-
]
74-
}
75-
`
76-
7712
func init() {
7813
rootCmd.AddCommand(mergeCmd)
7914
mergeCmd.PersistentFlags().StringP("onlyiptype", "t", "", "The only IP type to output, available options: \"ipv4\", \"ipv6\"")
@@ -91,27 +26,53 @@ var mergeCmd = &cobra.Command{
9126
log.Fatal("invalid argument onlyiptype: ", otype)
9227
}
9328

94-
var configBytes []byte
95-
switch lib.IPType(otype) {
96-
case lib.IPv4:
97-
configBytes = []byte(tempConfigWithIPv4)
98-
case lib.IPv6:
99-
configBytes = []byte(tempConfigWithIPv6)
100-
default:
101-
configBytes = []byte(tempConfig)
102-
}
103-
10429
instance, err := lib.NewInstance()
10530
if err != nil {
10631
log.Fatal(err)
10732
}
10833

109-
if err := instance.InitConfigFromBytes(configBytes); err != nil {
110-
log.Fatal(err)
111-
}
34+
instance.AddInput(getInputForMerge())
35+
instance.AddOutput(getOutputForMerge(otype))
11236

11337
if err := instance.Run(); err != nil {
11438
log.Fatal(err)
11539
}
11640
},
11741
}
42+
43+
func getInputForMerge() lib.InputConverter {
44+
return &special.Stdin{
45+
Type: special.TypeStdin,
46+
Action: lib.ActionAdd,
47+
Description: special.DescStdin,
48+
Name: "temp",
49+
}
50+
}
51+
52+
func getOutputForMerge(otype string) lib.OutputConverter {
53+
switch lib.IPType(otype) {
54+
case lib.IPv4:
55+
return &special.Stdout{
56+
Type: special.TypeStdout,
57+
Action: lib.ActionOutput,
58+
Description: special.DescStdout,
59+
OnlyIPType: lib.IPv4,
60+
}
61+
62+
case lib.IPv6:
63+
return &special.Stdout{
64+
Type: special.TypeStdout,
65+
Action: lib.ActionOutput,
66+
Description: special.DescStdout,
67+
OnlyIPType: lib.IPv6,
68+
}
69+
70+
default:
71+
return &special.Stdout{
72+
Type: special.TypeStdout,
73+
Action: lib.ActionOutput,
74+
Description: special.DescStdout,
75+
}
76+
}
77+
78+
}

0 commit comments

Comments
 (0)