Skip to content

Commit

Permalink
Add golangci-lint to the project and fix issues
Browse files Browse the repository at this point in the history
fix all the issues found by golangci-lint and add it to the project
  • Loading branch information
chmouel committed May 3, 2024
1 parent 9601555 commit cf60eb8
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 22 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
name: build-test-publish

jobs:
build:
go:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
Expand All @@ -20,12 +20,17 @@ jobs:
- run: go env
- name: go build
run: go build -v ./...

- uses: alexellis/setup-arkade@v3
- uses: alexellis/arkade-get@master
with:
golangci-lint: latest
- name: Lint Go Code
run: |
make lint-go
e2e:
name: e2e tests
runs-on: ubuntu-latest
needs: [build]

needs: [go]
steps:
- uses: ko-build/[email protected]
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
Expand Down
128 changes: 128 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
issues:
exclude-dirs:
- vendor
- pkg/provider/gitea/structs
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gosec
run:
build-tags:
- e2e
linters-settings:
gocritic:
disabled-checks:
- unlambda
errcheck:
exclude-functions:
- (*github.com/tektoncd/pipeline/vendor/go.uber.org/zap.SugaredLogger).Sync
- flag.Set
- logger.Sync
- fmt.Fprintf
- fmt.Fprintln
- (io.Closer).Close
- updateConfigMap
linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
#- containedctx
#- contextcheck
#- cyclop
- decorder
#- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
#- exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
#- funlen
#- gci
- ginkgolinter
- gocheckcompilerdirectives
#- gochecknoglobals
- gochecknoinits
- gochecksumtype
#- gocognit
#- goconst
- gocritic
#- gocyclo
- godot
#- godox
#- goerr113
#- gofmt
- gofumpt
- goheader
- goimports
#- gomnd
#- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
#- inamedparam
#- interfacebloat
#- ireturn
#- lll
- loggercheck
#- maintidx
- makezero
- mirror
- misspell
#- musttag
- nakedret
#- nestif
- nilerr
#- nilnil
#- nlreturn
- noctx
#- nolintlint
#- nonamedreturns
- nosprintfhostport
#- paralleltest
#- perfsprint
- prealloc
- predeclared
- promlinter
- protogetter
- reassign
- revive
#- rowserrcheck
- sloglint
#- sqlclosecheck
- staticcheck
- stylecheck
- tagalign
#- tagliatelle
- tenv
- testableexamples
# - testifylint
#- testpackage
#- thelper
- tparallel
#- unconvert
- unparam
- unused
- usestdlibvars
#- varnamelen
#- wastedassign
- whitespace
#- wrapcheck
#- wsl
- zerologlint
25 changes: 23 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
E2E_TAG ?= e2e
REGISTRY_NAME ?= registry

e2e-coverage:
GOLANGCI_LINT=golangci-lint
TIMEOUT_UNIT = 20m
GOFUMPT=gofumpt

e2e-coverage: ## run e2e tests with coverage
@go test -failfast -count=1 -tags=$(E2E_TAG) ./tests -coverpkg=./... -coverprofile /tmp/coverage.out
@go tool cover -func /tmp/coverage.out
e2e: e2e-coverage

e2e-docker:
e2e-docker: ## run e2e tests with a docker registry started
@if [[ $$(docker ps --filter name=$(REGISTRY_NAME) --format '{{.ID}}') == "" ]]; then \
echo -n "Starting container $(REGISTRY_NAME) ... "; \
docker run --name $(REGISTRY_NAME) -p 127.0.0.1:5000:5000 -d registry:2 >/dev/null; \
echo "done"; \
fi
make e2e-coverage
@docker rm -f $(REGISTRY_NAME) >/dev/null

lint: lint-go
lint-go: ## runs go linter on all go files
@echo "Linting go files..."
@$(GOLANGCI_LINT) run ./... --modules-download-mode=vendor \
--max-issues-per-linter=0 \
--max-same-issues=0 \
--timeout $(TIMEOUT_UNIT)

.PHONY: fumpt ## formats the GO code with gofumpt(excludes vendors dir)
fumpt:
@find internal cmd tests -name '*.go'|xargs -P4 $(GOFUMPT) -w -extra

.PHONY: help
help: ## print this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

