Skip to content

Commit e3c0a34

Browse files
committed
Add ability to output build time
1 parent 26546d6 commit e3c0a34

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
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.
19+
* The following flags were added:
20+
* `-include-networks-without-data` - include networks without any data in
21+
the database in the output.
22+
* `-include-build-time` - include the build time from the database's
23+
metadata in the output.
2124
* This repo no longer provides a public Go API. It is only intended to be
2225
used as a CLI program.
2326

cmd/mmdbinspect/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ func main() {
3535
"Include aliased networks (e.g. 6to4, Teredo). This option may cause IPv4 networks to be listed more than once via aliases.", //nolint: lll
3636
)
3737

38+
includeBuildTime := flag.Bool(
39+
"include-build-time",
40+
false,
41+
"Include the build time of the database in the output.",
42+
)
43+
3844
includeNetworksWithoutData := flag.Bool(
3945
"include-networks-without-data",
4046
false,
@@ -78,6 +84,7 @@ func main() {
7884
network,
7985
mmdb,
8086
*includeAliasedNetworks,
87+
*includeBuildTime,
8188
*includeNetworksWithoutData,
8289
)
8390

cmd/mmdbinspect/mmdbinspect.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import (
99
"os"
1010
"path/filepath"
1111
"strings"
12+
"time"
1213

1314
"github.com/oschwald/maxminddb-golang/v2"
1415
)
1516

16-
// record holds the records for a lookup in a database.
17+
// record holds the records for a lookup in a database. Note that
18+
// the order here affects the output order
1719
type record struct {
1820
DatabasePath string `json:"database_path"`
21+
BuildTime *time.Time `json:"build_time,omitempty"`
1922
RequestedLookup string `json:"requested_lookup"`
2023
Network netip.Prefix `json:"network"`
2124
Record any `json:"record,omitempty"`
@@ -26,6 +29,7 @@ type record struct {
2629
func records(
2730
networks, databases []string,
2831
includeAliasedNetworks,
32+
includeBuildTime,
2933
includeNetworksWithoutData bool,
3034
) iter.Seq2[*record, error] {
3135
var opts []maxminddb.NetworksOption
@@ -50,9 +54,16 @@ func records(
5054
return
5155
}
5256

57+
var buildTime *time.Time
58+
if includeBuildTime {
59+
t := time.Unix(int64(reader.Metadata.BuildEpoch), 0).UTC()
60+
buildTime = &t
61+
}
62+
5363
for _, thisNetwork := range networks {
5464
baseRecord := record{
5565
DatabasePath: path,
66+
BuildTime: buildTime,
5667
RequestedLookup: thisNetwork,
5768
}
5869
ok := recordsForNetwork(reader, opts, baseRecord, yield)

cmd/mmdbinspect/mmdbinspect_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/netip"
55
"path/filepath"
66
"testing"
7+
"time"
78

89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
@@ -140,10 +141,13 @@ var country81_2_69_142 = map[string]any{
140141
}
141142

142143
func TestRecords(t *testing.T) {
144+
countryBuildTime := time.Date(2019, 11, 4, 16, 30, 59, 0, time.UTC)
145+
143146
tests := []struct {
144147
name string
145148
dbs []string
146149
networks []string
150+
includeBuildTime bool
147151
includeNetworksWithoutData bool
148152
expectRecords []record
149153
expectErr string
@@ -167,6 +171,21 @@ func TestRecords(t *testing.T) {
167171
},
168172
},
169173
},
174+
{
175+
name: "with build time",
176+
dbs: []string{CountryDBPath},
177+
networks: []string{"81.2.69.142"},
178+
includeBuildTime: true,
179+
expectRecords: []record{
180+
{
181+
DatabasePath: CountryDBPath,
182+
BuildTime: &countryBuildTime,
183+
RequestedLookup: "81.2.69.142",
184+
Network: netip.MustParsePrefix("81.2.69.142/31"),
185+
Record: country81_2_69_142,
186+
},
187+
},
188+
},
170189
{
171190
name: "glob path",
172191
dbs: []string{filepath.Join(testDataDir, "GeoIP2-C*y-Test.mmdb")},
@@ -227,7 +246,7 @@ func TestRecords(t *testing.T) {
227246
for _, test := range tests {
228247
t.Run(test.name, func(t *testing.T) {
229248
var recs []record
230-
for record, err := range records(test.networks, test.dbs, false, test.includeNetworksWithoutData) {
249+
for record, err := range records(test.networks, test.dbs, false, test.includeBuildTime, test.includeNetworksWithoutData) {
231250
// For now, we don't test errors that happen half way through an
232251
// iteration. If we want to in the future, we will need to rework
233252
// this a bit.

0 commit comments

Comments
 (0)