Skip to content

Commit 50b811f

Browse files
authored
Upgrade golangci-lint and fix new lint warnings with Go 1.18 (#70)
* Upgrade golangci-lint * Fix new lint warnings with Go 1.18 * Remove the golangci template. This was only necessary when upgrading the Go version, which is now automated. * Fix go mod download conflicting with go mod tidy. Run go mod tidy after go mod download to restore go.sum to the state we should commit. * Prepare changelog for 0.2.10.
1 parent 7f42b86 commit 50b811f

File tree

10 files changed

+43
-237
lines changed

10 files changed

+43
-237
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ jobs:
2626
GOOS: ${{ matrix.GOOS }}
2727
run: echo Go GOOS=$GOOS
2828

29-
- uses: actions/checkout@v2
29+
- uses: actions/checkout@v3
3030

3131
# Uses Go version from the repository.
3232
- name: Read .go-version file
3333
id: goversion
3434
run: echo "::set-output name=version::$(cat .go-version)"
3535

36-
- uses: actions/setup-go@v2
36+
- uses: actions/setup-go@v3
3737
with:
3838
go-version: "${{ steps.goversion.outputs.version }}"
3939

4040
- name: golangci-lint
4141
env:
4242
GOOS: ${{ matrix.GOOS }}
43-
uses: golangci/golangci-lint-action@v2
43+
uses: golangci/golangci-lint-action@v3
4444
with:
4545
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
46-
version: v1.45.2
46+
version: v1.47.2
4747

4848
# Give the job more time to execute.
4949
# Regarding `--whole-files`, the linter is supposed to support linting of changed a patch only but,

.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# DO NOT EDIT!
2-
# This file is a rendered template, the source can be found in "./dev-tools/templates/.golangci.yml"
3-
#
41
# options for analysis running
52
run:
63
# timeout for analysis, e.g. 30s, 5m, default is 1m

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66

77
### Added
88

9-
- Add option to `file.NewFileRotator` to allow setting file extension. #68
10-
119
### Changed
1210

1311
### Deprecated
@@ -16,6 +14,18 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1614

1715
### Fixed
1816

17+
## [0.2.10]
18+
19+
### Added
20+
21+
- Add option to `file.NewFileRotator` to allow setting file extension. #68
22+
23+
### Changed
24+
25+
- dev-tools: remove support for golanci-lint version templating, fix for notice target modifying go.sum. #70
26+
27+
### Fixed
28+
1929
- Fix filename logging in `file.NewFileRotator`. #68
2030

2131
## [0.2.9]

dev-tools/mage/go-version.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

dev-tools/mage/linter.go

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,72 +21,26 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24-
"io/ioutil"
2524
"log"
2625
"net/http"
2726
"os"
2827
"path/filepath"
29-
"strings"
30-
"text/template"
3128

3229
"github.com/magefile/mage/mg"
3330
"github.com/magefile/mage/sh"
3431
)
3532

3633
const (
37-
linterInstallURL = "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh"
38-
linterInstallFilename = "./build/intall-golang-ci.sh"
39-
linterBinaryFilename = "./build/golangci-lint"
40-
linterVersion = "v1.45.2"
41-
linterConfigFilename = "./.golangci.yml"
42-
linterConfigTemplateFilename = "./dev-tools/templates/.golangci.yml"
34+
linterInstallURL = "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh"
35+
linterInstallFilename = "./build/install-golang-ci.sh"
36+
linterBinaryFilename = "./build/golangci-lint"
37+
linterVersion = "v1.47.2"
38+
linterConfigFilename = "./.golangci.yml"
4339
)
4440

4541
// Linter contains targets related to linting the Go code
4642
type Linter mg.Namespace
4743

