From ddcce006256daa1f92fcb9bdcfa2d2b812e95b36 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:25:35 +0100 Subject: [PATCH 1/5] update go to 1.23 --- Dockerfile | 2 +- go.mod | 2 +- hack/lint.Dockerfile | 2 +- hack/vendor.Dockerfile | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 39645911..a404c69c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # syntax=docker/dockerfile:1 -ARG GO_VERSION="1.21" +ARG GO_VERSION="1.23" ARG ALPINE_VERSION="3.18" ARG XX_VERSION="1.6.1" diff --git a/go.mod b/go.mod index 2433afe8..9ad7753f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/crazy-max/ftpgrab/v7 -go 1.21 +go 1.23.0 require ( github.com/alecthomas/kong v1.6.0 diff --git a/hack/lint.Dockerfile b/hack/lint.Dockerfile index aa587743..af03fca8 100644 --- a/hack/lint.Dockerfile +++ b/hack/lint.Dockerfile @@ -1,6 +1,6 @@ # syntax=docker/dockerfile:1 -ARG GO_VERSION="1.21" +ARG GO_VERSION="1.23" ARG ALPINE_VERSION="3.18" ARG GOLANGCI_LINT_VERSION="v1.55" diff --git a/hack/vendor.Dockerfile b/hack/vendor.Dockerfile index 021bfeb3..e2737a1c 100644 --- a/hack/vendor.Dockerfile +++ b/hack/vendor.Dockerfile @@ -1,6 +1,6 @@ # syntax=docker/dockerfile:1 -ARG GO_VERSION="1.21" +ARG GO_VERSION="1.23" ARG ALPINE_VERSION="3.18" ARG GOMOD_OUTDATED_VERSION="v0.8.0" From 3d965269851e8cd52294c9004b082aee12b38c2b Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:26:07 +0100 Subject: [PATCH 2/5] update alpine to 3.21 --- Dockerfile | 2 +- hack/lint.Dockerfile | 2 +- hack/vendor.Dockerfile | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index a404c69c..3df3450c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # syntax=docker/dockerfile:1 ARG GO_VERSION="1.23" -ARG ALPINE_VERSION="3.18" +ARG ALPINE_VERSION="3.21" ARG XX_VERSION="1.6.1" FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx diff --git a/hack/lint.Dockerfile b/hack/lint.Dockerfile index af03fca8..b8a62b16 100644 --- a/hack/lint.Dockerfile +++ b/hack/lint.Dockerfile @@ -1,7 +1,7 @@ # syntax=docker/dockerfile:1 ARG GO_VERSION="1.23" -ARG ALPINE_VERSION="3.18" +ARG ALPINE_VERSION="3.21" ARG GOLANGCI_LINT_VERSION="v1.55" FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS base diff --git a/hack/vendor.Dockerfile b/hack/vendor.Dockerfile index e2737a1c..86475c54 100644 --- a/hack/vendor.Dockerfile +++ b/hack/vendor.Dockerfile @@ -1,7 +1,7 @@ # syntax=docker/dockerfile:1 ARG GO_VERSION="1.23" -ARG ALPINE_VERSION="3.18" +ARG ALPINE_VERSION="3.21" ARG GOMOD_OUTDATED_VERSION="v0.8.0" FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS base From 65bde75690708ee0211cce1deaf014580259f868 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:26:28 +0100 Subject: [PATCH 3/5] update golangci-lint to 1.62 --- hack/lint.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/lint.Dockerfile b/hack/lint.Dockerfile index b8a62b16..00c9772c 100644 --- a/hack/lint.Dockerfile +++ b/hack/lint.Dockerfile @@ -2,7 +2,7 @@ ARG GO_VERSION="1.23" ARG ALPINE_VERSION="3.21" -ARG GOLANGCI_LINT_VERSION="v1.55" +ARG GOLANGCI_LINT_VERSION="v1.62" FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS base ENV GOFLAGS="-buildvcs=false" From bda2b759bc2ddc9977e6c8d58c521913f417cbd4 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:28:30 +0100 Subject: [PATCH 4/5] fix and update golangci-lint config --- .golangci.yml | 76 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a86d3d40..8c080056 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,41 +1,93 @@ run: timeout: 10m + # default uses Go version from the go.mod file, fallback on the env var + # `GOVERSION`, fallback on 1.17: https://golangci-lint.run/usage/configuration/#run-configuration + go: "1.23" linters: enable: + - bodyclose - depguard + - forbidigo + - gocritic - gofmt - goimports - - revive + - gosec + - gosimple - govet - - importas - ineffassign + - makezero - misspell + - noctx + - nolintlint + - revive + - staticcheck + - testifylint - typecheck - - errname - - makezero - - whitespace - unused + - whitespace disable-all: true linters-settings: + gocritic: + disabled-checks: + - "ifElseChain" + - "assignOp" + - "appendAssign" + - "singleCaseSwitch" + - "exitAfterDefer" # FIXME + govet: + enable: + - nilness + - unusedwrite depguard: rules: main: deny: - # The io/ioutil package has been deprecated. - # https://go.dev/doc/go1.16#ioutil - pkg: "io/ioutil" desc: The io/ioutil package has been deprecated. - importas: - no-unaliased: true + forbidigo: + forbid: + - '^fmt\.Errorf(# use errors\.Errorf instead)?$' + gosec: + excludes: + - G106 # Use of ssh InsecureIgnoreHostKey should be audited + - G115 # integer overflow conversion + - G204 # Audit use of command execution + - G402 # TLS MinVersion too low + config: + G306: "0644" + testifylint: + disable: + # disable rules that reduce the test condition + - "empty" + - "bool-compare" + - "len" + - "negative-positive" issues: + exclude-files: + - ".*\\.pb\\.go$" exclude-rules: - linters: - revive text: "stutters" + - linters: + - revive + text: "empty-block" + - linters: + - revive + text: "superfluous-else" + - linters: + - revive + text: "unused-parameter" + - linters: + - revive + text: "redefines-builtin-id" + - linters: + - revive + text: "if-return" -# show all -max-issues-per-linter: 0 -max-same-issues: 0 + # show all + max-issues-per-linter: 0 + max-same-issues: 0 From 4635752adcc3e0c45606dbd5b7d13e07ce94c295 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:34:00 +0100 Subject: [PATCH 5/5] fix lint issues --- cmd/main.go | 2 +- internal/journal/client.go | 2 +- internal/journal/journal.go | 2 +- internal/notif/script/client.go | 2 +- internal/notif/webhook/client.go | 20 +++++++++++++------- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8ac3fe31..45e1fd02 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -35,7 +35,7 @@ func main() { runtime.GOMAXPROCS(runtime.NumCPU()) meta.Version = version - meta.UserAgent = fmt.Sprintf("%s/%s go/%s %s", meta.ID, meta.Version, runtime.Version()[2:], strings.Title(runtime.GOOS)) + meta.UserAgent = fmt.Sprintf("%s/%s go/%s %s", meta.ID, meta.Version, runtime.Version()[2:], strings.Title(runtime.GOOS)) //nolint:staticcheck // ignoring "SA1019: strings.Title is deprecated", as for our use we don't need full unicode support if meta.Hostname, err = os.Hostname(); err != nil { log.Fatal().Err(err).Msg("Cannot resolve hostname") } diff --git a/internal/journal/client.go b/internal/journal/client.go index 9135dc49..8462cde7 100644 --- a/internal/journal/client.go +++ b/internal/journal/client.go @@ -25,5 +25,5 @@ func (c *Client) Add(entry Entry) { // IsEmpty checks if journal is empty func (c *Client) IsEmpty() bool { - return c.Entries == nil || len(c.Entries) == 0 + return len(c.Entries) == 0 } diff --git a/internal/journal/journal.go b/internal/journal/journal.go index 16b9d32c..0cfd7e6a 100644 --- a/internal/journal/journal.go +++ b/internal/journal/journal.go @@ -33,5 +33,5 @@ func (j Journal) MarshalJSON() ([]byte, error) { // IsEmpty checks if journal is empty func (j Journal) IsEmpty() bool { - return j.Entries == nil || len(j.Entries) == 0 + return len(j.Entries) == 0 } diff --git a/internal/notif/script/client.go b/internal/notif/script/client.go index 84d700d0..ab4ccda3 100644 --- a/internal/notif/script/client.go +++ b/internal/notif/script/client.go @@ -78,6 +78,6 @@ func (c *Client) Send(jnl journal.Journal) error { return errors.Wrap(err, strings.TrimSpace(stderr.String())) } - log.Debug().Msgf(strings.TrimSpace(stdout.String())) + log.Debug().Msg(strings.TrimSpace(stdout.String())) return nil } diff --git a/internal/notif/webhook/client.go b/internal/notif/webhook/client.go index 4516567d..964d09bf 100644 --- a/internal/notif/webhook/client.go +++ b/internal/notif/webhook/client.go @@ -2,6 +2,7 @@ package webhook import ( "bytes" + "context" "encoding/json" "net/http" @@ -34,10 +35,6 @@ func (c *Client) Name() string { // Send creates and sends a webhook notification with journal entries func (c *Client) Send(jnl journal.Journal) error { - hc := http.Client{ - Timeout: *c.cfg.Timeout, - } - body, err := json.Marshal(struct { Version string `json:"ftpgrab_version,omitempty"` ServerIP string `json:"server_ip,omitempty"` @@ -53,7 +50,11 @@ func (c *Client) Send(jnl journal.Journal) error { return err } - req, err := http.NewRequest(c.cfg.Method, c.cfg.Endpoint, bytes.NewBuffer(body)) + hc := http.Client{} + ctx, cancel := context.WithTimeout(context.Background(), *c.cfg.Timeout) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, c.cfg.Method, c.cfg.Endpoint, bytes.NewBuffer(body)) if err != nil { return err } @@ -66,6 +67,11 @@ func (c *Client) Send(jnl journal.Journal) error { req.Header.Set("User-Agent", c.meta.UserAgent) - _, err = hc.Do(req) - return err + resp, err := hc.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + return nil }