Skip to content

Commit

Permalink
Merge pull request #31 from FogDong/feat-lint
Browse files Browse the repository at this point in the history
Feat: add lint config and fix the lint
  • Loading branch information
FogDong authored Sep 1, 2022
2 parents 04805ae + 76a6f59 commit 5f22206
Show file tree
Hide file tree
Showing 19 changed files with 256 additions and 32 deletions.
201 changes: 201 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
run:
timeout: 10m

skip-files:
- "zz_generated\\..+\\.go$"
- ".*_test.go$"

skip-dirs:
- "hack"
- "e2e"

output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number

linters-settings:
errcheck:
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: false

# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: false

# [deprecated] comma-separated list of pairs of the form pkg:regex
# the regex is used to ignore names within pkg. (default "fmt:.*").
# see https://github.com/kisielk/errcheck#the-deprecated-method for details
ignore: fmt:.*,io/ioutil:^Read.*

exhaustive:
# indicates that switch statements are to be considered exhaustive if a
# 'default' case is present, even if all enum members aren't listed in the
# switch
default-signifies-exhaustive: true

govet:
# report about shadowed variables
check-shadowing: false

gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true

goimports:
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
local-prefixes: github.com/oam-dev/kubevela

gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 30

maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true

dupl:
# tokens count to trigger issue, 150 by default
threshold: 100

goconst:
# minimal length of string constant, 3 by default
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 5

lll:
# tab width in spaces. Default to 1.
tab-width: 1

unused:
# treat code as a program (not a library) and report unused exported identifiers; default is false.
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false

unparam:
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false

nakedret:
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
max-func-lines: 30

