Skip to content

Commit

Permalink
update golangci-lint and clean (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaymckay authored Mar 8, 2022
1 parent ee97d74 commit 15f3e84
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 77 deletions.
172 changes: 97 additions & 75 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,27 @@ linters-settings:

# https://github.com/ultraware/funlen
funlen:
lines: 100
statements: 50
# accounting for comments
lines: 160
statements: 70

# https://github.com/golang/lint
golint:
min-confidence: 0

# https://github.com/tommy-muehle/go-mnd
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: argument,case,condition,return

# https://github.com/walle/lll
lll:
line-length: 100

# https://github.com/mdempsky/maligned
maligned:
suggest-new: true
# https://github.com/denis-tingaikin/go-header
goheader:
template: |-
Copyright (c) {{ YEAR }} Target Brands, Inc. All rights reserved.
Use of this source code is governed by the LICENSE file in this repository.
# https://github.com/client9/misspell
misspell:
locale: US

# https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/nolintlint
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
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
allow-leading-space: true # allow non-"machine-readable" format (ie. with leading space)
allow-unused: false # allow nolint directives that don't address a linting issue
require-explanation: true # require an explanation for nolint directives
require-specific: true # require nolint directives to be specific about which linter is being skipped

# This section provides the configuration for which linters
# golangci will execute. Several of them were disabled by
Expand All @@ -70,58 +59,91 @@ linters:

# enable a specific set of linters to run
enable:
- bodyclose
- deadcode # enabled by default
- dupl
- errcheck # enabled by default
- funlen
- goconst
- gocyclo
- godot
- gofmt
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
- gosimple # enabled by default
- govet # enabled by default
- ineffassign # enabled by default
- lll
- maligned
- misspell
- nakedret
- nolintlint
- staticcheck # enabled by default
- structcheck # enabled by default
- stylecheck
- typecheck # enabled by default
- unconvert
- unparam
- unused # enabled by default
- varcheck # enabled by default
- whitespace

- bidichk # checks for dangerous unicode character sequences
- bodyclose # checks whether HTTP response body is closed successfully
- contextcheck # check the function whether use a non-inherited context
- deadcode # finds unused code
- dupl # code clone detection
- errcheck # checks for unchecked errors
- errorlint # find misuses of errors
- exportloopref # check for exported loop vars
- funlen # detects long functions
- goconst # finds repeated strings that could be replaced by a constant
- gocyclo # computes and checks the cyclomatic complexity of functions
- godot # checks if comments end in a period
- gofmt # checks whether code was gofmt-ed
- goheader # checks is file header matches to pattern
- goimports # fixes imports and formats code in same style as gofmt
- gomoddirectives # manage the use of 'replace', 'retract', and 'excludes' directives in go.mod
- goprintffuncname # checks that printf-like functions are named with f at the end
- gosec # inspects code for security problems
- gosimple # linter that specializes in simplifying a code
- govet # reports suspicious constructs, ex. Printf calls whose arguments don't align with the format string
- ineffassign # detects when assignments to existing variables aren't used
- makezero # finds slice declarations with non-zero initial length
- misspell # finds commonly misspelled English words in comments
- nakedret # finds naked returns in functions greater than a specified function length
- nilerr # finds the code that returns nil even if it checks that the error is not nil
- noctx # noctx finds sending http request without context.Context
- nolintlint # reports ill-formed or insufficient nolint directives
- revive # linter for go
- staticcheck # applies static analysis checks, go vet on steroids
- structcheck # finds unused struct fields
- stylecheck # replacement for golint
- tenv # analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
- typecheck # parses and type-checks go code, like the front-end of a go compiler
- unconvert # remove unnecessary type conversions
- unparam # reports unused function parameters
- unused # checks for unused constants, variables, functions and types
- varcheck # finds unused global variables and constants
- whitespace # detects leading and trailing whitespace
- wsl # forces code to use empty lines

# static list of linters we know golangci can run but we've
# chosen to leave disabled for now
# - asciicheck
# - depguard
# - dogsled
# - exhaustive
# - gochecknoinits
# - gochecknoglobals
# - gocognit
# - gocritic
# - godox
# - goerr113
# - interfacer
# - nestif
# - noctx
# - prealloc
# - rowserrcheck
# - scopelint
# - testpackage
# - wsl
# - asciicheck - non-critical
# - cyclop - unused complexity metric
# - depguard - unused
# - dogsled - blanks allowed
# - durationcheck - unused
# - errname - unused
# - exhaustive - unused
# - exhaustivestruct - style preference
# - forbidigo - unused
# - forcetypeassert - unused
# - gci - use goimports
# - gochecknoinits - unused
# - gochecknoglobals - global variables allowed
# - gocognit - unused complexity metric
# - gocritic - style preference
# - godox - to be used in the future
# - goerr113 - to be used in the future
# - golint - archived, replaced with revive
# - gofumpt - use gofmt
# - gomnd - get too many false-positives
# - gomodguard - unused
# - ifshort - use both styles
# - ireturn - allow interfaces to be returned
# - importas - want flexibility with naming
# - lll - not too concerned about line length
# - interfacer - archived
# - nestif - non-critical
# - nilnil - style preference
# - nlreturn - style preference
# - maligned - archived, replaced with govet 'fieldalignment'
# - paralleltest - false-positives
# - prealloc - don't use
# - predeclared - unused
# - promlinter - style preference
# - rowserrcheck - unused
# - scopelint - deprecated - replaced with exportloopref
# - sqlclosecheck - unused
# - tagliatelle - use a mix of variable naming
# - testpackage - don't use this style of testing
# - thelper - false-positives
# - varnamelen - unused
# - wastedassign - duplicate functionality
# - wrapcheck - style preference

# This section provides the configuration for how golangci
# will report the issues it finds.
Expand All @@ -131,7 +153,7 @@ issues:
# prevent linters from running on *_test.go files
- path: _test\.go
linters:
- dupl
- funlen
- goconst
- gocyclo
- gomnd
1 change: 1 addition & 0 deletions cmd/vela-email/loginauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
return nil, errors.New("unknown fromServer")
}
}

