Skip to content

Commit c9848d5

Browse files
committed
golangci lint
1 parent 7769d03 commit c9848d5

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

.golangci.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ linters:
1515
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
1616
disable-all: true
1717
enable:
18-
- bodyclose
1918
- deadcode
2019
- depguard
2120
- dogsled
@@ -33,21 +32,13 @@ linters:
3332
- gomnd
3433
- goprintffuncname
3534
- gosec
36-
- gosimple
3735
- govet
3836
- ineffassign
3937
- lll
4038
- misspell
4139
- nakedret
42-
- noctx
4340
- nolintlint
44-
- rowserrcheck
45-
- staticcheck
46-
- structcheck
47-
- stylecheck
4841
- typecheck
4942
- unconvert
50-
- unparam
51-
- unused
5243
- varcheck
5344
- whitespace

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ testall:
88
#go get github.com/smartystreets/goconvey
99
#goconvey
1010

11+
lint:
12+
go fmt ./...
13+
golangci-lint run
14+
1115
bash:
1216
docker compose exec testdb bash
1317

asn2ip/asn2ip.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
"github.com/pkg/errors"
1010
)
1111

12+
const (
13+
IPVersion4 = 4
14+
IPVersion6 = 6
15+
)
16+
1217
// https://github.com/g0dsCookie/asn2ip
1318
// https://www.radb.net/query/help
1419
type Fetcher interface {
@@ -58,11 +63,13 @@ func readLine(conn net.Conn) (string, error) {
5863
// more command refer here, https://www.radb.net/query/help
5964
func fetch(conn net.Conn, as string, version int) ([]*net.IPNet, error) {
6065
cmd := ""
61-
if version == 4 {
66+
67+
switch version {
68+
case IPVersion4:
6269
cmd = fmt.Sprintf("!gAS%s\n", as)
63-
} else if version == 6 {
70+
case IPVersion6:
6471
cmd = fmt.Sprintf("!6AS%s\n", as)
65-
} else {
72+
default:
6673
return nil, errors.Errorf("unknown ip protocol version %d", version)
6774
}
6875

@@ -85,7 +92,7 @@ func fetch(conn net.Conn, as string, version int) ([]*net.IPNet, error) {
8592
}
8693

8794
if state == "start" {
88-
if len(line) <= 0 {
95+
if len(line) == 0 {
8996
return nil, errors.Errorf("empty response for as %s", as)
9097
}
9198
if line[0] != 'A' {
@@ -118,7 +125,11 @@ func (f *fetcher) Fetch(ipv4, ipv6 bool, asn ...string) (map[string]map[string][
118125
}
119126
defer func() {
120127
// gracefully close socket
121-
conn.Write([]byte("exit\n"))
128+
_, err := conn.Write([]byte("exit\n"))
129+
if err != nil {
130+
panic(err) // TODO
131+
}
132+
122133
conn.Close()
123134
}()
124135

@@ -130,14 +141,14 @@ func (f *fetcher) Fetch(ipv4, ipv6 bool, asn ...string) (map[string]map[string][
130141
for _, v := range asn {
131142
result[v] = map[string][]*net.IPNet{"ipv4": {}, "ipv6": {}}
132143
if ipv4 {
133-
net, err := fetch(conn, v, 4)
144+
net, err := fetch(conn, v, IPVersion4)
134145
if err != nil {
135146
return nil, err
136147
}
137148
result[v]["ipv4"] = net
138149
}
139150
if ipv6 {
140-
net, err := fetch(conn, v, 6)
151+
net, err := fetch(conn, v, IPVersion6)
141152
if err != nil {
142153
return nil, err
143154
}

asn2ip/asn2ip_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ func TestRun(t *testing.T) {
1212

1313
fetcher := asn2ip.NewFetcher("whois.radb.net", 43)
1414

15-
asnum := "AS15169"
16-
asnum = "AS4134"
17-
asnum = strings.Replace(asnum, "AS", "", -1)
15+
asnum := "AS4134"
16+
asnum = strings.ReplaceAll(asnum, "AS", "")
1817
ips, err := fetcher.Fetch(true, false, asnum)
1918
if err != nil {
2019
t.Error(err)

fileutil/file.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ func EnsureFileExists(path string) {
4747
}
4848
}
4949

50-
5150
func ReadFile(path string) ([]byte, error) {
52-
content, err := ioutil.ReadFile(path)
53-
if err != nil {
51+
content, err := ioutil.ReadFile(path)
52+
if err != nil {
5453
log.Fatal("read file fail, err: ", err)
55-
return nil, err
56-
}
54+
return nil, err
55+
}
5756

58-
return content, nil
57+
return content, nil
5958
}
6059

6160
func WriteFile(path string, content string) error {

0 commit comments

Comments
 (0)