gocritic:
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint` run to see all tags and checks.
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
enabled-tags:
- performance

settings: # settings passed to gocritic
captLocal: # must be valid enabled check name
paramsOnly: true
rangeValCopy:
sizeThreshold: 32

makezero:
# Allow only slices initialized with a length of zero. Default is false.
always: false

linters:
enable:
- megacheck
- govet
- gocyclo
- gocritic
- goconst
- goimports
- gofmt # We enable this as well as goimports for its simplify mode.
- revive
- unconvert
- misspell
- nakedret
fast: false


issues:
# Excluding configuration per-path and per-linter
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test(ing)?\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
- exportloopref
- unparam

# Ease some gocritic warnings on test files.
- path: _test\.go
text: "(unnamedResult|exitAfterDefer)"
linters:
- gocritic

# These are performance optimisations rather than style issues per se.
# They warn when function arguments or range values copy a lot of memory
# rather than using a pointer.
- text: "(hugeParam|rangeValCopy):"
linters:
- gocritic

# This "TestMain should call os.Exit to set exit code" warning is not clever
# enough to notice that we call a helper method that calls os.Exit.
- text: "SA3000:"
linters:
- staticcheck

- text: "k8s.io/api/core/v1"
linters:
- goimports

# This is a "potential hardcoded credentials" warning. It's triggered by
# any variable with 'secret' in the same, and thus hits a lot of false
# positives in Kubernetes land where a Secret is an object type.
- text: "G101:"
linters:
- gosec
- gas

# This is an 'errors unhandled' warning that duplicates errcheck.
- text: "G104:"
linters:
- gosec
- gas

# The Azure AddToUserAgent method appends to the existing user agent string.
# It returns an error if you pass it an empty string lettinga you know the
# user agent did not change, making it more of a warning.
- text: \.AddToUserAgent
linters:
- errcheck

- text: "don't use an underscore"
linters:
- revive

# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
# excluded by default patterns execute `golangci-lint run --help`.
# Default value for this option is true.
exclude-use-default: false

# Show only new issues: if there are unstaged changes or untracked files,
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
# It's a super-useful option for integration of golangci-lint into existing
# large codebase. It's not practical to fix all existing issues at the moment
# of integration: much better don't allow issues in new code.
# Default is false.
new: false

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0
4 changes: 2 additions & 2 deletions api/condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// A ConditionType represents a condition a resource could be in.
// nolint:golint
// nolint:revive
type ConditionType string

// Condition types.
Expand All @@ -45,7 +45,7 @@ const (
)

// A ConditionReason represents the reason a resource is in a condition.
// nolint:golint
// nolint:revive
type ConditionReason string

// Reasons a resource is or is not ready.
Expand Down
13 changes: 9 additions & 4 deletions api/v1alpha1/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ limitations under the License.
package v1alpha1

const (
ReasonExecute = "Execute"
// ReasonExecute is the reason for executing a workflow
ReasonExecute = "Execute"
// ReasonGenerate is the reason for generating a workflow
ReasonGenerate = "Generate"
)

const (
// MessageSuccessfully is the message for successfully
MessageSuccessfully = "WorkflowRun finished successfully"
MessageTerminated = "WorkflowRun finished with termination"

// MessageTerminated is the message for terminated
MessageTerminated = "WorkflowRun finished with termination"
// MessageFailedGenerate is the message for failed to generate
MessageFailedGenerate = "fail to generate workflow runners"
MessageFailedExecute = "fail to execute"
// MessageFailedExecute is the message for failed to execute
MessageFailedExecute = "fail to execute"
)
2 changes: 2 additions & 0 deletions api/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type WorkflowRunList struct {
Items []WorkflowRun `json:"items"`
}

// WorkflowRunSpec is the spec for the WorkflowRun
type WorkflowRunSpec struct {
Mode *WorkflowExecuteMode `json:"mode,omitempty"`
WorkflowSpec *WorkflowSpec `json:"workflowSpec,omitempty"`
Expand Down Expand Up @@ -208,6 +209,7 @@ func (wr *WorkflowRun) GetCondition(t condition.ConditionType) condition.Conditi
return wr.Status.GetCondition(t)
}

// WorkflowRunConditionType is a valid condition type for a WorkflowRun
const WorkflowRunConditionType string = "WorkflowRun"

// WorkflowStepPhase describes the phase of a workflow step.
Expand Down
1 change: 1 addition & 0 deletions charts/vela-workflow/crds/core.oam.dev_workflowruns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ spec:
metadata:
type: object
spec:
description: WorkflowRunSpec is the spec for the WorkflowRun
properties:
mode:
description: WorkflowExecuteMode defines the mode of workflow execution
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/agiledragon/gomonkey/v2 v2.4.0
github.com/crossplane/crossplane-runtime v0.14.1-0.20210722005935-0b469fcc77cd
github.com/evanphx/json-patch v4.12.0+incompatible
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/google/go-cmp v0.5.8
github.com/hashicorp/go-version v1.3.0
github.com/onsi/ginkgo v1.16.5
Expand All @@ -15,6 +16,7 @@ require (
github.com/prometheus/client_golang v1.11.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.23.6
Expand All @@ -38,7 +40,6 @@ require (
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/zapr v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand All @@ -65,7 +66,6 @@ require (
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
3 changes: 3 additions & 0 deletions pkg/backup/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"github.com/kubevela/workflow/api/v1alpha1"
)

// PersistType is the type of persister.
type PersistType string

const (
// PersistTypeSLS is the SLS persister.
PersistTypeSLS PersistType = "sls"
)

// NewPersister is a factory method for creating a persister.
func NewPersister(persistType PersistType) persistWorkflowRecord {
switch persistType {
case PersistTypeSLS:
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/delegating_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type delegatingReader struct {
scheme *runtime.Scheme
}

func (d *delegatingReader) shouldBypassCache(ctx context.Context, obj runtime.Object) (bool, error) {
func (d *delegatingReader) shouldBypassCache(obj runtime.Object) (bool, error) {
gvk, err := apiutil.GVKForObject(obj, d.scheme)
if err != nil {
return false, err
Expand All @@ -80,7 +80,7 @@ func (d *delegatingReader) shouldBypassCache(ctx context.Context, obj runtime.Ob

// Get retrieves an obj for a given object key from the Kubernetes Cluster.
func (d *delegatingReader) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
if isUncached, err := d.shouldBypassCache(ctx, obj); err != nil {
if isUncached, err := d.shouldBypassCache(obj); err != nil {
return err
} else if isUncached {
return d.ClientReader.Get(ctx, key, obj)
Expand All @@ -90,7 +90,7 @@ func (d *delegatingReader) Get(ctx context.Context, key client.ObjectKey, obj cl

// List retrieves list of objects for a given namespace and list options.
func (d *delegatingReader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if isUncached, err := d.shouldBypassCache(ctx, list); err != nil {
if isUncached, err := d.shouldBypassCache(list); err != nil {
return err
} else if isUncached {
return d.ClientReader.List(ctx, list, opts...)
Expand Down
Loading

0 comments on commit 5f22206

Please sign in to comment.