@@ -5,75 +5,10 @@ import (
5
5
"strings"
6
6
7
7
"github.com/Loyalsoldier/geoip/lib"
8
+ "github.com/Loyalsoldier/geoip/plugin/special"
8
9
"github.com/spf13/cobra"
9
10
)
10
11
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
-
77
12
func init () {
78
13
rootCmd .AddCommand (mergeCmd )
79
14
mergeCmd .PersistentFlags ().StringP ("onlyiptype" , "t" , "" , "The only IP type to output, available options: \" ipv4\" , \" ipv6\" " )
@@ -91,27 +26,53 @@ var mergeCmd = &cobra.Command{
91
26
log .Fatal ("invalid argument onlyiptype: " , otype )
92
27
}
93
28
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
-
104
29
instance , err := lib .NewInstance ()
105
30
if err != nil {
106
31
log .Fatal (err )
107
32
}
108
33
109
- if err := instance .InitConfigFromBytes (configBytes ); err != nil {
110
- log .Fatal (err )
111
- }
34
+ instance .AddInput (getInputForMerge ())
35
+ instance .AddOutput (getOutputForMerge (otype ))
112
36
113
37
if err := instance .Run (); err != nil {
114
38
log .Fatal (err )
115
39
}
116
40
},
117
41
}
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