13 changes: 7 additions & 6 deletions cmd/cache/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
func fetchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "fetch",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
target, err := cmd.Flags().GetString(sourceFlag)
if err != nil {
return err
Expand All @@ -50,10 +50,9 @@ func fetchCmd() *cobra.Command {
return m
})
if len(matches) == 0 {
return fmt.Errorf("Didn't match any files with %v", patterns)
} else {
fmt.Fprintf(os.Stderr, "Matched the following files: %v\n", matches)
return fmt.Errorf("didn't match any files with %v", patterns)
}
fmt.Fprintf(os.Stderr, "Matched the following files: %v\n", matches)
// TODO: Hash files based of matches
hashStr, err := hash.Compute(matches)
if err != nil {
Expand Down Expand Up @@ -82,11 +81,13 @@ func fetchCmd() *cobra.Command {

func glob(root string, fn func(string) bool) []string {
var files []string
filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
if err := filepath.WalkDir(root, func(s string, _ fs.DirEntry, _ error) error {
if fn(s) {
files = append(files, s)
}
return nil
})
}); err != nil {
fmt.Fprintf(os.Stderr, "error walking the path %q: %v\n", root, err)
}
return files
}
2 changes: 1 addition & 1 deletion cmd/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
defer stop()
cache := rootCmd()
if err := cache.ExecuteContext(ctx); err != nil {
os.Exit(1)
os.Exit(1) //nolint:gocritic
}
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/cache/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
func uploadCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "upload",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
target, err := cmd.Flags().GetString(targetFlag)
if err != nil {
return err
Expand Down Expand Up @@ -48,10 +48,9 @@ func uploadCmd() *cobra.Command {
return m
})
if len(matches) == 0 {
return fmt.Errorf("Didn't match any files with %v", patterns)
} else {
fmt.Fprintf(os.Stderr, "Matched the following files: %v\n", matches)
return fmt.Errorf("didn't match any files with %v", patterns)
}
fmt.Fprintf(os.Stderr, "Matched the following files: %v\n", matches)

// TODO: Hash files based of matches
hashStr, err := hash.Compute(matches)
Expand Down
2 changes: 1 addition & 1 deletion internal/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func Fetch(ctx context.Context, hash, target, folder string, insecure bool) error {
// check that folder exists or automatically create it
if _, err := os.Stat(folder); os.IsNotExist(err) {
if err := os.MkdirAll(folder, 0755); err != nil {
if err := os.MkdirAll(folder, 0o755); err != nil {
return fmt.Errorf("failed to create folder: %w", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/oci/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/types"
)

func Upload(ctx context.Context, hash, target, folder string, insecure bool) error {
func Upload(_ context.Context, hash, target, folder string, insecure bool) error {
cacheImageRef := strings.ReplaceAll(target, "{{hash}}", hash)
fmt.Fprintf(os.Stderr, "Upload %s content to oci image %s\n", folder, cacheImageRef)

Expand Down
6 changes: 3 additions & 3 deletions tests/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
)

const (
DEFAULT_REG = "oci://127.0.0.1:5000/cache/go"
defaultReg = "oci://127.0.0.1:5000/cache/go"
goModContent = `module foo/bar/hello.moto`
hashOfGoModContent = "749da1a3a827cde86850743dd2bbf6b65d13497d4b0ecf88d1df7a77ce687f86"
)

func TestOCIUpload(t *testing.T) {
ctx := context.Background()
regTarget := DEFAULT_REG
regTarget := defaultReg
if os.Getenv("TARGET_REGISTRY") != "" {
regTarget = os.Getenv("TARGET_REGISTRY")
}
Expand All @@ -38,5 +38,5 @@ func TestOCIUpload(t *testing.T) {
assert.NilError(t, err)
assert.NilError(t, upload.Upload(ctx, hash, regTarget, tmpdir.Path(), true))
assert.NilError(t, fetch.Fetch(ctx, hash, regTarget, tmpdir.Path(), false))
assert.NilError(t, fetch.Fetch(ctx, "unknown", regTarget, tmpdir.Path(), false)) // should not error on unkown hash
assert.NilError(t, fetch.Fetch(ctx, "unknown", regTarget, tmpdir.Path(), false)) // should not error on unknown hash
}

0 comments on commit cf60eb8

Please sign in to comment.