Skip to content

Commit 2847e0b

Browse files
committed
Update linter
1 parent 11de229 commit 2847e0b

File tree

13 files changed

+85
-118
lines changed

13 files changed

+85
-118
lines changed

.github/workflows/go.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: golangci-lint
2727
uses: golangci/[email protected]
2828
with:
29-
version: v1.49.0
29+
version: v1.51.1
3030

3131
mod-tidy:
3232
name: Go Mod Tidy

.golangci.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ linters:
1919
- gocritic
2020
- gofmt
2121
- goimports
22-
- golint
2322
- gosec
2423
- govet
2524
- lll
26-
- maligned
2725
- megacheck
2826
- misspell
2927
- nakedret
3028
- prealloc
29+
- revive
3130
- stylecheck
3231
- unconvert
3332
- unparam

bin/style

+59-62
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,59 @@
1-
#!/usr/bin/env ruby
2-
3-
require 'fileutils'
4-
require 'open3'
5-
require 'open-uri'
6-
7-
class Style
8-
class << self
9-
INSTALLER_URL = 'https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh'
10-
EXECUTABLE = 'golangci-lint'
11-
VERSION = '1.49.0'
12-
VERSION_TAG = "v#{VERSION}"
13-
14-
def call
15-
install unless up_to_date?
16-
run(*ARGV)
17-
end
18-
19-
private
20-
21-
def install_path
22-
gopath = ENV.fetch('GOPATH', ENV.fetch('HOME')).split(':').first
23-
File.join(gopath, 'bin')
24-
end
25-
26-
def bin_path
27-
File.join(install_path, EXECUTABLE)
28-
end
29-
30-
def exist?
31-
File.executable?(bin_path)
32-
end
33-
34-
def up_to_date?
35-
return false unless exist?
36-
out, stat = Open3.capture2(bin_path, '--version')
37-
raise("Unable to check version of #{bin_path}") unless stat.success?
38-
out.match(/\b#{VERSION}\b/)
39-
end
40-
41-
def install
42-
puts "Installing #{EXECUTABLE}..."
43-
44-
open(INSTALLER_URL) do |installer|
45-
Open3.popen2('sh', '-s', '--', '-b', install_path, VERSION_TAG) do |stdin, out, stat|
46-
IO.copy_stream(installer, stdin)
47-
stdin.close
48-
print out.read
49-
50-
raise("failed to install #{EXECUTABLE} #{VERSION_TAG}") unless stat.value.success?
51-
end
52-
end
53-
end
54-
55-
def run(*argv)
56-
config = ENV.fetch('GOLANGCI_CONFIG', '.golangci.yml')
57-
exec(bin_path, 'run', '--config', config, *argv)
58-
end
59-
end
60-
end
61-
62-
__FILE__ == $PROGRAM_NAME and Style.call
1+
#!/bin/sh
2+
3+
# This script is intentionally written to be POSIX compliant to be as portable as possible
4+
5+
set -e
6+
7+
VERSION="1.51.1"
8+
VERSION_TAG="v${VERSION}"
9+
10+
INSTALLER_URL="github.com/golangci/golangci-lint/cmd/golangci-lint"
11+
EXECUTABLE="$(basename "${INSTALLER_URL}")"
12+
13+
INSTALL_PATH="${HOME}/.local/share/${EXECUTABLE}/${VERSION}"
14+
EXECUTABLE_PATH="${INSTALL_PATH}/${EXECUTABLE}" # e.g. $HOME/.local/share/golangci/1.32.0/golangci-lint
15+
16+
GOPATH="${GOPATH:-${HOME}}"
17+
GOPATH_PRIMARY="${GOPATH%%:*}" # Delete :* from the end, yielding the first path
18+
BIN_INSTALL_PATH="${GOPATH_PRIMARY}/bin"
19+
BIN_EXECUTABLE_PATH="${BIN_INSTALL_PATH}/${EXECUTABLE}" # e.g. $HOME/bin/golangci-lint
20+
21+
installed() {
22+
[ -x "${EXECUTABLE_PATH}" ]
23+
}
24+
25+
install() {
26+
echo "Installing ${EXECUTABLE} version ${VERSION}" >&2
27+
28+
mkdir -p "${INSTALL_PATH}"
29+
GOBIN="${INSTALL_PATH}" go install "${INSTALLER_URL}@${VERSION_TAG}"
30+
}
31+
32+
linked() {
33+
[ -L "${BIN_EXECUTABLE_PATH}" ] && [ "$(readlink "${BIN_EXECUTABLE_PATH}")" = "${EXECUTABLE_PATH}" ]
34+
}
35+
36+
link() {
37+
mkdir -p "${BIN_INSTALL_PATH}"
38+
rm -fv "${BIN_EXECUTABLE_PATH}"
39+
ln -sfv "${EXECUTABLE_PATH}" "${BIN_EXECUTABLE_PATH}"
40+
}
41+
42+
case "$1" in
43+
"--installed")
44+
installed
45+
;;
46+
"--install")
47+
installed || install
48+
;;
49+
"--linked")
50+
installed && linked
51+
;;
52+
"--link")
53+
(installed || install) && (linked || link)
54+
;;
55+
*)
56+
installed || install
57+
exec "${EXECUTABLE_PATH}" "$@"
58+
;;
59+
esac

