-
Notifications
You must be signed in to change notification settings - Fork 16
fix: address golangci-lint warnings #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewkroh
wants to merge
15
commits into
elastic:main
Choose a base branch
from
andrewkroh:fix/linter-warnings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6bab08f
golangci.yml - ignore staticcheck QF1008
andrewkroh 6c42ce7
go.mod - add tool directives
andrewkroh ea0fba4
add godoc to packages and exported types
andrewkroh 2154be6
use blank identifier to unused parameters
andrewkroh 2ae7710
make context.Context the first func parameter
andrewkroh 2cfcb82
pass cmd context to GCP pub/sub API calls
andrewkroh 8404c8e
log errors from flag.Value.Set
andrewkroh f0730e5
do no modify data based into a io.Writer
andrewkroh 0e0a110
gofumpt
andrewkroh c818625
replaced deprecated io/ioutil with io and os packages
andrewkroh a4dea8f
replaced deprecated zap.OnFatal with zap.WithFatalHook
andrewkroh a04ba75
fix(output/gcs): context leak
andrewkroh 7fc84c5
fix(webhook): add io.Copy error check
andrewkroh bcb9bb1
fix(tls,udp): match package name to dir name
andrewkroh 42c0f59
add changelog
andrewkroh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ```release-note:bug | ||
| Fixes a bug in the webhook output where an error when reading the response body was ignored. | ||
| ``` | ||
|
|
||
| ```release-note:bug | ||
| Resolved a potential context leak in the GCS output by ensuring the cancel function is always called. | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| LICENSE := ASL2-Short | ||
| VERSION ?= local | ||
|
|
||
| GOIMPORTS := go run golang.org/x/tools/cmd/goimports | ||
| GOLANGCI_LINT:= go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint | ||
| GOLICENSER := go run github.com/elastic/go-licenser | ||
|
|
||
| check-fmt: | ||
| @${GOLICENSER} -d -license ${LICENSE} | ||
| @${GOIMPORTS} -l -e -local github.com/elastic . | read && echo "Code differs from gofmt's style. Run 'gofmt -w .'" 1>&2 && exit 1 || true | ||
| @${GOLANGCI_LINT} fmt --diff > /dev/null || (echo "Please run 'make fmt' to fix the formatting issues" 1>&2 && exit 1) | ||
|
|
||
| docker: | ||
| docker build -t docker.elastic.co/observability/stream:${VERSION} . | ||
|
|
||
| fmt: | ||
| ${GOLICENSER} -license ${LICENSE} | ||
| ${GOIMPORTS} -l -w -local github.com/elastic . | ||
| ${GOLANGCI_LINT} fmt ./... | ||
|
|
||
| .PHONY: check-fmt docker fmt | ||
| lint: | ||
| @${GOLANGCI_LINT} run ./... | ||
|
|
||
| .PHONY: check-fmt docker fmt lint |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,20 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more agreements. | ||
| // Elasticsearch B.V. licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| // Package httpserver provides a configurable mock HTTP server for testing and | ||
| // development purposes. It allows users to define request matching rules and | ||
| // dynamic templated responses via configuration files. Features include support | ||
| // for request sequencing, custom headers, authentication, TLS, and advanced | ||
| // response templating. | ||
| package httpserver | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
|
|
@@ -24,6 +30,7 @@ import ( | |
| "github.com/elastic/stream/internal/output" | ||
| ) | ||
|
|
||
| // Server is an HTTP server for mocking HTTP responses. | ||
| type Server struct { | ||
| logger *zap.SugaredLogger | ||
| opts *Options | ||
|
|
@@ -32,6 +39,7 @@ type Server struct { | |
| ctx context.Context | ||
| } | ||
|
|
||
| // Options are the options for the HTTP server. | ||
| type Options struct { | ||
| *output.Options | ||
| TLSCertificate string // TLS certificate file path. | ||
|
|
@@ -46,13 +54,14 @@ type Options struct { | |
| ExitOnUnmatchedRule bool // If true it will exit if a request does not match any rule. | ||
| } | ||
|
|
||
| // New creates a new HTTP server. | ||
| func New(opts *Options, logger *zap.SugaredLogger) (*Server, error) { | ||
| if opts.Addr == "" { | ||
| return nil, errors.New("a listen address is required") | ||
| } | ||
|
|
||
| if !(opts.TLSCertificate == "" && opts.TLSKey == "") && | ||
| !(opts.TLSCertificate != "" && opts.TLSKey != "") { | ||
| if (opts.TLSCertificate != "" || opts.TLSKey != "") && | ||
| (opts.TLSCertificate == "" || opts.TLSKey == "") { | ||
|
Comment on lines
+63
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or better |
||
| return nil, errors.New("both TLS certificate and key files must be defined") | ||
| } | ||
|
|
||
|
|
@@ -121,6 +130,7 @@ func New(opts *Options, logger *zap.SugaredLogger) (*Server, error) { | |
| }, nil | ||
| } | ||
|
|
||
| // Start starts the HTTP server. | ||
| func (o *Server) Start(ctx context.Context) error { | ||
| o.ctx = ctx | ||
|
|
||
|
|
@@ -144,6 +154,8 @@ func (o *Server) Start(ctx context.Context) error { | |
| return nil | ||
| } | ||
|
|
||
| // Close gracefully shuts down the server without interrupting any | ||
| // active connections. | ||
| func (o *Server) Close() error { | ||
| o.logger.Info("shutting down http-server...") | ||
|
|
||
|
|
@@ -231,7 +243,7 @@ func newHandlerFromConfig(config *config, notFoundHandler http.HandlerFunc, logg | |
| route.Queries(key, v) | ||
| } | ||
| } | ||
| route.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool { | ||
| route.MatcherFunc(func(r *http.Request, _ *mux.RouteMatch) bool { | ||
| for key := range exclude { | ||
| if r.URL.Query().Has(key) { | ||
| return false | ||
|
|
@@ -246,7 +258,7 @@ func newHandlerFromConfig(config *config, notFoundHandler http.HandlerFunc, logg | |
| } | ||
| } | ||
|
|
||
| route.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool { | ||
| route.MatcherFunc(func(r *http.Request, _ *mux.RouteMatch) bool { | ||
| user, password, _ := r.BasicAuth() | ||
| if rule.User != "" && user != rule.User { | ||
| return false | ||
|
|
@@ -266,15 +278,15 @@ func newHandlerFromConfig(config *config, notFoundHandler http.HandlerFunc, logg | |
| logger.Errorf("compiling body match regexp: %s", re, err) | ||
| } | ||
| } | ||
| route.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool { | ||
| route.MatcherFunc(func(r *http.Request, _ *mux.RouteMatch) bool { | ||
| if rule.RequestBody == "" { | ||
| return true | ||
| } | ||
| body, err := ioutil.ReadAll(r.Body) | ||
| body, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) | ||
| r.Body = io.NopCloser(bytes.NewBuffer(body)) | ||
| if bodyRE != nil { | ||
| return bodyRE.Match(body) | ||
| } | ||
|
|
@@ -304,7 +316,7 @@ func strRequest(r *http.Request) string { | |
| b.WriteString(fmt.Sprintf("'%s: %s' ", k, v)) | ||
| } | ||
| b.WriteString(", Request Body: ") | ||
| body, _ := ioutil.ReadAll(r.Body) | ||
| body, _ := io.ReadAll(r.Body) | ||
| b.Write(body) | ||
| return b.String() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just FYI, this could be
r.cmd.RunE = func(*cobra.Command, []string) error {. No need for change.