chore: lint code with golangci-lint (#1828)

* feat: add golangci-lint linting

* chore: fix linter issues

* feat: add linting into the workflow

* docs: update lint docs

* fix: cr suggestions

* fix: remove old formatting and vetting scripts

* fix: add docker make target

* fix: action go caching

* fix: depreciated actions checkout version

* fix: cr suggestion

* fix: cr suggestions

---------

Co-authored-by: Manu Gupta <manugupt1@gmail.com>
This commit is contained in:
Nicholas Wiersma
2023-02-25 06:39:17 +02:00
committed by GitHub
parent 66582eebfe
commit d932d50232
134 changed files with 596 additions and 600 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"github.com/gomods/athens/pkg/storage"
)
// Fetcher fetches module from an upstream source
// Fetcher fetches module from an upstream source.
type Fetcher interface {
// Fetch downloads the sources from an upstream and returns the corresponding
// .info, .mod, and .zip files.
+15 -16
View File
@@ -3,6 +3,7 @@ package module
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
@@ -14,15 +15,15 @@ var (
versionSeparator = "."
)
// Filter is a filter of modules
// Filter is a filter of modules.
type Filter struct {
root ruleNode
filePath string
}
// NewFilter creates new filter based on rules defined in a configuration file
// WARNING: this is not concurrently safe
// Configuration consists of two operations: + for include and - for exclude
// NewFilter creates new filter based on rules defined in a configuration file.
// WARNING: this is not concurrently safe.
// Configuration consists of two operations: + for include and - for exclude:
// e.g.
// - github.com/a
// - github.com/a/b
@@ -33,7 +34,7 @@ type Filter struct {
// -
// + github.com/a
//
// will exclude all items from communication except github.com/a
// will exclude all items from communication except github.com/a.
func NewFilter(filterFilePath string) (*Filter, error) {
// Do not return an error if the file path is empty
// Do not attempt to parse it as well.
@@ -42,10 +43,9 @@ func NewFilter(filterFilePath string) (*Filter, error) {
}
return initFromConfig(filterFilePath)
}
// AddRule adds rule for specified path
// AddRule adds rule for specified path.
func (f *Filter) AddRule(path string, qualifiers []string, rule FilterRule) {
f.ensurePath(path)
@@ -70,7 +70,7 @@ func (f *Filter) AddRule(path string, qualifiers []string, rule FilterRule) {
latest.next[last] = rn
}
// Rule returns the filter rule to be applied to the given path
// Rule returns the filter rule to be applied to the given path.
func (f *Filter) Rule(path, version string) FilterRule {
segs := getPathSegments(path)
rule := f.getAssociatedRule(version, segs...)
@@ -145,7 +145,6 @@ func initFromConfig(filePath string) (*Filter, error) {
f.root = rn
for idx, line := range lines {
// Ignore newline
if len(line) == 0 {
continue
@@ -160,7 +159,7 @@ func initFromConfig(filePath string) (*Filter, error) {
}
ruleSign := strings.TrimSpace(split[0])
rule := Default
var rule FilterRule
switch ruleSign {
case "+":
rule = Include
@@ -195,11 +194,11 @@ func initFromConfig(filePath string) (*Filter, error) {
// matches checks if the given version matches the given qualifier.
// Qualifiers can be:
// - plain versions
// - v1.2.3 enables v1.2.3
// - ~1.2.3: enables 1.2.x which are at least 1.2.3
// - ^1.2.3: enables 1.x.x which are at least 1.2.3
// - <1.2.3: enables everything lower than 1.2.3 includes 1.2.2 and 0.58.9 as well
// - plain versions.
// - v1.2.3 enables v1.2.3.
// - ~1.2.3: enables 1.2.x which are at least 1.2.3.
// - ^1.2.3: enables 1.x.x which are at least 1.2.3.
// - <1.2.3: enables everything lower than 1.2.3 includes 1.2.2 and 0.58.9 as well.
func matches(version, qualifier string) bool {
if len(qualifier) < 2 || len(version) < 1 {
return false
@@ -297,7 +296,7 @@ func newRule(r FilterRule) ruleNode {
func getConfigLines(filterFile string) ([]string, error) {
const op errors.Op = "module.getConfigLines"
f, err := os.Open(filterFile)
f, err := os.Open(filepath.Clean(filterFile))
if err != nil {
return nil, errors.E(op, err)
}
+6 -6
View File
@@ -1,16 +1,16 @@
package module
// FilterRule defines behavior of module communication
// FilterRule defines behavior of module communication.
type FilterRule int
const (
// Default filter rule does not alter default/parent behavior
// Default filter rule does not alter default/parent behavior.
Default FilterRule = iota
// Include treats modules the usual way
// Used for reverting Exclude of parent path
// Include treats modules the usual way.
// Used for reverting Exclude of parent path.
Include
// Exclude filter rule excludes package and its children from communication
// Exclude filter rule excludes package and its children from communication.
Exclude
// Direct filter rule forces the package to be fetched directly from upstream proxy
// Direct filter rule forces the package to be fetched directly from upstream proxy.
Direct
)
+8 -10
View File
@@ -35,7 +35,7 @@ type goModule struct {
GoModSum string `json:"goModSum"` // checksum for go.mod (as in go.sum)
}
// NewGoGetFetcher creates fetcher which uses go get tool to fetch modules
// NewGoGetFetcher creates fetcher which uses go get tool to fetch modules.
func NewGoGetFetcher(goBinaryName, gogetDir string, envVars []string, fs afero.Fs) (Fetcher, error) {
const op errors.Op = "module.NewGoGetFetcher"
if err := validGoBinary(goBinaryName); err != nil {
@@ -64,7 +64,7 @@ func (g *goGetFetcher) Fetch(ctx context.Context, mod, ver string) (*storage.Ver
sourcePath := filepath.Join(goPathRoot, "src")
modPath := filepath.Join(sourcePath, getRepoDirName(mod, ver))
if err := g.fs.MkdirAll(modPath, os.ModeDir|os.ModePerm); err != nil {
clearFiles(g.fs, goPathRoot)
_ = clearFiles(g.fs, goPathRoot)
return nil, errors.E(op, err)
}
@@ -72,14 +72,13 @@ func (g *goGetFetcher) Fetch(ctx context.Context, mod, ver string) (*storage.Ver
ctx,
g.goBinaryName,
g.envVars,
g.fs,
goPathRoot,
modPath,
mod,
ver,
)
if err != nil {
clearFiles(g.fs, goPathRoot)
_ = clearFiles(g.fs, goPathRoot)
return nil, errors.E(op, err)
}
@@ -116,7 +115,6 @@ func downloadModule(
ctx context.Context,
goBinaryName string,
envVars []string,
fs afero.Fs,
gopath,
repoRoot,
module,
@@ -137,7 +135,7 @@ func downloadModule(
err := cmd.Run()
if err != nil {
err = fmt.Errorf("%v: %s", err, stderr)
err = fmt.Errorf("%w: %s", err, stderr)
var m goModule
if jsonErr := json.NewDecoder(stdout).Decode(&m); jsonErr != nil {
return goModule{}, errors.E(op, err)
@@ -165,17 +163,17 @@ func isLimitHit(o string) bool {
}
// getRepoDirName takes a raw repository URI and a version and creates a directory name that the
// repository contents can be put into
// repository contents can be put into.
func getRepoDirName(repoURI, version string) string {
escapedURI := strings.Replace(repoURI, "/", "-", -1)
escapedURI := strings.ReplaceAll(repoURI, "/", "-")
return fmt.Sprintf("%s-%s", escapedURI, version)
}
func validGoBinary(name string) error {
const op errors.Op = "module.validGoBinary"
err := exec.Command(name).Run()
_, ok := err.(*exec.ExitError)
if err != nil && !ok {
eErr := &exec.ExitError{}
if err != nil && !errors.AsErr(err, &eErr) {
return errors.E(op, err)
}
return nil
+5 -5
View File
@@ -28,7 +28,7 @@ type vcsLister struct {
fs afero.Fs
}
// NewVCSLister creates an UpstreamLister which uses VCS to fetch a list of available versions
// NewVCSLister creates an UpstreamLister which uses VCS to fetch a list of available versions.
func NewVCSLister(goBinPath string, env []string, fs afero.Fs) UpstreamLister {
return &vcsLister{
goBinPath: goBinPath,
@@ -39,13 +39,13 @@ func NewVCSLister(goBinPath string, env []string, fs afero.Fs) UpstreamLister {
func (l *vcsLister) List(ctx context.Context, module string) (*storage.RevInfo, []string, error) {
const op errors.Op = "vcsLister.List"
ctx, span := observ.StartSpan(ctx, op.String())
_, span := observ.StartSpan(ctx, op.String())
defer span.End()
tmpDir, err := afero.TempDir(l.fs, "", "go-list")
if err != nil {
return nil, nil, errors.E(op, err)
}
defer l.fs.RemoveAll(tmpDir)
defer func() { _ = l.fs.RemoveAll(tmpDir) }()
cmd := exec.Command(
l.goBinPath,
@@ -62,12 +62,12 @@ func (l *vcsLister) List(ctx context.Context, module string) (*storage.RevInfo,
if err != nil {
return nil, nil, errors.E(op, err)
}
defer clearFiles(l.fs, gopath)
defer func() { _ = clearFiles(l.fs, gopath) }()
cmd.Env = prepareEnv(gopath, l.env)
err = cmd.Run()
if err != nil {
err = fmt.Errorf("%v: %s", err, stderr)
err = fmt.Errorf("%w: %s", err, stderr)
// as of now, we can't recognize between a true NotFound
// and an unexpected error, so we choose the more
// hopeful path of NotFound. This way the Go command
+1 -1
View File
@@ -9,7 +9,7 @@ import (
// prepareEnv will return all the appropriate
// environment variables for a Go Command to run
// successfully (such as GOPATH, GOCACHE, PATH etc)
// successfully (such as GOPATH, GOCACHE, PATH etc).
func prepareEnv(gopath string, envVars []string) []string {
gopathEnv := fmt.Sprintf("GOPATH=%s", gopath)
cacheEnv := fmt.Sprintf("GOCACHE=%s", filepath.Join(gopath, "cache"))
+6 -6
View File
@@ -14,10 +14,10 @@ type zipReadCloser struct {
goPath string
}
// Close closes the zip file handle and clears up disk space used by the underlying disk ref
// It is the caller's responsibility to call this method to free up utilized disk space
// Close closes the zip file handle and clears up disk space used by the underlying disk ref.
// It is the caller's responsibility to call this method to free up utilized disk space.
func (rc *zipReadCloser) Close() error {
rc.zip.Close()
_ = rc.zip.Close()
return clearFiles(rc.fs, rc.goPath)
}
@@ -25,8 +25,8 @@ func (rc *zipReadCloser) Read(p []byte) (n int, err error) {
return rc.zip.Read(p)
}
// clearFiles deletes all data from the given fs at path root
// This function must be called when zip is closed to cleanup the entire GOPATH created by the diskref
// clearFiles deletes all data from the given fs at path root.
// This function must be called when zip is closed to cleanup the entire GOPATH created by the diskref.
func clearFiles(fs afero.Fs, root string) error {
const op errors.Op = "module.ClearFiles"
// This is required because vgo ensures dependencies are read-only
@@ -36,7 +36,7 @@ func clearFiles(fs afero.Fs, root string) error {
if err != nil {
return err
}
return fs.Chmod(path, 0770)
return fs.Chmod(path, 0o770)
}
err := afero.Walk(fs, root, walkFn)
if err != nil {