Skip to content

Commit b2ad3cf

Browse files
committed
Add a flag to include networks without data
1 parent 7a8cd48 commit b2ad3cf

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
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: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ type record struct {
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

0 commit comments

Comments
 (0)