From b248324e8aa9c2260dc4e3c1f4c9d81e235eecf8 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Wed, 22 May 2024 11:02:43 +0100 Subject: [PATCH] Linting. --- .golangci.yml | 48 ++++++++++++++++++++-------------------------- http/http.go | 7 +++++++ http/parameters.go | 23 +++++++++++++++------- http/service.go | 24 ++++++++++++----------- 4 files changed, 57 insertions(+), 45 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 99afd74..bd74379 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,6 +4,16 @@ # This file is not a configuration example, # it contains the exhaustive configuration with explanations of the options. +issues: + # Which files to exclude: they will be analyzed, but issues from them won't be reported. + # There is no need to include all autogenerated files, + # we confidently recognize autogenerated files. + # If it's not, please let us know. + # "/" will be replaced by current OS file path separator to properly work on Windows. + # Default: [] + exclude-files: + - ".*_ssz\\.go$" + # Options for analysis running. run: # The default concurrency value is the number of available CPU. @@ -39,16 +49,6 @@ run: # Default: true # skip-dirs-use-default: false - # Which files to skip: they will be analyzed, but issues from them won't be reported. - # Default value is empty list, - # but there is no need to include all autogenerated files, - # we confidently recognize autogenerated files. - # If it's not please let us know. - # "/" will be replaced by current OS file path separator to properly work on Windows. - skip-files: - - ".*_ssz\\.go$" - - ".*_encoding\\.go$" - # If set we pass it to "go list -mod={option}". From "go help modules": # If invoked with -mod=readonly, the go command is disallowed from the implicit # automatic updating of go.mod described above. Instead, it fails when any changes @@ -69,7 +69,7 @@ run: # Define the Go version limit. # Mainly related to generics support since go1.18. # Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.18 - # go: '1.20' + # go: '1.19' # output configuration options @@ -109,6 +109,10 @@ linters-settings: lll: line-length: 132 + nlreturn: + # Allow two-line blocks without requiring a newline + block-size: 3 + stylecheck: checks: [ "all", "-ST1000" ] @@ -129,48 +133,38 @@ linters: - contextcheck - cyclop - deadcode - - decorder - depguard - dupl - - dupword - - errchkjson - - errorlint + - err113 + - execinquery - exhaustive - exhaustivestruct - exhaustruct - - forbidigo - forcetypeassert - funlen - gci - gochecknoglobals - - gochecknoinits - gocognit - goconst - - goerr113 - - gofumpt - - goheader - golint - gomnd - ifshort - interfacer - ireturn + - inamedparam - lll - - maintidx - maligned + - mnd - musttag + - nestif - nilnil - nlreturn - - nolintlint - nosnakecase + - perfsprint - promlinter - - rowserrcheck - scopelint - - sqlclosecheck - structcheck - - thelper - varcheck - varnamelen - - wastedassign - - whitespace - wrapcheck - wsl diff --git a/http/http.go b/http/http.go index fc096c2..d30e0b6 100644 --- a/http/http.go +++ b/http/http.go @@ -54,6 +54,7 @@ func (s *Service) get(ctx context.Context, endpoint string) (ContentType, io.Rea return ContentTypeUnknown, nil, errors.Wrap(err, "failed to create GET request") } + s.addExtraHeaders(req) // Prefer SSZ if available. req.Header.Set("Accept", "application/octet-stream;q=1,application/json;q=0.9") span.AddEvent("Sending request") @@ -120,3 +121,9 @@ func contentTypeFromResp(resp *http.Response) (ContentType, error) { } return ParseFromMediaType(respContentType[0]) } + +func (s *Service) addExtraHeaders(req *http.Request) { + for k, v := range s.extraHeaders { + req.Header.Add(k, v) + } +} diff --git a/http/parameters.go b/http/parameters.go index 3f53cb8..7f15369 100644 --- a/http/parameters.go +++ b/http/parameters.go @@ -1,4 +1,4 @@ -// Copyright © 2022 Attestant Limited. +// Copyright © 2022, 2023 Attestant Limited. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -22,10 +22,11 @@ import ( ) type parameters struct { - logLevel zerolog.Level - monitor metrics.Service - address string - timeout time.Duration + logLevel zerolog.Level + monitor metrics.Service + address string + timeout time.Duration + extraHeaders map[string]string } // Parameter is the interface for service parameters. @@ -67,11 +68,19 @@ func WithTimeout(timeout time.Duration) Parameter { }) } +// WithExtraHeaders sets additional headers to be sent with each HTTP request. +func WithExtraHeaders(headers map[string]string) Parameter { + return parameterFunc(func(p *parameters) { + p.extraHeaders = headers + }) +} + // parseAndCheckParameters parses and checks parameters to ensure that mandatory parameters are present and correct. func parseAndCheckParameters(params ...Parameter) (*parameters, error) { parameters := parameters{ - logLevel: zerolog.GlobalLevel(), - timeout: 2 * time.Second, + logLevel: zerolog.GlobalLevel(), + timeout: 2 * time.Second, + extraHeaders: make(map[string]string), } for _, p := range params { if params != nil { diff --git a/http/service.go b/http/service.go index d838f8f..88656c9 100644 --- a/http/service.go +++ b/http/service.go @@ -1,4 +1,4 @@ -// Copyright © 2022 Attestant Limited. +// Copyright © 2022, 2023 Attestant Limited. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -32,11 +32,12 @@ import ( // Service is an Ethereum 2 client service. type Service struct { - base *url.URL - address string - client *http.Client - timeout time.Duration - pubkey *phase0.BLSPubKey + base *url.URL + address string + client *http.Client + timeout time.Duration + pubkey *phase0.BLSPubKey + extraHeaders map[string]string } // log is a service-wide logger. @@ -100,11 +101,12 @@ func New(ctx context.Context, params ...Parameter) (builderclient.Service, error base.User = nil } s := &Service{ - base: base, - address: base.String(), - client: client, - timeout: parameters.timeout, - pubkey: pubkey, + base: base, + address: base.String(), + client: client, + timeout: parameters.timeout, + pubkey: pubkey, + extraHeaders: parameters.extraHeaders, } // Close the service on context done.