Skip to content

Commit

Permalink
Add GitHub actions CI (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonbondon authored Jan 22, 2021
1 parent 17f35fc commit 2c0a15e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 6 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every weekday
interval: weekly

# Maintain dependencies for Go modules
- package-ecosystem: "gomod"
directory: "/"
schedule:
# Check for updates to Go modules every weekday
interval: weekly
57 changes: 57 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Tests
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/[email protected]
with:
go-version: '1.15'
id: go

- name: Check out code into the Go module directory
uses: actions/[email protected]
with:
submodules: true

- uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Get dependencies
run: |
go mod download
- name: Test
run: |
go test ./... -v
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/[email protected]
with:
go-version: '1.15'
id: go

- name: Check out code into the Go module directory
uses: actions/[email protected]

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.35
28 changes: 28 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
linters:
enable-all: true
disable:
- funlen
- godot
- exhaustivestruct
- gocognit

linters-settings:
wsl:
allow-cuddle-declarations: true
gci:
local-prefixes: github.com/gordonbondon/maxminddb-cidrs

issues:
exclude-rules:
- linters:
- wsl
- errcheck
- dogsled
- funlen
- scopelint
path: tests|_test\.go
- linters:
- forbidigo
- goerr113
- gomnd
path: main.go
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# maxminddb-cidrs

![Build status](https://github.com/gordonbondon/maxminddb-cidrs/workflows/Tests/badge.svg?branch=master)

Convenience command-line and library wrapper for [maxminddb-golang](https://github.com/oschwald/maxminddb-golang)
that helps retrieving network lists based on [ISO 3166-2](https://en.wikipedia.org/wiki/ISO_3166-2) country codes and subdivision codes.

Expand Down
1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func main() {
IPv4: ipv4Flag,
IPv6: ipv6Flag,
})

if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 5 additions & 2 deletions pkg/cidrs/cidrs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cidrs

import (
"fmt"
"net"

maxminddb "github.com/oschwald/maxminddb-golang"
Expand Down Expand Up @@ -42,7 +43,7 @@ func List(options *ListOptions) ([]string, error) {
if options.NetworksReader == nil {
db, err := maxminddb.Open(options.DBPath)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed opening db: %w", err)
}
defer db.Close()

Expand Down Expand Up @@ -74,7 +75,7 @@ func List(options *ListOptions) ([]string, error) {
for networks.Next() {
subnet, err := networks.Network(&record)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed parsing network: %w", err)
}

if options.IPv4 && subnet.IP.To4() == nil {
Expand All @@ -90,6 +91,7 @@ func List(options *ListOptions) ([]string, error) {
for _, i := range record.Subdivisions {
if _, ok := s[i.IsoCode]; ok {
results = append(results, subnet.String())

continue
}
}
Expand All @@ -98,6 +100,7 @@ func List(options *ListOptions) ([]string, error) {
}
}
}

if networks.Err() != nil {
return nil, networks.Err()
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/cidrs/cidrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ func (n *TestNetrowksReader) Next() bool {
if len(n.networks) > 0 {
n.next = n.networks[0]
n.networks = n.networks[1:]

return true
} else {
return false
}

return false
}

func (n *TestNetrowksReader) Network(result interface{}) (*net.IPNet, error) {
Expand Down Expand Up @@ -69,6 +70,8 @@ func (n *TestNetrowksReader) Err() error {
}

func TestCIDRs(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
reader *TestNetrowksReader
Expand Down Expand Up @@ -132,7 +135,7 @@ func TestCIDRs(t *testing.T) {
expected: []string{"192.0.2.1/32"},
},
{
name: "matches for multiple countires",
name: "matches for multiple countries",
reader: &TestNetrowksReader{
networks: []struct {
Country string
Expand Down Expand Up @@ -214,7 +217,11 @@ func TestCIDRs(t *testing.T) {
}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

tc.options.NetworksReader = tc.reader
actual, _ := cidrs.List(&tc.options)

Expand Down

0 comments on commit 2c0a15e

Please sign in to comment.