Skip to content

Commit

Permalink
refactor: add lint step and fix linting issues
Browse files Browse the repository at this point in the history
Signed-off-by: ankitm123 <ankitmohapatra123@gmail.com>
  • Loading branch information
ankitm123 committed Nov 6, 2021
1 parent a300172 commit 546853e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 66 deletions.
60 changes: 16 additions & 44 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ linters-settings:
- wrapperFunc
- importShadow # not important for now
- unnamedResult # not important
- commentedOutCode
- nestingReduce
gocyclo:
min-complexity: 15
goimports:
goimports: {}
golint:
min-confidence: 0
gofmt:
Expand All @@ -47,9 +45,9 @@ linters-settings:
settings:
mnd:
# don't include the "operation" and "assign"
checks: argument,case,condition,return
checks: [argument, case, condition, return]
govet:
check-shadowing: false
check-shadowing: true
settings:
printf:
funcs:
Expand All @@ -62,7 +60,7 @@ linters-settings:
line-length: 140
maligned:
suggest-new: true
misspell:
misspell: {}
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
Expand All @@ -77,18 +75,13 @@ linters:
- bodyclose
- deadcode
- depguard
# - dogsled
- dupl
- errcheck
# - funlen
# - goconst
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- ineffassign
# - interfacer (This tool is deprecated)
- misspell
- nakedret
- rowserrcheck
Expand All @@ -99,52 +92,31 @@ linters:
- unparam
- unused
- varcheck
- golint
- revive
- gocritic
- govet
# - lll
# - gocyclo
# - nestif
# don't enable:
# - testpackage
# - gochecknoinits
# - stylecheck
# - lll
# - govet
# - whitespace
# - exhaustive (TODO: enable after next release; current release at time of writing is v1.27)
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - maligned
# - prealloc
# - wsl
# - nolintlint
# - gomnd
# - scopelint
- dupl
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# - path: _test\.go
# linters:
# - gomnd
# https://github.com/go-critic/go-critic/issues/926
# - path: _test\.go
# linters:
# - gomnd
# https://github.com/go-critic/go-critic/issues/926
- linters:
- gocritic
text: "unnecessaryDefer:"
exclude:
- 'shadow: declaration of "err" shadows declaration at'
max-same-issues: 0

run:
timeout: 30m
skip-dirs:
- test/testdata_etc
- internal/cache
- internal/renameio
- internal/robustio
- cmd/docs
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.29.x # use the fixed version to not introduce new linters unexpectedly
golangci-lint-version: 1.42.x # use the fixed version to not introduce new linters unexpectedly
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
8 changes: 7 additions & 1 deletion .lighthouse/jenkins-x/pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ spec:
resources: {}
- name: build-make-linux
resources: {}
- image: golangci/golangci-lint:v1.42.1-alpine
name: make-lint
resources: {}
script: |
#!/bin/sh
golangci-lint run
- name: build-make-test
resources: {}
- name: build-container-build
resources: {}
podTemplate: {}
serviceAccountName: tekton-bot
timeout: 240h0m0s
timeout: 1h0m0s
status: {}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ORG := jenkins-x-plugins
ORG_REPO := $(ORG)/$(NAME)
RELEASE_ORG_REPO := $(ORG_REPO)
ROOT_PACKAGE := github.com/$(ORG_REPO)
GO_VERSION := 1.13
GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/,*.go)

BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown')
Expand Down
5 changes: 2 additions & 3 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//nolint:gofmt,goimports
// +build !windows

package app

import (
"github.com/jenkins-x-plugins/jx-admin/pkg/cmd"
)
import "github.com/jenkins-x-plugins/jx-admin/pkg/cmd"

