-
Notifications
You must be signed in to change notification settings - Fork 656
/
merge.go
78 lines (65 loc) · 1.75 KB
/
merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"log"
"strings"
"github.com/Loyalsoldier/geoip/lib"
"github.com/Loyalsoldier/geoip/plugin/special"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(mergeCmd)
mergeCmd.PersistentFlags().StringP("onlyiptype", "t", "", "The only IP type to output, available options: \"ipv4\", \"ipv6\"")
}
var mergeCmd = &cobra.Command{
Use: "merge",
Aliases: []string{"m"},
Short: "Merge plaintext IP & CIDR from standard input, then print to standard output",
Run: func(cmd *cobra.Command, args []string) {
otype, _ := cmd.Flags().GetString("onlyiptype")
otype = strings.ToLower(strings.TrimSpace(otype))
if otype != "" && otype != "ipv4" && otype != "ipv6" {
log.Fatal("invalid argument onlyiptype: ", otype)
}
instance, err := lib.NewInstance()
if err != nil {
log.Fatal(err)
}
instance.AddInput(getInputForMerge())
instance.AddOutput(getOutputForMerge(otype))
if err := instance.Run(); err != nil {
log.Fatal(err)
}
},
}
func getInputForMerge() lib.InputConverter {
return &special.Stdin{
Type: special.TypeStdin,
Action: lib.ActionAdd,
Description: special.DescStdin,
Name: "temp",
}
}
func getOutputForMerge(otype string) lib.OutputConverter {
switch lib.IPType(otype) {
case lib.IPv4:
return &special.Stdout{
Type: special.TypeStdout,
Action: lib.ActionOutput,
Description: special.DescStdout,
OnlyIPType: lib.IPv4,
}
case lib.IPv6:
return &special.Stdout{
Type: special.TypeStdout,
Action: lib.ActionOutput,
Description: special.DescStdout,
OnlyIPType: lib.IPv6,
}
default:
return &special.Stdout{
Type: special.TypeStdout,
Action: lib.ActionOutput,
Description: special.DescStdout,
}
}
}