Skip to content

Commit

Permalink
Merge pull request #83 from maxmind/greg/eng-1012-all-githubcom-repos…
Browse files Browse the repository at this point in the history
…-are-upgraded-to

Upgrade to github.com/oschwald/maxminddb-golang/v2
  • Loading branch information
horgh authored Jan 16, 2025
2 parents 60b1337 + 5fc10f3 commit 7d50614
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
build:
strategy:
matrix:
go-version: [1.21.x, 1.22.x]
go-version: [1.23.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
name: "Build ${{ matrix.go-version }} test on ${{ matrix.platform }}"
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.0.0

* Upgrade to `github.com/oschwald/maxminddb-golang/v2`. This is a breaking
API change, but should not affect the use of the program.

## 0.2.0 (2024-01-10)

* Don't escape `&`, `<`, and `>` in JSON output
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ _We aim always to support the current and penultimate major releases of the Go c
The easiest way is via `go install`:

```bash
$ go install github.com/maxmind/mmdbinspect/cmd/mmdbinspect@latest
$ go install github.com/maxmind/mmdbinspect/v2/cmd/mmdbinspect@latest
```

This installs `mmdbinspect` to `$GOPATH/bin/mmdbinspect`.
Expand Down Expand Up @@ -473,6 +473,6 @@ Please report bugs by filing an issue with our GitHub issue tracker at [https://

## Copyright and License

This software is Copyright (c) 2019 - 2024 by MaxMind, Inc.
This software is Copyright (c) 2019 - 2025 by MaxMind, Inc.

This is free software, licensed under the [Apache License, Version 2.0](LICENSE-APACHE) or the [MIT License](LICENSE-MIT), at your option.
2 changes: 1 addition & 1 deletion cmd/mmdbinspect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"strings"

"github.com/maxmind/mmdbinspect/pkg/mmdbinspect"
"github.com/maxmind/mmdbinspect/v2/pkg/mmdbinspect"
)

type arrayFlags []string
Expand Down
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module github.com/maxmind/mmdbinspect
module github.com/maxmind/mmdbinspect/v2

go 1.21

toolchain go1.22.3
go 1.23

require (
github.com/oschwald/maxminddb-golang v1.13.1
github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.2
github.com/stretchr/testify v1.10.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.29.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/oschwald/maxminddb-golang v1.13.1 h1:G3wwjdN9JmIK2o/ermkHM+98oX5fS+k5MbwsmL4MRQE=
github.com/oschwald/maxminddb-golang v1.13.1/go.mod h1:K4pgV9N/GcK694KSTmVSDTODk4IsCNThNdTmnaBZ/F8=
github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.2 h1:jG+FaCBv3h6GD5F+oenTfe3+0NmX8sCKjni5k3A5Dek=
github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.2/go.mod h1:rHaQJ5SjfCdL4sqCKa3FhklRcaXga2/qyvmQuA+ZJ6M=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
32 changes: 13 additions & 19 deletions pkg/mmdbinspect/mmdbinspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"errors"
"fmt"
"io/fs"
"net"
"net/netip"
"os"
"strings"

"github.com/oschwald/maxminddb-golang"
"github.com/oschwald/maxminddb-golang/v2"
)

// RecordForNetwork holds a network and the corresponding record.
type RecordForNetwork struct {
Network string
Network netip.Prefix
Record any
}

Expand Down Expand Up @@ -46,9 +46,9 @@ func OpenDB(path string) (*maxminddb.Reader, error) {
return db, nil
}

// RecordsForNetwork fetches mmdb records inside a given network. If an
// address is provided without a netmask a /32 will be inferred for v4
// addresses and a /128 will be inferred for v6 addresses.
// RecordsForNetwork fetches mmdb records inside a given network. If an IP
// address is provided without a prefix length, it will be treated as a
// network containing a single address (i.e., /32 for IPv4 and /128 for IPv6).
func RecordsForNetwork(reader maxminddb.Reader, includeAliasedNetworks bool, maybeNetwork string) (any, error) {
lookupNetwork := maybeNetwork

Expand All @@ -60,33 +60,27 @@ func RecordsForNetwork(reader maxminddb.Reader, includeAliasedNetworks bool, may
}
}

//nolint:forbidigo // preexisting
_, network, err := net.ParseCIDR(lookupNetwork)
network, err := netip.ParsePrefix(lookupNetwork)
if err != nil {
return nil, fmt.Errorf("%v is not a valid IP address", maybeNetwork)
}

var n *maxminddb.Networks
var opts []maxminddb.NetworksOption
if includeAliasedNetworks {
n = reader.NetworksWithin(network)
} else {
n = reader.NetworksWithin(network, maxminddb.SkipAliasedNetworks)
opts = append(opts, maxminddb.IncludeAliasedNetworks)
}

var found []any

for n.Next() {
for res := range reader.NetworksWithin(network, opts...) {
var record any
address, err := n.Network(&record)

err := res.Decode(&record)
if err != nil {
return nil, fmt.Errorf("could not get next network: %w", err)
}

found = append(found, RecordForNetwork{address.String(), record})
}

if n.Err() != nil {
return nil, fmt.Errorf("traversing networks: %w", n.Err())
found = append(found, RecordForNetwork{res.Prefix(), record})
}

return found, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/mmdbinspect/mmdbinspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mmdbinspect
import (
"testing"

"github.com/oschwald/maxminddb-golang"
"github.com/oschwald/maxminddb-golang/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down

0 comments on commit 7d50614

Please sign in to comment.