Skip to content

Commit 01a43f1

Browse files
committed
golangci-lint: enable nosprintfhostport linter
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 59932f5 commit 01a43f1

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ linters:
4747
- misspell # Detects commonly misspelled English words in comments.
4848
- nakedret # Detects uses of naked returns.
4949
- nilnesserr # Detects returning nil errors. It combines the features of nilness and nilerr,
50+
- nosprintfhostport # Detects misuse of Sprintf to construct a host with port in a URL.
5051
- nolintlint # Detects ill-formed or insufficient nolint directives.
5152
- perfsprint # Detects fmt.Sprintf uses that can be replaced with a faster alternative.
5253
- prealloc # Detects slice declarations that could potentially be pre-allocated.

opts/hosts_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package opts
22

33
import (
4-
"fmt"
4+
"net"
55
"testing"
66

77
"gotest.tools/v3/assert"
@@ -25,13 +25,13 @@ func TestParseHost(t *testing.T) {
2525
"fd://something": "fd://something",
2626
"tcp://host:": "tcp://host:" + defaultHTTPPort,
2727
"tcp://": defaultTCPHost,
28-
"tcp://:2375": fmt.Sprintf("tcp://%s:%s", defaultHTTPHost, defaultHTTPPort),
29-
"tcp://:2376": fmt.Sprintf("tcp://%s:%s", defaultHTTPHost, defaultTLSHTTPPort),
28+
"tcp://:2375": "tcp://" + net.JoinHostPort(defaultHTTPHost, defaultHTTPPort),
29+
"tcp://:2376": "tcp://" + net.JoinHostPort(defaultHTTPHost, defaultTLSHTTPPort),
3030
"tcp://0.0.0.0:8080": "tcp://0.0.0.0:8080",
3131
"tcp://192.168.0.0:12000": "tcp://192.168.0.0:12000",
3232
"tcp://192.168:8080": "tcp://192.168:8080",
3333
"tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P
34-
" tcp://:7777/path ": fmt.Sprintf("tcp://%s:7777/path", defaultHTTPHost),
34+
" tcp://:7777/path ": "tcp://" + net.JoinHostPort(defaultHTTPHost, "7777") + "/path",
3535
"tcp://docker.com:2375": "tcp://docker.com:2375",
3636
"unix://": "unix://" + defaultUnixSocket,
3737
"unix://path/to/socket": "unix://path/to/socket",
@@ -70,11 +70,11 @@ func TestParseDockerDaemonHost(t *testing.T) {
7070
"[::1]:5555/path": "tcp://[::1]:5555/path",
7171
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2375",
7272
"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
73-
":6666": fmt.Sprintf("tcp://%s:6666", defaultHTTPHost),
74-
":6666/path": fmt.Sprintf("tcp://%s:6666/path", defaultHTTPHost),
73+
":6666": "tcp://" + net.JoinHostPort(defaultHTTPHost, "6666"),
74+
":6666/path": "tcp://" + net.JoinHostPort(defaultHTTPHost, "6666") + "/path",
7575
"tcp://": defaultTCPHost,
76-
"tcp://:7777": fmt.Sprintf("tcp://%s:7777", defaultHTTPHost),
77-
"tcp://:7777/path": fmt.Sprintf("tcp://%s:7777/path", defaultHTTPHost),
76+
"tcp://:7777": "tcp://" + net.JoinHostPort(defaultHTTPHost, "7777"),
77+
"tcp://:7777/path": "tcp://" + net.JoinHostPort(defaultHTTPHost, "7777") + "/path",
7878
"unix:///run/docker.sock": "unix:///run/docker.sock",
7979
"unix://": "unix://" + defaultUnixSocket,
8080
"fd://": "fd://",
@@ -96,16 +96,16 @@ func TestParseDockerDaemonHost(t *testing.T) {
9696
}
9797

9898
func TestParseTCP(t *testing.T) {
99-
defaultHTTPHost := "tcp://127.0.0.1:2376"
99+
const defaultHost = "tcp://127.0.0.1:2376"
100100
invalids := map[string]string{
101101
"tcp:a.b.c.d": "",
102102
"tcp:a.b.c.d/path": "",
103103
"udp://127.0.0.1": "invalid proto, expected tcp: udp://127.0.0.1",
104104
"udp://127.0.0.1:2375": "invalid proto, expected tcp: udp://127.0.0.1:2375",
105105
}
106106
valids := map[string]string{
107-
"": defaultHTTPHost,
108-
"tcp://": defaultHTTPHost,
107+
"": defaultHost,
108+
"tcp://": defaultHost,
109109
"0.0.0.1:": "tcp://0.0.0.1:2376",
110110
"0.0.0.1:5555": "tcp://0.0.0.1:5555",
111111
"0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path",
@@ -124,12 +124,12 @@ func TestParseTCP(t *testing.T) {
124124
"localhost:5555/path": "tcp://localhost:5555/path",
125125
}
126126
for invalidAddr, expectedError := range invalids {
127-
if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || expectedError != "" && err.Error() != expectedError {
127+
if addr, err := ParseTCPAddr(invalidAddr, defaultHost); err == nil || expectedError != "" && err.Error() != expectedError {
128128
t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
129129
}
130130
}
131131
for validAddr, expectedAddr := range valids {
132-
if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr {
132+
if addr, err := ParseTCPAddr(validAddr, defaultHost); err != nil || addr != expectedAddr {
133133
t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr)
134134
}
135135
}

0 commit comments

Comments
 (0)