// Run runs the command, if args are not nil they will be set on the command
func Run(args []string) error {
Expand Down
3 changes: 1 addition & 2 deletions hack/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if ! [ -x "$(command -v golangci-lint)" ]; then
echo "Installing GolangCI-Lint"
${DIR}/install_golint.sh -b $GOPATH/bin v1.31.0
${DIR}/install_golint.sh -b $GOPATH/bin v1.42.1
fi

export GO111MODULE=on
golangci-lint run \
--timeout 30m \
--verbose \
--build-tags build
10 changes: 6 additions & 4 deletions pkg/bootjobs/bootjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package bootjobs

import (
"context"
"sort"
"strings"

"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/pkg/errors"
batchv1 "k8s.io/api/batch/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sort"
"strings"
)

// GetSortedJobs gets the boot jobs with an optional commit sha filter
func GetSortedJobs(client kubernetes.Interface, ns string, selector string, commitSHA string) ([]batchv1.Job, error) {
func GetSortedJobs(client kubernetes.Interface, ns, selector, commitSHA string) ([]batchv1.Job, error) {
jobList, err := client.BatchV1().Jobs(ns).List(context.TODO(), metav1.ListOptions{
LabelSelector: selector,
})
Expand All @@ -24,7 +25,8 @@ func GetSortedJobs(client kubernetes.Interface, ns string, selector string, comm
answer := jobList.Items
if commitSHA != "" {
var filtered []batchv1.Job
for _, job := range answer {
for k := range answer {
job := answer[k]
labels := job.Labels
if labels != nil {
sha := labels[LabelCommitSHA]
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func TestCreate(t *testing.T) {
outFile, err := ioutil.TempFile("", "")
require.NoError(t, err, "failed to create tempo file")
outFileName := outFile.Name()
args := append(tc.Args, "--git-server", "https://fake.com", "--git-kind", "fake", "--env-git-owner", "jstrachan", "--cluster", tc.Name, "--out", outFileName)
args = append(args, tc.Args...)
co.Args = args
tc.Args = append(tc.Args, "--git-server", "https://fake.com", "--git-kind", "fake", "--env-git-owner", "jstrachan", "--cluster", tc.Name, "--out", outFileName)

co.Args = tc.Args
co.Environment = tc.Environment
if co.Environment == "" {
co.Environment = "dev"
Expand Down
11 changes: 5 additions & 6 deletions pkg/cmd/joblog/joblog.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,13 @@ See: https://jenkins-x.io/docs/v3/guides/operator/

if o.CommitSHA != "" {
logger.Logger().Infof("waiting for boot Job pod with selector %s in namespace %s for commit SHA %s...", info(selector), info(ns), info(o.CommitSHA))

} else {
logger.Logger().Infof("waiting for boot Job pod with selector %s in namespace %s...", info(selector), info(ns))
}
return nil
}

func (o *Options) waitForActiveJob(client kubernetes.Interface, ns string, selector string, info func(a ...interface{}) string, containerName string) error {
func (o *Options) waitForActiveJob(client kubernetes.Interface, ns, selector string, info func(a ...interface{}) string, containerName string) error {
job, err := o.waitForLatestJob(client, ns, selector)
if err != nil {
return errors.Wrapf(err, "failed to wait for active Job in namespace %s with selector %v", ns, selector)
Expand All @@ -184,7 +183,7 @@ func (o *Options) waitForActiveJob(client kubernetes.Interface, ns string, selec
return o.viewActiveJobLog(client, ns, selector, containerName, job)
}

func (o *Options) viewActiveJobLog(client kubernetes.Interface, ns string, selector string, containerName string, job *batchv1.Job) error {
func (o *Options) viewActiveJobLog(client kubernetes.Interface, ns, selector, containerName string, job *batchv1.Job) error {
var foundPods []string
for {
complete, pod, err := o.waitForJobCompleteOrPodRunning(client, ns, selector, job.Name)
Expand Down Expand Up @@ -233,7 +232,7 @@ func (o *Options) viewActiveJobLog(client kubernetes.Interface, ns string, selec
}
}

func (o *Options) viewJobLog(client kubernetes.Interface, ns string, selector string, containerName string, job *batchv1.Job) error {
func (o *Options) viewJobLog(client kubernetes.Interface, ns, selector, containerName string, job *batchv1.Job) error {
opts := metav1.ListOptions{
LabelSelector: "job-name=" + job.Name,
}
Expand Down Expand Up @@ -436,7 +435,7 @@ func (o *Options) checkIfJobComplete(client kubernetes.Interface, ns, name strin
return false, job, nil
}

func (o *Options) pickJobToLog(client kubernetes.Interface, ns string, selector string, jobs []batchv1.Job) error {
func (o *Options) pickJobToLog(client kubernetes.Interface, ns, selector string, jobs []batchv1.Job) error {
var names []string
m := map[string]*batchv1.Job{}
for i := range jobs {
Expand All @@ -462,7 +461,7 @@ func (o *Options) pickJobToLog(client kubernetes.Interface, ns string, selector

func toJobName(j *batchv1.Job, number int) string {
status := JobStatus(j)
d := time.Now().Sub(j.CreationTimestamp.Time).Round(time.Minute)
d := time.Since(j.CreationTimestamp.Time).Round(time.Minute)
return fmt.Sprintf("#%d started %s %s", number, d.String(), status)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/reqhelpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ func applyDefaults(cmd *cobra.Command, r *jxcore.RequirementsConfig, flags *Requ
}

gitKind := r.Cluster.GitKind
gitKinds := append(giturl.KindGits, "fake")
if gitKind != "" && stringhelpers.StringArrayIndex(gitKinds, gitKind) < 0 {
giturl.KindGits = append(giturl.KindGits, "fake")
if gitKind != "" && stringhelpers.StringArrayIndex(giturl.KindGits, gitKind) < 0 {
return nil, options.InvalidOption("git-kind", gitKind, giturl.KindGits)
}

Expand Down

0 comments on commit 546853e

Please sign in to comment.