return nil, nil
}
19 changes: 19 additions & 0 deletions cmd/vela-email/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (p *Plugin) Validate() error {
defer logrus.Trace("exited plugin.Validate")

logrus.Info("Validating Parameters...")

if len(p.Attachment.Filename) != 0 {
fileInfo, err := os.Stat(p.Attachment.Filename)
if errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -168,68 +169,85 @@ func (p *Plugin) Exec() error {
defer logrus.Trace("exited plugin.Execute")

logrus.Debug("Parsing Subject...")

subject, err := p.injectEnv(p.Email.Subject)
if err != nil {
return err
}

p.Email.Subject = subject

if len(p.Email.HTML) > 0 {
logrus.Debug("Parsing HTML...")

body, err := p.injectEnv(string(p.Email.HTML))
if err != nil {
return err
}

logrus.Debug("Parsing CSS...")

body, err = inliner.Inline(body)
if err != nil {
return err
}

p.Email.HTML = []byte(body)
} else {
logrus.Debug("Parsing Text...")

body, err := p.injectEnv(string(p.Email.Text))
if err != nil {
return err
}

p.Email.Text = []byte(body)
}

var auth smtp.Auth

switch strings.ToLower(p.Auth) {
case "plainauth":
logrus.Info("Using login authentication from smtp/PlainAuth...")

auth = smtp.PlainAuth("", p.SMTPHost.Username, p.SMTPHost.Password, p.SMTPHost.Host)
case "loginauth":
logrus.Info("Using login authentication from loginauth/LoginAuth...")

auth = LoginAuth(p.SMTPHost.Username, p.SMTPHost.Password)
default:
logrus.Info("Using no login authentication...")

auth = nil
}

host := p.SMTPHost.Host + ":" + p.SMTPHost.Port

switch strings.ToLower(p.SendType) {
case "starttls":
logrus.Info("Sending email with StartTLS...")

if err := p.Email.SendWithStartTLS(host, auth, p.TLSConfig); err != nil {
return fmt.Errorf("error sending with StartTLS: %w", err)
}
case "tls":
logrus.Info("Sending email with TLS...")

if err := p.Email.SendWithTLS(host, auth, p.TLSConfig); err != nil {
return fmt.Errorf("error sending with TLS: %w", err)
}
case "plain":
fallthrough
default:
logrus.Info("Sending email with Plain...")

if err := p.Email.Send(host, auth); err != nil {
return fmt.Errorf("error sending with Plain: %w", err)
}
}

logrus.Info("Plugin finished")

return nil
}

Expand Down Expand Up @@ -259,5 +277,6 @@ func stringToSlice(s []string) []string {
slice = append(slice, temp...)
}
}

return slice
}
3 changes: 2 additions & 1 deletion cmd/vela-email/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"errors"
"io"
"os"
"strings"
Expand Down Expand Up @@ -226,7 +227,7 @@ func TestValidateErrors(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
if err := test.parameters.Validate(); err == nil {
t.Errorf("Validate() should have raised an error")
} else if err != test.wantErr {
} else if !errors.Is(err, test.wantErr) {
t.Errorf("Validate() error = %v, wantErr = %v", err, test.wantErr)
}
})
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func New() *version.Version {

v, err := semver.NewVersion(Tag)
if err != nil {
fmt.Println(fmt.Errorf("unable to parse semantic version for %s: %v", Tag, err))
fmt.Println(fmt.Errorf("unable to parse semantic version for %s: %w", Tag, err))
}

return &version.Version{
Expand Down

0 comments on commit 15f3e84

Please sign in to comment.