refactor: add more linters and fix findings (#27)

This commit is contained in:
Robert Kaussow 2023-02-08 10:16:01 +01:00 committed by GitHub
parent 2c054944d9
commit 8ac84bd4c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 145 additions and 65 deletions

View File

@ -8,7 +8,7 @@ platform:
steps: steps:
- name: deps - name: deps
image: golang:1.19 image: golang:1.20
commands: commands:
- make deps - make deps
volumes: volumes:
@ -16,7 +16,7 @@ steps:
path: /go path: /go
- name: lint - name: lint
image: golang:1.19 image: golang:1.20
commands: commands:
- make lint - make lint
volumes: volumes:
@ -24,7 +24,7 @@ steps:
path: /go path: /go
- name: test - name: test
image: golang:1.19 image: golang:1.20
commands: commands:
- make test - make test
volumes: volumes:
@ -156,6 +156,6 @@ depends_on:
--- ---
kind: signature kind: signature
hmac: 9cadf4dc495cf5ea9e54b4964eab9fd7a545d6b419c7cd78bc6846108428a926 hmac: ebcc6ff0a66ff606330d4e9fc8a2015e36b7c592e6278a36303e4970aa195575
... ...

View File

@ -1,25 +1,92 @@
linters: linters:
enable:
- gosimple
- deadcode
- typecheck
- govet
- errcheck
- staticcheck
- unused
- structcheck
- varcheck
- dupl
- gofmt
- misspell
- gocritic
- bidichk
- ineffassign
- revive
- gofumpt
- depguard
enable-all: false enable-all: false
disable-all: true disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- decorder
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forcetypeassert
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- grouper
- importas
- interfacebloat
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
# - rowserrcheck
# - sqlclosecheck
# - structcheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
# - wastedassign
- whitespace
- wsl
fast: false fast: false
run: run:
@ -28,4 +95,4 @@ run:
linters-settings: linters-settings:
gofumpt: gofumpt:
extra-rules: true extra-rules: true
lang-version: "1.18" lang-version: "1.20"

View File

@ -19,7 +19,7 @@ GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(G
XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest
GENERATE ?= GENERATE ?=
XGO_VERSION := go-1.19.x XGO_VERSION := go-1.20.x
XGO_TARGETS ?= linux/amd64,linux/arm64,linux/arm-6,linux/arm-7 XGO_TARGETS ?= linux/amd64,linux/arm64,linux/arm-6,linux/arm-7
TARGETOS ?= linux TARGETOS ?= linux

View File

@ -17,6 +17,7 @@ type Network struct {
// //
// If `trace` logging is requested the context will use `httptrace` to // If `trace` logging is requested the context will use `httptrace` to
// capture all network requests. // capture all network requests.
//nolint:containedctx
Context context.Context Context context.Context
/// Whether SSL verification is skipped /// Whether SSL verification is skipped

View File

@ -4,7 +4,7 @@ import (
"strings" "strings"
) )
// StringSliceFlag is a flag type which support comma separated values and escaping to not split at unwanted lines // StringSliceFlag is a flag type which support comma separated values and escaping to not split at unwanted lines.
type StringSliceFlag struct { type StringSliceFlag struct {
slice []string slice []string
} }
@ -15,6 +15,7 @@ func (s *StringSliceFlag) String() string {
func (s *StringSliceFlag) Set(value string) error { func (s *StringSliceFlag) Set(value string) error {
s.slice = splitWithEscaping(value, ",", "\\") s.slice = splitWithEscaping(value, ",", "\\")
return nil return nil
} }
@ -22,19 +23,20 @@ func (s *StringSliceFlag) Get() []string {
return s.slice return s.slice
} }
func splitWithEscaping(s, separator, escapeString string) []string { func splitWithEscaping(in, separator, escapeString string) []string {
if len(s) == 0 { if len(in) == 0 {
return []string{} return []string{}
} }
a := strings.Split(s, separator) out := strings.Split(in, separator)
for i := len(a) - 2; i >= 0; i-- { //nolint:gomnd
if strings.HasSuffix(a[i], escapeString) { for i := len(out) - 2; i >= 0; i-- {
a[i] = a[i][:len(a[i])-len(escapeString)] + separator + a[i+1] if strings.HasSuffix(out[i], escapeString) {
a = append(a[:i+1], a[i+2:]...) out[i] = out[i][:len(out[i])-len(escapeString)] + separator + out[i+1]
out = append(out[:i+1], out[i+2:]...)
} }
} }
return a return out
} }

View File

@ -20,6 +20,7 @@ func TestSplitWithEscaping(t *testing.T) {
for _, test := range tests { for _, test := range tests {
strings := splitWithEscaping(test.Input, ",", "\\") strings := splitWithEscaping(test.Input, ",", "\\")
got, want := strings, test.Output got, want := strings, test.Output
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("Got tag %v, want %v", got, want) t.Errorf("Got tag %v, want %v", got, want)
} }

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/thegeeklab/drone-plugin-lib/v2 module github.com/thegeeklab/drone-plugin-lib/v2
go 1.19 go 1.20
require ( require (
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0

View File

@ -49,6 +49,7 @@ func HTTP(ctx context.Context) context.Context {
"code": code, "code": code,
"header": header, "header": header,
}).Trace("ClientTrace.Got1xxxResponse") }).Trace("ClientTrace.Got1xxxResponse")
return nil return nil
}, },
@ -83,14 +84,14 @@ func HTTP(ctx context.Context) context.Context {
logrus.Trace("ClientTrace.TLSHandshakeStart") logrus.Trace("ClientTrace.TLSHandshakeStart")
}, },
TLSHandshakeDone: func(cs tls.ConnectionState, err error) { TLSHandshakeDone: func(connState tls.ConnectionState, err error) {
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"version": cs.Version, "version": connState.Version,
"handshake-complete": cs.HandshakeComplete, "handshake-complete": connState.HandshakeComplete,
"did-resume": cs.DidResume, "did-resume": connState.DidResume,
"cipher-suite": cs.CipherSuite, "cipher-suite": connState.CipherSuite,
"negotiated-protocol": cs.NegotiatedProtocol, "negotiated-protocol": connState.NegotiatedProtocol,
"server-name": cs.ServerName, "server-name": connState.ServerName,
"error": err, "error": err,
}).Trace("ClientTrace.TLSHandshakeDone") }).Trace("ClientTrace.TLSHandshakeDone")
}, },

