Skip to content

Commit 9824fce

Browse files
committed
Add a flag to include networks without data
1 parent e44fb7f commit 9824fce

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
matches, it will be treated as if multiple `-db` arguments were provided.
1717
Note that you must quote the parameter when using globs to prevent the
1818
shell's globbing from interfering. See the [pattern syntax](https://pkg.go.dev/path#Match)
19+
* You may now include networks with no data in the output by using the
20+
`include-networks-without-data` flag.
1921
* This repo no longer provides a public Go API. It is only intended to be
2022
used as a CLI program.
2123

cmd/mmdbinspect/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ func main() {
2828
var mmdb arrayFlags
2929

3030
flag.Var(&mmdb, "db", "Path to an mmdb file. You may pass this arg more than once.")
31+
3132
includeAliasedNetworks := flag.Bool(
3233
"include-aliased-networks",
3334
false,
3435
"Include aliased networks (e.g. 6to4, Teredo). This option may cause IPv4 networks to be listed more than once via aliases.", //nolint: lll
3536
)
3637

38+
includeNetworksWithoutData := flag.Bool(
39+
"include-networks-without-data",
40+
false,
41+
`Include networks that have no data in the database. The "record" will be null for these.`,
42+
)
43+
3744
useJSONL := flag.Bool("jsonl", false, "Output as JSONL instead of YAML.")
3845

3946
flag.Usage = usage
@@ -65,7 +72,12 @@ func main() {
6572
encoder = yaml.NewEncoder(w)
6673
}
6774

68-
iterator := records(network, mmdb, *includeAliasedNetworks)
75+
iterator := records(
76+
network,
77+
mmdb,
78+
*includeAliasedNetworks,
79+
*includeNetworksWithoutData,
80+
)
6981

7082
for r, err := range iterator {
7183
if err != nil {

cmd/mmdbinspect/mmdbinspect.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@ type record struct {
1818
DatabasePath string `json:"database_path"`
1919
RequestedLookup string `json:"requested_lookup"`
2020
Network netip.Prefix `json:"network"`
21-
Record any `json:"record"`
21+
Record any `json:"record,omitempty"`
2222
}
2323

2424
// records returns an iterator over the records for the networks and
2525
// databases provided.
2626
func records(
2727
networks, databases []string,
28-
includeAliasedNetworks bool,
28+
includeAliasedNetworks,
29+
includeNetworksWithoutData bool,
2930
) iter.Seq2[*record, error] {
31+
var opts []maxminddb.NetworksOption
32+
if includeAliasedNetworks {
33+
opts = append(opts, maxminddb.IncludeAliasedNetworks)
34+
}
35+
if includeNetworksWithoutData {
36+
opts = append(opts, maxminddb.IncludeNetworksWithoutData)
37+
}
38+
3039
return func(yield func(*record, error) bool) {
3140
for _, glob := range databases {
3241
matches, err := filepath.Glob(glob)
@@ -46,7 +55,7 @@ func records(
4655
DatabasePath: path,
4756
RequestedLookup: thisNetwork,
4857
}
49-
ok := recordsForNetwork(reader, includeAliasedNetworks, baseRecord, yield)
58+
ok := recordsForNetwork(reader, opts, baseRecord, yield)
5059
if !ok {
5160
return
5261
}
@@ -62,7 +71,7 @@ func records(
6271
// network containing a single address (i.e., /32 for IPv4 and /128 for IPv6).
6372
func recordsForNetwork(
6473
reader *maxminddb.Reader,
65-
includeAliasedNetworks bool,
74+
opts []maxminddb.NetworksOption,
6675
record record,
6776
yield func(*record, error) bool,
6877
) bool {
@@ -89,11 +98,6 @@ func recordsForNetwork(
8998
network = netip.PrefixFrom(addr, bits)
9099
}
91100

92-
var opts []maxminddb.NetworksOption
93-
if includeAliasedNetworks {
94-
opts = append(opts, maxminddb.IncludeAliasedNetworks)
95-
}
96-
97101
for res := range reader.NetworksWithin(network, opts...) {
98102
record.Network = res.Prefix()
99103

cmd/mmdbinspect/mmdbinspect_test.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ var country81_2_69_142 = map[string]any{
141141

142142
func TestRecords(t *testing.T) {
143143
tests := []struct {
144-
name string
145-
dbs []string
146-
networks []string
147-
expectRecords []record
148-
expectErr string
144+
name string
145+
dbs []string
146+
networks []string
147+
includeNetworksWithoutData bool
148+
expectRecords []record
149+
expectErr string
149150
}{
150151
{
151152
name: "multiple non-glob paths and multiple IPs",
@@ -186,10 +187,23 @@ func TestRecords(t *testing.T) {
186187
},
187188
},
188189
{
189-
name: "network missing from DB",
190+
name: "network missing from DB without including empty networks",
190191
dbs: []string{CityDBPath},
191192
networks: []string{"10.0.0.0"},
192193
},
194+
{
195+
name: "network missing from DB with including empty networks",
196+
dbs: []string{CityDBPath},
197+
networks: []string{"10.0.0.0"},
198+
includeNetworksWithoutData: true,
199+
expectRecords: []record{
200+
{
201+
DatabasePath: CityDBPath,
202+
RequestedLookup: "10.0.0.0",
203+
Network: netip.MustParsePrefix("10.0.0.0/8"),
204+
},
205+
},
206+
},
193207
{
194208
name: "file does not exist",
195209
dbs: []string{"does/not/exist.mmdb"},
@@ -213,7 +227,7 @@ func TestRecords(t *testing.T) {
213227
for _, test := range tests {
214228
t.Run(test.name, func(t *testing.T) {
215229
var recs []record
216-
for record, err := range records(test.networks, test.dbs, false) {
230+
for record, err := range records(test.networks, test.dbs, false, test.includeNetworksWithoutData) {
217231
// For now, we don't test errors that happen half way through an
218232
// iteration. If we want to in the future, we will need to rework
219233
// this a bit.

0 commit comments

Comments
 (0)