48-
// UpdateGoVersion updates the linter configuration with the new version of Go.
49-
func (Linter) UpdateGoVersion() error {
50-
goVersionBytes, err := ioutil.ReadFile(goVersionFilename)
51-
if err != nil {
52-
return fmt.Errorf("failed to read the %q file: %w", goVersionFilename, err)
53-
}
54-
goVersion := strings.TrimSpace(string(goVersionBytes))
55-
log.Printf("The Go version is %s\n", goVersion)
56-
57-
templateContext := struct{ GoVersion string }{GoVersion: goVersion}
58-
template, err := template.ParseFiles(linterConfigTemplateFilename)
59-
if err != nil {
60-
return fmt.Errorf("failed to read the template file %q: %w", linterConfigTemplateFilename, err)
61-
}
62-
63-
configFile, err := os.OpenFile(linterConfigFilename, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0700)
64-
if err != nil {
65-
return fmt.Errorf("failed to create/replace the linter config %q: %w", linterConfigFilename, err)
66-
}
67-
defer configFile.Close()
68-
69-
warning := fmt.Sprintf("# DO NOT EDIT!\n# This file is a rendered template, the source can be found in %q\n#\n", linterConfigTemplateFilename)
70-
_, err = configFile.WriteString(warning)
71-
if err != nil {
72-
return fmt.Errorf("failed to write into the linter config %q: %w", linterConfigFilename, err)
73-
}
74-
75-
err = template.Execute(configFile, templateContext)
76-
if err != nil {
77-
return fmt.Errorf("failed to execute the template %q: %w", linterConfigTemplateFilename, err)
78-
}
79-
80-
err = assertUnchanged(linterConfigFilename)
81-
if err != nil {
82-
log.Printf("Successfully updated the linter configuration %q to Go version %s, please commit the changes now", linterConfigFilename, goVersion)
83-
} else {
84-
log.Printf("The linter configuration %q is up to date, no changes made", linterConfigFilename)
85-
}
86-
87-
return nil
88-
}
89-
9044
// CheckConfig makes sure that the `.golangci.yml` does not have uncommitted changes
9145
func (Linter) CheckConfig() error {
9246
err := assertUnchanged(linterConfigFilename)
@@ -100,18 +54,28 @@ func (Linter) CheckConfig() error {
10054
// using the official installation script downloaded from GitHub.
10155
// If the linter binary already exists does nothing.
10256
func (Linter) Install() error {
57+
return install(false)
58+
}
59+
60+
// ForceInstall force installs the linter regardless of whether it exists or not.
61+
// Useful primarily when the linter version should be updated.
62+
func (Linter) ForceInstall() error {
63+
return install(true)
64+
}
65+
66+
func install(force bool) error {
10367
dirPath := filepath.Dir(linterBinaryFilename)
10468
err := os.MkdirAll(dirPath, 0700)
10569
if err != nil {
10670
return fmt.Errorf("failed to create path %q: %w", dirPath, err)
10771
}
10872

10973
_, err = os.Stat(linterBinaryFilename)
110-
if err == nil {
74+
if !force && err == nil {
11175
log.Println("The linter has been already installed, skipping...")
11276
return nil
11377
}
114-
if !errors.Is(err, os.ErrNotExist) {
78+
if err != nil && !errors.Is(err, os.ErrNotExist) {
11579
return fmt.Errorf("failed check if file %q exists: %w", linterBinaryFilename, err)
11680
}
11781

dev-tools/mage/notice.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func GenerateNotice(overrides, rules, noticeTemplate string) error {
3434
return fmt.Errorf("error while downloading dependencies: %w", err)
3535
}
3636

37+
// Ensure the go.mod file is left unchanged after go mod download all runs.
38+
// go mod download will modify go.sum in a way that conflicts with go mod tidy.
39+
// https://github.com/golang/go/issues/43994#issuecomment-770053099
40+
defer gotool.Mod.Tidy() //nolint:errcheck // No value in handling this error.
41+
3742
out, _ := gotool.ListDepsForNotice()
3843
depsFile, _ := os.CreateTemp("", "depsout")
3944
defer os.Remove(depsFile.Name())

dev-tools/templates/.golangci.yml

Lines changed: 0 additions & 140 deletions
This file was deleted.

transport/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (c *Client) handleError(err error) error {
206206
c.log.Debugf("handle error: %+v", err)
207207

208208
var nerr net.Error
209-
if errors.As(err, &nerr) && !(nerr.Temporary() || nerr.Timeout()) {
209+
if errors.As(err, &nerr) && !nerr.Timeout() {
210210
_ = c.Close()
211211
}
212212
}

transport/httpcommon/proxy_headers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (p *ProxyHeaders) UnmarshalYAML(unmarshal func(interface{}) error) error {
7575
// URI returns conventional url.URL structure.
7676
func (p ProxyHeaders) Headers() http.Header {
7777
var httpHeaders http.Header
78-
if p != nil && len(p) > 0 {
78+
if len(p) > 0 {
7979
httpHeaders = http.Header{}
8080
for k, v := range p {
8181
httpHeaders.Add(k, v)

0 commit comments

Comments
 (0)