View File

@ -19,6 +19,13 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
const (
NetDailerTimeout = 30 * time.Second
HTTPTransportIdleTimeout = 90 * time.Second
HTTPTransportTLSHandshakeTimeout = 10 * time.Second
HTTPTransportMaxIdleConns = 100
)
// networkFlags has the cli.Flags for the drone.Network. // networkFlags has the cli.Flags for the drone.Network.
func networkFlags(category string) []cli.Flag { func networkFlags(category string) []cli.Flag {
return []cli.Flag{ return []cli.Flag{
@ -32,35 +39,36 @@ func networkFlags(category string) []cli.Flag {
} }
// NetworkFromContext creates a drone.Network from the cli.Context. // NetworkFromContext creates a drone.Network from the cli.Context.
func NetworkFromContext(c *cli.Context) drone.Network { func NetworkFromContext(ctx *cli.Context) drone.Network {
dialer := &net.Dialer{ dialer := &net.Dialer{
Timeout: 30 * time.Second, Timeout: NetDailerTimeout,
KeepAlive: 30 * time.Second, KeepAlive: NetDailerTimeout,
DualStack: true, DualStack: true,
} }
transport := &http.Transport{ transport := &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext, DialContext: dialer.DialContext,
MaxIdleConns: 100, MaxIdleConns: HTTPTransportMaxIdleConns,
IdleConnTimeout: 90 * time.Second, IdleConnTimeout: HTTPTransportIdleTimeout,
TLSHandshakeTimeout: 10 * time.Second, TLSHandshakeTimeout: HTTPTransportTLSHandshakeTimeout,
ExpectContinueTimeout: 1 * time.Second, ExpectContinueTimeout: 1 * time.Second,
} }
ctx := context.Background() context := context.Background()
skipVerify := c.Bool("transport.skip-verify") skipVerify := ctx.Bool("transport.skip-verify")
if skipVerify { if skipVerify {
logrus.Warning("ssl verification is turned off") logrus.Warning("ssl verification is turned off")
transport.TLSClientConfig = &tls.Config{ transport.TLSClientConfig = &tls.Config{
//nolint:gosec
InsecureSkipVerify: true, InsecureSkipVerify: true,
} }
} }
if c.String("log-level") == logrus.TraceLevel.String() { if ctx.String("log-level") == logrus.TraceLevel.String() {
ctx = trace.HTTP(ctx) context = trace.HTTP(context)
} }
client := &http.Client{ client := &http.Client{
@ -68,7 +76,7 @@ func NetworkFromContext(c *cli.Context) drone.Network {
} }
return drone.Network{ return drone.Network{
Context: ctx, Context: context,
SkipVerify: skipVerify, SkipVerify: skipVerify,
Client: client, Client: client,
} }

View File

@ -11,7 +11,7 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
// repoFlags has the cli.Flags for the drone.Repo // repoFlags has the cli.Flags for the drone.Repo.
func repoFlags(category string) []cli.Flag { func repoFlags(category string) []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{

View File

@ -11,7 +11,9 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
var ( // Flags has the cli.Flags for the Drone plugin.
func Flags() []cli.Flag {
var (
FlagsBuildCategory = "Drone Build Flags" FlagsBuildCategory = "Drone Build Flags"
FlagsRepoCategory = "Drone Repo Flags" FlagsRepoCategory = "Drone Repo Flags"
FlagsCommitCategory = "Drone Commit Flags" FlagsCommitCategory = "Drone Commit Flags"
@ -20,10 +22,8 @@ var (
FlagsVersioningCategory = "Drone Versioning Flags" FlagsVersioningCategory = "Drone Versioning Flags"
FlagsSystemCategory = "Drone System Flags" FlagsSystemCategory = "Drone System Flags"
FlagsPluginCategory = "Plugin Flags" FlagsPluginCategory = "Plugin Flags"
) )
// Flags has the cli.Flags for the Drone plugin.
func Flags() []cli.Flag {
flags := []cli.Flag{} flags := []cli.Flag{}
flags = append(flags, buildFlags(FlagsBuildCategory)...) flags = append(flags, buildFlags(FlagsBuildCategory)...)