dev.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ commands:
1111
godoc -http=:6060
1212
test: go test -race ./...
1313
style:
14-
run: bin/style
15-
desc: Static verification using gometalinter or autofix issues when possible.
14+
run: bin/style run
15+
desc: Static verification using golangci or autofix issues when possible.
1616
syntax:
1717
optional: --fix

logger/loggable.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
)
88

99
// This create a private key-space in the Context, meaning that only this package can get or set "contextKey" types
10-
type contextKey int
10+
type contextKey struct{}
1111

12-
const (
13-
logFieldsKey contextKey = iota
12+
var (
13+
logFieldsKey = contextKey{}
1414
)
1515

1616
type Loggable interface {

logger/loggable_test.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func ExampleWatchingLoggable() {
6464
func TestEmptyContext(t *testing.T) {
6565
ctx := context.Background()
6666
// Using a basic type on purpose, disable linter
67-
ctx = context.WithValue(ctx, "a", "b") //nolint:golint,staticcheck
67+
ctx = context.WithValue(ctx, "a", "b") //nolint:revive,staticcheck
6868
// Not showing up in logs
6969
checkData(ctx, t, logrus.Fields{"component": "testing"})
7070
}
@@ -116,19 +116,6 @@ func TestWithField(t *testing.T) {
116116
})
117117
}
118118

119-
func TestLoggableKeyClash(t *testing.T) {
120-
ctx := context.Background()
121-
ctx = WithField(ctx, "a", "b")
122-
123-
// logFieldsKey is an int declared as a contextKey, so trying to set an int shouldn't override the contextKey
124-
// Using a basic type on purpose, disable linter
125-
ctx = context.WithValue(ctx, int(logFieldsKey), "foo") //nolint:golint,staticcheck
126-
127-
checkData(ctx, t, logrus.Fields{
128-
"a": "b",
129-
})
130-
}
131-
132119
func TestChildContext(t *testing.T) {
133120
ctx := context.Background()
134121
ctx = WithField(ctx, "a", "b")

logrusbugsnag/bugsnag_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package logrusbugsnag
33
import (
44
"bytes"
55
"errors"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"sync"
99
"testing"
@@ -25,15 +25,15 @@ type nilRoundTripper struct{}
2525

2626
func (rt *nilRoundTripper) RoundTrip(_ *http.Request) (*http.Response, error) {
2727
return &http.Response{
28-
Body: ioutil.NopCloser(bytes.NewReader(nil)),
28+
Body: io.NopCloser(bytes.NewReader(nil)),
2929
StatusCode: http.StatusOK,
3030
}, nil
3131
}
3232

3333
func setup(record bool) {
3434
testOnce.Do(func() {
3535
l := logrus.New()
36-
l.Out = ioutil.Discard
36+
l.Out = io.Discard
3737

3838
bugsnag.Configure(bugsnag.Configuration{
3939
APIKey: testAPIKey,
@@ -57,7 +57,7 @@ func BenchmarkHook_Fire(b *testing.B) {
5757
setup(false)
5858

5959
l := logrus.New()
60-
l.Out = ioutil.Discard
60+
l.Out = io.Discard
6161

6262
hook, err := NewBugsnagHook(nil)
6363
assert.NoError(b, err)
@@ -89,7 +89,7 @@ func TestNewBugsnagHook(t *testing.T) {
8989
setup(true)
9090

9191
l := logrus.New()
92-
l.Out = ioutil.Discard
92+
l.Out = io.Discard
9393

9494
hook, err := NewBugsnagHook(nil)
9595
assert.NoError(t, err)

profiler/ui.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package profiler
33
import (
44
"context"
55
"errors"
6-
"io/ioutil"
76
"net/http"
87
"net/http/httptest"
98
httppprof "net/http/pprof"
@@ -19,7 +18,7 @@ func pprofUIHandler(w http.ResponseWriter, r *http.Request) {
1918
ctx := r.Context()
2019
vars := mux.Vars(r)
2120

22-
f, err := ioutil.TempFile("", "pprof.*.pb")
21+
f, err := os.CreateTemp("", "pprof.*.pb")
2322
if err != nil {
2423
log(ctx, err).Error("error creating temporary file")
2524
http.Error(w, err.Error(), http.StatusInternalServerError)

shell/shell_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"os"
109
"path/filepath"
1110
"strings"
@@ -27,7 +26,7 @@ func ExampleSupervisor_Wait() {
2726
stdin.Write([]byte("foo"))
2827
stdin.Close()
2928

30-
output, _ := ioutil.ReadAll(stdout)
29+
output, _ := io.ReadAll(stdout)
3130

3231
// Must call Wait _after_ interacting with pipes
3332
cmd.Wait()
@@ -144,7 +143,7 @@ func TestCommandRunPipe(t *testing.T) {
144143
assert.NotEqual(t, 0, c.Process.Pid) // But the process exists
145144

146145
// Calling ReadAll will wait for the pipe to close, so all the output is there.
147-
output, err := ioutil.ReadAll(pipe)
146+
output, err := io.ReadAll(pipe)
148147
assert.NoError(t, err)
149148
assert.Equal(t, []byte("foo"), output)
150149

@@ -174,7 +173,7 @@ func TestCommandRunWaitPipeFails(t *testing.T) {
174173
assert.True(t, c.ProcessState.Exited())
175174

176175
// Calling ReadAll will wait for the pipe to close, so all the output is there.
177-
_, err = ioutil.ReadAll(pipe)
176+
_, err = io.ReadAll(pipe)
178177
assert.Error(t, err, "read |0: file already closed")
179178
}
180179

srvutil/metrics_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"bytes"
66
"context"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net"
1010
"net/http"
1111
"net/http/httptest"
@@ -70,7 +70,7 @@ func TestRequestMetricsMiddleware(t *testing.T) {
7070
res, err := http.DefaultClient.Do(req)
7171
assert.NoError(t, err)
7272
assert.Equal(t, http.StatusOK, res.StatusCode)
73-
body, err := ioutil.ReadAll(res.Body)
73+
body, err := io.ReadAll(res.Body)
7474
assert.NoError(t, err)
7575
assert.Equal(t, "hello world", string(body))
7676

srvutil/server_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"os"
109
"strings"
@@ -73,7 +72,7 @@ func TestNewServer(t *testing.T) {
7372
res, err := http.Get(u)
7473
assert.NoError(t, err)
7574
assert.Equal(t, http.StatusOK, res.StatusCode)
76-
body, err := ioutil.ReadAll(res.Body)
75+
body, err := io.ReadAll(res.Body)
7776
assert.NoError(t, err)
7877
assert.Equal(t, "great success", string(body))
7978

@@ -136,7 +135,7 @@ func TestNewServerFromFactory(t *testing.T) {
136135
res, err := http.Get(u)
137136
assert.NoError(t, err)
138137
assert.Equal(t, http.StatusOK, res.StatusCode)
139-
body, err := ioutil.ReadAll(res.Body)
138+
body, err := io.ReadAll(res.Body)
140139
assert.NoError(t, err)
141140
assert.Equal(t, "great success", string(body))
142141

@@ -214,7 +213,7 @@ func TestStoppableKeepaliveListener_Accept(t *testing.T) {
214213
res, err := http.Get(u) // This will block on tb.Dying()
215214
assert.NoError(t, err)
216215
assert.Equal(t, http.StatusOK, res.StatusCode)
217-
body, err := ioutil.ReadAll(res.Body)
216+
body, err := io.ReadAll(res.Body)
218217
assert.NoError(t, err)
219218
assert.Equal(t, "great success", string(body))
220219
close(done)

statsd/taggable.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
)
1010

1111
// This create a private key-space in the Context, meaning that only this package can get or set "contextKey" types
12-
type contextKey int
12+
type contextKey struct{}
1313

14-
const (
15-
tagsKey contextKey = iota
14+
var (
15+
tagsKey = contextKey{}
1616
)
1717

1818
type Tags map[string]interface{}

statsd/taggable_test.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func ExampleSelectKeys() {
9191
func TestEmptyContext(t *testing.T) {
9292
ctx := context.Background()
9393
// Using a basic type on purpose, disable linter
94-
ctx = context.WithValue(ctx, "a", "b") //nolint:golint,staticcheck
94+
ctx = context.WithValue(ctx, "a", "b") //nolint:revive,staticcheck
9595
// Not showing up in tags
9696
assert.Empty(t, getStatsTags(ctx))
9797
}
@@ -109,19 +109,6 @@ func TestWithTags(t *testing.T) {
109109
}, getStatsTags(ctx))
110110
}
111111

112-
func TestWithTags_keyClash(t *testing.T) {
113-
ctx := context.Background()
114-
ctx = WithTags(ctx, Tags{"a": "b"})
115-
116-
// tagsKey is an int declared as a contextKey, so trying to set an int shouldn't override the contextKey
117-
// Using a basic type on purpose, disable linter
118-
ctx = context.WithValue(ctx, int(tagsKey), "foo") //nolint:golint,staticcheck
119-
120-
assert.Equal(t, []string{
121-
"a:b",
122-
}, getStatsTags(ctx))
123-
}
124-
125112
func TestChildContext(t *testing.T) {
126113
ctx := context.Background()
127114
ctx = WithTags(ctx, Tags{"a": "b"})

0 commit comments

Comments
 (0)