Skip to content

Commit 25a96d4

Browse files
style: add .golangci-lint.yaml
1 parent 9beb3f2 commit 25a96d4

File tree

7 files changed

+78
-35
lines changed

7 files changed

+78
-35
lines changed

.golangci.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
linters:
2+
enable:
3+
- errorlint
4+
- forbidigo
5+
- gochecknoinits
6+
- gocritic
7+
- goconst
8+
- gocyclo
9+
- gofumpt
10+
- goimports
11+
- misspell
12+
- revive
13+
- unconvert
14+
- unparam
15+
- wastedassign
16+
17+
linters-settings:
18+
gocyclo:
19+
min-complexity: 12
20+
gofumpt:
21+
extra-rules: true
22+
govet:
23+
enable-all: true
24+
disable:
25+
- fieldalignment
26+
27+
run:
28+
skip-dirs:
29+
- goreleaser

pkg/config/config.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"os"
56
"strings"
67

@@ -142,18 +143,20 @@ func must(err error) {
142143
}
143144
}
144145

146+
var trueString = "true"
147+
145148
func detectCI() string {
146-
if os.Getenv("GITHUB_ACTIONS") == "true" {
149+
if os.Getenv("GITHUB_ACTIONS") == trueString {
147150
return "github"
148151
}
149-
if os.Getenv("GITLAB_CI") == "true" {
152+
if os.Getenv("GITLAB_CI") == trueString {
150153
return "gitlab"
151154
}
152155
return "default"
153156
}
154157

155158
func defaultProvider() string {
156-
if os.Getenv("GITLAB_CI") == "true" {
159+
if os.Getenv("GITLAB_CI") == trueString {
157160
return "gitlab"
158161
}
159162
return "github"
@@ -218,7 +221,8 @@ func InitConfig(cmd *cobra.Command) error {
218221
viper.SetConfigType("json")
219222

220223
if err := viper.ReadInConfig(); err != nil {
221-
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
224+
var viperConfigNotFound viper.ConfigFileNotFoundError
225+
if !errors.As(err, &viperConfigNotFound) {
222226
return err
223227
}
224228
}

pkg/plugin/discovery/discovery.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ func (d *Discovery) FindPlugin(t, name string) (*plugin.PluginInfo, error) {
6969
if err != nil {
7070
return nil, err
7171
}
72-
if err := setAndEnsurePluginPath(pInfo); err != nil {
72+
73+
err = setAndEnsurePluginPath(pInfo)
74+
if err != nil {
7375
return nil, err
7476
}
7577

pkg/plugin/discovery/download.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"compress/gzip"
66
"crypto/sha256"
77
"encoding/hex"
8+
"errors"
89
"fmt"
910
"io"
1011
"os"
@@ -66,7 +67,7 @@ func extractFileFromTarGz(name, inputFile, outputFile string) error {
6667
tarReader := tar.NewReader(decompressedFile)
6768
for {
6869
header, err := tarReader.Next()
69-
if err == io.EOF {
70+
if errors.Is(err, io.EOF) {
7071
return fmt.Errorf("could not extract file")
7172
}
7273
if err != nil {
@@ -97,9 +98,9 @@ func downloadPlugin(pluginInfo *plugin.PluginInfo, downloadInfo *resolver.Plugin
9798
return "", err
9899
}
99100
if downloadInfo.Checksum != "" {
100-
sum, err := hex.DecodeString(downloadInfo.Checksum)
101-
if err != nil {
102-
return "", err
101+
sum, decErr := hex.DecodeString(downloadInfo.Checksum)
102+
if decErr != nil {
103+
return "", decErr
103104
}
104105
req.SetChecksum(sha256.New(), sum, true)
105106
}
@@ -108,7 +109,8 @@ func downloadPlugin(pluginInfo *plugin.PluginInfo, downloadInfo *resolver.Plugin
108109
if showProgress {
109110
showDownloadProgressBar(pluginInfo.ShortNormalizedName, res)
110111
}
111-
if err := res.Err(); err != nil {
112+
err = res.Err()
113+
if err != nil {
112114
return "", err
113115
}
114116

pkg/plugin/discovery/local.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package discovery
22

33
import (
44
"errors"
5-
"io/ioutil"
5+
"fmt"
66
"os"
77
"path"
88
"runtime"
99
"sort"
1010

11-
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
12-
1311
"github.com/Masterminds/semver/v3"
12+
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
1413
)
1514

1615
const PluginDir = ".semrel"
@@ -20,7 +19,8 @@ var osArchDir = runtime.GOOS + "_" + runtime.GOARCH
2019
func setAndEnsurePluginPath(pluginInfo *plugin.PluginInfo) error {
2120
pluginPath := path.Join(PluginDir, osArchDir, pluginInfo.NormalizedName)
2221
if _, err := os.Stat(pluginPath); os.IsNotExist(err) {
23-
if err := os.MkdirAll(pluginPath, 0o755); err != nil {
22+
err = os.MkdirAll(pluginPath, 0o755)
23+
if err != nil {
2424
return err
2525
}
2626
} else if err != nil {
@@ -33,7 +33,7 @@ func setAndEnsurePluginPath(pluginInfo *plugin.PluginInfo) error {
3333
var ErrPluginNotFound = errors.New("no plugin was found")
3434

3535
func getMatchingVersionDir(pluginInfo *plugin.PluginInfo) (string, error) {
36-
vDirs, err := ioutil.ReadDir(pluginInfo.PluginPath)
36+
vDirs, err := os.ReadDir(pluginInfo.PluginPath)
3737
if err != nil {
3838
return "", err
3939
}
@@ -75,7 +75,7 @@ func findPluginLocally(pluginInfo *plugin.PluginInfo) (string, error) {
7575
return "", ErrPluginNotFound
7676
}
7777

78-
files, err := ioutil.ReadDir(vPth)
78+
files, err := os.ReadDir(vPth)
7979
if err != nil {
8080
return "", err
8181
}
@@ -86,7 +86,12 @@ func findPluginLocally(pluginInfo *plugin.PluginInfo) (string, error) {
8686
if f.IsDir() {
8787
continue
8888
}
89-
if f.Mode()&0o100 == 0 {
89+
fInfo, err := f.Info()
90+
if err != nil {
91+
return "", fmt.Errorf("failed to get file info for %s: %w", f.Name(), err)
92+
}
93+
// check if the file is executable by the user
94+
if fInfo.Mode()&0o100 == 0 {
9095
continue
9196
}
9297
return path.Join(vPth, f.Name()), nil

pkg/plugin/discovery/resolver/github/github.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package github
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"os"
99
"regexp"
@@ -18,21 +18,22 @@ import (
1818
"golang.org/x/oauth2"
1919
)
2020

21-
type GitHubResolver struct {
21+
type Resolver struct {
2222
ghClient *github.Client
2323
}
2424

25-
func NewResolver() *GitHubResolver {
25+
func NewResolver() *Resolver {
2626
var tc *http.Client
2727
if ghToken := os.Getenv("GITHUB_TOKEN"); ghToken != "" {
2828
tc = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: ghToken}))
2929
}
30-
return &GitHubResolver{
30+
return &Resolver{
3131
ghClient: github.NewClient(tc),
3232
}
3333
}
3434

35-
func (g *GitHubResolver) githubReleaseToDownloadInfo(repoOwner, repoName string, release *github.RepositoryRelease) (*resolver.PluginDownloadInfo, error) {
35+
//gocyclo:ignore
36+
func (g *Resolver) githubReleaseToDownloadInfo(repoOwner, repoName string, release *github.RepositoryRelease) (*resolver.PluginDownloadInfo, error) {
3637
var checksumAsset *github.ReleaseAsset
3738
var pluginAsset *github.ReleaseAsset
3839
osArchRe := regexp.MustCompile("(?i)" + runtime.GOOS + "(_|-)" + runtime.GOARCH)
@@ -56,7 +57,7 @@ func (g *GitHubResolver) githubReleaseToDownloadInfo(repoOwner, repoName string,
5657
if err != nil {
5758
return nil, err
5859
}
59-
checksumData, err := ioutil.ReadAll(checksumDownload)
60+
checksumData, err := io.ReadAll(checksumDownload)
6061
checksumDownload.Close()
6162
if err != nil {
6263
return nil, err
@@ -91,7 +92,7 @@ func (gr ghReleases) Len() int { return len(gr) }
9192
func (gr ghReleases) Less(i, j int) bool { return gr[j].version.LessThan(gr[i].version) }
9293
func (gr ghReleases) Swap(i, j int) { gr[i], gr[j] = gr[j], gr[i] }
9394

94-
func (g *GitHubResolver) getAllValidGitHubReleases(repoOwner, repoName string) (ghReleases, error) {
95+
func (g *Resolver) getAllValidGitHubReleases(repoOwner, repoName string) (ghReleases, error) {
9596
ret := make(ghReleases, 0)
9697
opts := &github.ListOptions{Page: 1, PerPage: 100}
9798
for {
@@ -118,7 +119,7 @@ func (g *GitHubResolver) getAllValidGitHubReleases(repoOwner, repoName string) (
118119
return ret, nil
119120
}
120121

121-
func (g *GitHubResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver.PluginDownloadInfo, error) {
122+
func (g *Resolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver.PluginDownloadInfo, error) {
122123
if pluginInfo.RepoSlug == "" {
123124
pluginInfo.RepoSlug = knownPlugins[pluginInfo.ShortNormalizedName]
124125
}
@@ -155,6 +156,6 @@ func (g *GitHubResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver
155156
return di, err
156157
}
157158

158-
func (g *GitHubResolver) Names() []string {
159+
func (g *Resolver) Names() []string {
159160
return []string{"github", "gh"}
160161
}

pkg/plugin/discovery/resolver/registry/registry.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ import (
1111
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver"
1212
)
1313

14-
type RegistryResolver struct{}
14+
type Resolver struct{}
1515

16-
func NewResolver() *RegistryResolver {
17-
return &RegistryResolver{}
16+
func NewResolver() *Resolver {
17+
return &Resolver{}
1818
}
1919

20-
func (r *RegistryResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver.PluginDownloadInfo, error) {
21-
pluginApiRes, err := getPluginInfo(pluginInfo.NormalizedName)
20+
func (r *Resolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver.PluginDownloadInfo, error) {
21+
pluginAPIRes, err := getPluginInfo(pluginInfo.NormalizedName)
2222
if err != nil {
2323
return nil, err
2424
}
2525

2626
foundVersion := ""
2727
if pluginInfo.Constraint == nil {
28-
foundVersion = pluginApiRes.LatestRelease
28+
foundVersion = pluginAPIRes.LatestRelease
2929
} else {
3030
versions := make(semver.Collection, 0)
31-
for v := range pluginApiRes.Versions {
31+
for v := range pluginAPIRes.Versions {
3232
pv, err := semver.NewVersion(v)
3333
if err != nil {
3434
return nil, err
@@ -48,7 +48,7 @@ func (r *RegistryResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolv
4848
return nil, errors.New("version not found")
4949
}
5050

51-
releaseAsset := pluginApiRes.Versions[foundVersion].getMatchingAsset()
51+
releaseAsset := pluginAPIRes.Versions[foundVersion].getMatchingAsset()
5252
if releaseAsset == nil {
5353
return nil, fmt.Errorf("a matching plugin was not found for %s/%s", runtime.GOOS, runtime.GOARCH)
5454
}
@@ -60,6 +60,6 @@ func (r *RegistryResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolv
6060
}, nil
6161
}
6262

63-
func (r *RegistryResolver) Names() []string {
63+
func (r *Resolver) Names() []string {
6464
return []string{"registry"}
6565
}

0 commit comments

Comments
 (0)