-
Notifications
You must be signed in to change notification settings - Fork 15
/
cmd_import.go
150 lines (135 loc) · 5.18 KB
/
cmd_import.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"github.com/ipinfo/cli/lib/complete"
"github.com/ipinfo/cli/lib/complete/predict"
"github.com/ipinfo/mmdbctl/lib"
"github.com/spf13/pflag"
)
var predictIpVsn = []string{"4", "6"}
var predictSize = []string{"24", "28", "32"}
var predictMerge = []string{"none", "toplevel", "recurse"}
var completionsImport = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
"-i": predict.Nothing,
"--in": predict.Nothing,
"-o": predict.Nothing,
"--out": predict.Nothing,
"-c": predict.Nothing,
"--csv": predict.Nothing,
"-t": predict.Nothing,
"--tsv": predict.Nothing,
"-j": predict.Nothing,
"--json": predict.Nothing,
"-f": predict.Nothing,
"--fields": predict.Nothing,
"--fields-from-header": predict.Nothing,
"--range-multicol": predict.Nothing,
"--joinkey-col": predict.Nothing,
"--no-fields": predict.Nothing,
"--no-network": predict.Nothing,
"--ip": predict.Set(predictIpVsn),
"-s": predict.Set(predictSize),
"--size": predict.Set(predictSize),
"-m": predict.Set(predictMerge),
"--merge": predict.Set(predictMerge),
"--ignore-empty-values": predict.Nothing,
"--disallow-reserved": predict.Nothing,
"--alias-6to4": predict.Nothing,
"--disable-metadata-pointers": predict.Nothing,
},
}
func printHelpImport() {
fmt.Printf(
`Usage: %s import [<opts>] [<input>] [<output>]
Example:
# Imports an input file and outputs an mmdb file with default configurations.
$ %[1]s import input.csv output.mmdb
Options:
General:
--help, -h
show help.
Input/Output:
-i <fname>, --in <fname>
input file name. (e.g. data.csv or - for stdin)
must be in CSV, TSV or JSON.
default: stdin.
-o <fname>, --out <fname>
output file name. (e.g. sample.mmdb)
default: stdout.
-c, --csv
interpret input file as CSV.
by default, the .csv extension will turn this on.
-t, --tsv
interpret input file as TSV.
by default, the .tsv extension will turn this on.
-j, --json
interpret input file as JSON.
by default, the .json extension will turn this on.
Fields:
One of the following fields flags, or other flags that implicitly specify
these, must be used, otherwise --fields-from-header is assumed.
The first field is always implicitly the network field, unless
--range-multicol is used, in which case the first 2 fields are considered
to be start_ip,end_ip.
When specifying --fields, do not specify the network column(s).
-f, --fields <comma-separated-fields>
explicitly specify the fields to assume exist in the input file.
example: col1,col2,col3
default: N/A.
--fields-from-header
assume first line of input file is a header, and set the fields as that.
default: true if no other field source is used, false otherwise.
--range-multicol
assume that the network field is actually two columns start_ip,end_ip.
default: false.
--joinkey-col
assume --range-multicol and that the 3rd column is join_key, and ignore
this column when converting to JSON.
default: false.
--no-fields
specify that no fields exist except the implicit network field.
when enabled, --no-network has no effect; the network field is written.
default: false.
--no-network
if --fields-from-header is set, then don't write the network field, which
is assumed to be the *first* field in the header.
default: false.
Meta:
--ip <4 | 6>
output file's ip version.
default: 6.
-s, --size <24 | 28 | 32>
size of records in the mmdb tree.
default: 32.
-m, --merge <none | toplevel | recurse>
the merge strategy to use when inserting entries that conflict.
none => no merge; only replace conflicts.
toplevel => merge only top-level keys.
recurse => recursively merge.
default: none.
--ignore-empty-values
if enabled, write into /0 with empty values for all fields, and for any
entry, don't write out a field whose value is the empty string.
default: false.
--disallow-reserved
disallow reserved networks to be added to the tree.
default: false.
--alias-6to4
enable the mapping of some IPv6 networks into the IPv4 network, e.g.
::ffff:0:0/96, 2001::/32 & 2002::/16.
default: false.
--disable-metadata-pointers
some mmdb readers fail to properly read pointers within metadata. this
allows turning off such pointers.
default: true.
`, progBase)
}
func cmdImport() error {
f := lib.CmdImportFlags{}
f.Init()
pflag.Parse()
return lib.CmdImport(f, pflag.Args()[1:], printHelpImport)
}