0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-24 13:10:39 +00:00

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

View File

@ -1,25 +1,92 @@
linters:
enable:
- gosimple
- deadcode
- typecheck
- govet
- errcheck
- staticcheck
- unused
- structcheck
- varcheck
- dupl
- gofmt
- misspell
- gocritic
- bidichk
- ineffassign
- revive
- gofumpt
- depguard
enable-all: false
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
run:
@ -28,4 +95,4 @@ run:
linters-settings:
gofumpt:
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
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
TARGETOS ?= linux

View File

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

View File

@ -4,7 +4,7 @@ import (
"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 {
slice []string
}
@ -15,6 +15,7 @@ func (s *StringSliceFlag) String() string {
func (s *StringSliceFlag) Set(value string) error {
s.slice = splitWithEscaping(value, ",", "\\")
return nil
}
@ -22,19 +23,20 @@ func (s *StringSliceFlag) Get() []string {
return s.slice
}
func splitWithEscaping(s, separator, escapeString string) []string {
if len(s) == 0 {
func splitWithEscaping(in, separator, escapeString string) []string {
if len(in) == 0 {
return []string{}
}
a := strings.Split(s, separator)
out := strings.Split(in, separator)
for i := len(a) - 2; i >= 0; i-- {
if strings.HasSuffix(a[i], escapeString) {
a[i] = a[i][:len(a[i])-len(escapeString)] + separator + a[i+1]
a = append(a[:i+1], a[i+2:]...)
//nolint:gomnd
for i := len(out) - 2; i >= 0; i-- {
if strings.HasSuffix(out[i], escapeString) {
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 {
strings := splitWithEscaping(test.Input, ",", "\\")
got, want := strings, test.Output
if !reflect.DeepEqual(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
go 1.19
go 1.20
require (
github.com/sirupsen/logrus v1.9.0

View File

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

View File

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

View File

@ -11,7 +11,7 @@ import (
"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 {
return []cli.Flag{
&cli.StringFlag{

View File

@ -11,6 +11,8 @@ import (
"github.com/urfave/cli/v2"
)
// Flags has the cli.Flags for the Drone plugin.
func Flags() []cli.Flag {
var (
FlagsBuildCategory = "Drone Build Flags"
FlagsRepoCategory = "Drone Repo Flags"
@ -22,8 +24,6 @@ var (
FlagsPluginCategory = "Plugin Flags"
)
// Flags has the cli.Flags for the Drone plugin.
func Flags() []cli.Flag {
flags := []cli.Flag{}
flags = append(flags, buildFlags(FlagsBuildCategory)...)