Skip to content

Commit eb44621

Browse files
committed
Apply manual fixes to appease linters
Drop enabling govets shadow because it's too annoying
1 parent e7ee285 commit eb44621

File tree

19 files changed

+77
-79
lines changed

19 files changed

+77
-79
lines changed

.golangci.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ linters-settings:
1515
- whyNoLint
1616
- wrapperFunc
1717
gocyclo:
18-
min-complexity: 15
18+
min-complexity: 31
1919
gofumpt:
2020
extra-rules: true
21-
govet:
22-
enable:
23-
- shadow
2421
lll:
2522
line-length: 140
2623
misspell:

bmc/firmware.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88

99
"github.com/bmc-toolbox/bmclib/v2/constants"
10-
bconsts "github.com/bmc-toolbox/bmclib/v2/constants"
1110
bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors"
1211
"github.com/hashicorp/go-multierror"
1312
"github.com/pkg/errors"
@@ -493,7 +492,7 @@ type FirmwareTaskVerifier interface {
493492
// return values:
494493
// state - returns one of the FirmwareTask statuses (see devices/constants.go).
495494
// status - returns firmware task progress or other arbitrary task information.
496-
FirmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error)
495+
FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error)
497496
}
498497

499498
// firmwareTaskVerifierProvider is an internal struct to correlate an implementation/provider and its name
@@ -504,7 +503,7 @@ type firmwareTaskVerifierProvider struct {
504503

505504
// firmwareTaskStatus returns the status of the firmware upload process.
506505

507-
func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []firmwareTaskVerifierProvider) (state constants.TaskState, status string, metadata Metadata, err error) {
506+
func firmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string, generic []firmwareTaskVerifierProvider) (state constants.TaskState, status string, metadata Metadata, err error) {
508507
metadata = newMetadata()
509508

510509
for _, elem := range generic {
@@ -534,7 +533,7 @@ func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, c
534533
}
535534

536535
// FirmwareTaskStatusFromInterfaces identifies implementations of the FirmwareTaskVerifier interface and passes the found implementations to the firmwareTaskStatus() wrapper.
537-
func FirmwareTaskStatusFromInterfaces(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []interface{}) (state constants.TaskState, status string, metadata Metadata, err error) {
536+
func FirmwareTaskStatusFromInterfaces(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string, generic []interface{}) (state constants.TaskState, status string, metadata Metadata, err error) {
538537
metadata = newMetadata()
539538

540539
implementations := make([]firmwareTaskVerifierProvider, 0)

bmc/inventory_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/bmc-toolbox/bmclib/v2/errors"
9-
bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors"
109
"github.com/bmc-toolbox/common"
1110
"github.com/stretchr/testify/assert"
1211
)
@@ -73,7 +72,7 @@ func TestInventoryFromInterfaces(t *testing.T) {
7372
badImplementation bool
7473
}{
7574
{"success with metadata", &common.Device{Common: common.Common{Vendor: "foo"}}, nil, 5 * time.Second, "foo", 1, false},
76-
{"failure with bad implementation", nil, bmclibErrs.ErrProviderImplementation, 5 * time.Second, "foo", 1, true},
75+
{"failure with bad implementation", nil, errors.ErrProviderImplementation, 5 * time.Second, "foo", 1, true},
7776
}
7877

7978
for _, tc := range testCases {

client.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,20 @@ func (c *Client) defaultTimeout(ctx context.Context) time.Duration {
148148
return time.Until(deadline) / time.Duration(l)
149149
}
150150

151+
func cloneHTTPClient(old *http.Client) *http.Client {
152+
client := *old
153+
transport, ok := client.Transport.(*http.Transport)
154+
if !ok {
155+
panic(fmt.Errorf("expected an http client of type http.Transport, got %T instead", client.Transport))
156+
}
157+
client.Transport = transport.Clone()
158+
return &client
159+
}
160+
151161
func (c *Client) registerRPCProvider() error {
152162
driverRPC := rpc.New(c.providerConfig.rpc.ConsumerURL, c.Auth.Host, c.providerConfig.rpc.Opts.HMAC.Secrets)
153163
c.providerConfig.rpc.Logger = c.Logger
154-
httpClient := *c.httpClient
155-
httpClient.Transport = c.httpClient.Transport.(*http.Transport).Clone()
156-
c.providerConfig.rpc.HTTPClient = &httpClient
164+
c.providerConfig.rpc.HTTPClient = cloneHTTPClient(c.httpClient)
157165
if err := mergo.Merge(driverRPC, c.providerConfig.rpc, mergo.WithOverride, mergo.WithTransformers(&rpc.Provider{})); err != nil {
158166
return fmt.Errorf("failed to merge user specified rpc config with the config defaults, rpc provider not available: %w", err)
159167
}
@@ -183,18 +191,15 @@ func (c *Client) registerIPMIProvider() error {
183191

184192
// register ASRR vendorapi provider
185193
func (c *Client) registerASRRProvider() {
186-
asrHttpClient := *c.httpClient
187-
asrHttpClient.Transport = c.httpClient.Transport.(*http.Transport).Clone()
188-
driverAsrockrack := asrockrack.NewWithOptions(c.Auth.Host+":"+c.providerConfig.asrock.Port, c.Auth.User, c.Auth.Pass, c.Logger, asrockrack.WithHTTPClient(&asrHttpClient))
194+
asrHttpClient := cloneHTTPClient(c.httpClient)
195+
driverAsrockrack := asrockrack.NewWithOptions(c.Auth.Host+":"+c.providerConfig.asrock.Port, c.Auth.User, c.Auth.Pass, c.Logger, asrockrack.WithHTTPClient(asrHttpClient))
189196
c.Registry.Register(asrockrack.ProviderName, asrockrack.ProviderProtocol, asrockrack.Features, nil, driverAsrockrack)
190197
}
191198

192199
// register gofish provider
193200
func (c *Client) registerGofishProvider() {
194-
gfHttpClient := *c.httpClient
195-
gfHttpClient.Transport = c.httpClient.Transport.(*http.Transport).Clone()
196201
gofishOpts := []redfish.Option{
197-
redfish.WithHttpClient(&gfHttpClient),
202+
redfish.WithHttpClient(cloneHTTPClient(c.httpClient)),
198203
redfish.WithVersionsNotCompatible(c.providerConfig.gofish.VersionsNotCompatible),
199204
redfish.WithUseBasicAuth(c.providerConfig.gofish.UseBasicAuth),
200205
redfish.WithPort(c.providerConfig.gofish.Port),
@@ -233,29 +238,25 @@ func (c *Client) registerDellProvider() {
233238

234239
// register supermicro vendorapi provider
235240
func (c *Client) registerSupermicroProvider() {
236-
smcHttpClient := *c.httpClient
237-
smcHttpClient.Transport = c.httpClient.Transport.(*http.Transport).Clone()
238241
driverSupermicro := supermicro.NewClient(
239242
c.Auth.Host,
240243
c.Auth.User,
241244
c.Auth.Pass,
242245
c.Logger,
243-
supermicro.WithHttpClient(&smcHttpClient),
246+
supermicro.WithHttpClient(cloneHTTPClient(c.httpClient)),
244247
supermicro.WithPort(c.providerConfig.supermicro.Port),
245248
)
246249

247250
c.Registry.Register(supermicro.ProviderName, supermicro.ProviderProtocol, supermicro.Features, nil, driverSupermicro)
248251
}
249252

250253
func (c *Client) registerOpenBMCProvider() {
251-
httpClient := *c.httpClient
252-
httpClient.Transport = c.httpClient.Transport.(*http.Transport).Clone()
253254
driver := openbmc.New(
254255
c.Auth.Host,
255256
c.Auth.User,
256257
c.Auth.Pass,
257258
c.Logger,
258-
openbmc.WithHttpClient(&httpClient),
259+
openbmc.WithHttpClient(cloneHTTPClient(c.httpClient)),
259260
openbmc.WithPort(c.providerConfig.openbmc.Port),
260261
)
261262

@@ -371,7 +372,7 @@ func (c *Client) Open(ctx context.Context) error {
371372
}
372373

373374
// Close pass through to library function
374-
func (c *Client) Close(ctx context.Context) (err error) {
375+
func (c *Client) Close(ctx context.Context) (err error) { // nolint:contextcheck
375376
ctx, span := c.traceprovider.Tracer(pkgName).Start(ctx, "Close")
376377
defer span.End()
377378

client_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func TestWithConnectionTimeout(t *testing.T) {
138138
}
139139

140140
func TestDefaultTimeout(t *testing.T) {
141+
// nolint:containedctx
141142
tests := map[string]struct {
142143
ctx context.Context
143144
want func(n int) time.Duration
@@ -209,9 +210,9 @@ func (t *testProvider) BootDeviceSet(ctx context.Context, bootDevice string, set
209210
}
210211

211212
func registryNames(r []*registrar.Driver) []string {
212-
var names []string
213-
for _, d := range r {
214-
names = append(names, d.Name)
213+
names := make([]string, len(r))
214+
for i := range r {
215+
names[i] = r[i].Name
215216
}
216217
return names
217218
}

internal/httpclient/httpclient_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package httpclient
22

33
import (
44
"crypto/x509"
5+
"errors"
56
"fmt"
67
"net/http"
78
"net/http/httptest"
@@ -52,17 +53,17 @@ func TestBuildWithOptions(t *testing.T) {
5253
opts = append(opts, SecureTLSOption(tc.withCertPool(server.Certificate())))
5354
}
5455
client := Build(opts...)
55-
req, _ := http.NewRequest(http.MethodGet, server.URL, nil)
56+
req, _ := http.NewRequest(http.MethodGet, server.URL, http.NoBody) // nolint:noctx
5657

57-
_, err := client.Do(req)
58+
resp, err := client.Do(req)
5859
if tc.wantErr {
5960
if err == nil {
6061
t.Fatal("Missing expected error")
6162
}
6263

6364
// Different versions of Go return different error messages so we just
6465
// check that its a *url.Error{}
65-
if _, ok := err.(*url.Error); !ok {
66+
if errors.Is(err, &url.Error{}) {
6667
t.Fatalf("Missing expected error: got %T: '%s'", err, err)
6768
}
6869
return
@@ -71,6 +72,8 @@ func TestBuildWithOptions(t *testing.T) {
7172
if err != nil {
7273
t.Fatalf("Got unexpected error %s", err)
7374
}
75+
76+
resp.Body.Close()
7477
})
7578
}
7679
}

internal/redfishwrapper/bios_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func biosConfigFromFixture(t *testing.T) map[string]string {
3131
}
3232

3333
var bios map[string]any
34-
err = json.Unmarshal([]byte(b), &bios)
34+
err = json.Unmarshal(b, &bios)
3535
if err != nil {
3636
t.Fatalf("%s, failed to unmarshal fixture: %s", err.Error(), fixturePath)
3737
}
3838

3939
expectedBiosConfig := make(map[string]string)
40-
for k, v := range bios["Attributes"].(map[string]any) {
40+
for k, v := range bios["Attributes"].(map[string]any) { // nolint:forcetypeassert
4141
expectedBiosConfig[k] = fmt.Sprintf("%v", v)
4242
}
4343

internal/redfishwrapper/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,13 @@ func redfishVersionMeetsOrExceeds(version string, major, minor, patch int) bool
250250
return false
251251
}
252252

253-
var rfVer []int64
254-
for _, part := range parts {
255-
ver, err := strconv.ParseInt(part, 10, 32)
253+
rfVer := make([]int64, 3)
254+
for i := range parts {
255+
ver, err := strconv.ParseInt(parts[i], 10, 32)
256256
if err != nil {
257257
return false
258258
}
259-
rfVer = append(rfVer, ver)
259+
rfVer[i] = ver
260260
}
261261

262262
if rfVer[0] < int64(major) {

internal/redfishwrapper/firmware.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,39 +317,39 @@ func (p pipeReaderFakeSeeker) Seek(offset int64, whence int) (int64, error) {
317317
//
318318
// It creates a temporary form without reading in the update file payload and returns
319319
// sizeOf(form) + sizeOf(update file)
320-
func multipartPayloadSize(payload *multipartPayload) (int64, *bytes.Buffer, error) {
320+
func multipartPayloadSize(payload *multipartPayload) (int64, error) {
321321
body := &bytes.Buffer{}
322322
form := multipart.NewWriter(body)
323323

324324
// Add UpdateParameters field part
325325
part, err := updateParametersFormField("UpdateParameters", form)
326326
if err != nil {
327-
return 0, body, err
327+
return 0, err
328328
}
329329

330330
if _, err = io.Copy(part, bytes.NewReader(payload.updateParameters)); err != nil {
331-
return 0, body, err
331+
return 0, err
332332
}
333333

334334
// Add updateFile form
335335
_, err = form.CreateFormFile("UpdateFile", filepath.Base(payload.updateFile.Name()))
336336
if err != nil {
337-
return 0, body, err
337+
return 0, err
338338
}
339339

340340
// determine update file size
341341
finfo, err := payload.updateFile.Stat()
342342
if err != nil {
343-
return 0, body, err
343+
return 0, err
344344
}
345345

346346
// add terminating boundary to multipart form
347347
err = form.Close()
348348
if err != nil {
349-
return 0, body, err
349+
return 0, err
350350
}
351351

352-
return int64(body.Len()) + finfo.Size(), body, nil
352+
return int64(body.Len()) + finfo.Size(), nil
353353
}
354354

355355
// runRequestWithMultipartPayload is a copy of https://github.com/stmcginnis/gofish/blob/main/client.go#L349
@@ -385,7 +385,7 @@ func (c *Client) runRequestWithMultipartPayload(url string, payload *multipartPa
385385
//
386386
// Without the content-length header the http client will set the Transfer-Encoding to 'chunked'
387387
// and that does not work for some BMCs (iDracs).
388-
contentLength, _, err := multipartPayloadSize(payload)
388+
contentLength, err := multipartPayloadSize(payload)
389389
if err != nil {
390390
return nil, errors.Wrap(err, "error determining multipart payload size")
391391
}

internal/redfishwrapper/firmware_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@ func TestRunRequestWithMultipartPayload(t *testing.T) {
115115
t.Fatal(err)
116116
}
117117

118-
_, err = client.runRequestWithMultipartPayload(tc.updateURI, tc.payload)
118+
resp, err := client.runRequestWithMultipartPayload(tc.updateURI, tc.payload)
119119
if tc.err != nil {
120120
assert.ErrorContains(t, err, tc.err.Error())
121121
return
122122
}
123123

124124
assert.Nil(t, err)
125+
resp.Body.Close()
125126
client.Close(context.Background())
126127
})
127128
}
@@ -392,7 +393,7 @@ func TestMultipartPayloadSize(t *testing.T) {
392393

393394
for _, tc := range testCases {
394395
t.Run(tc.testName, func(t *testing.T) {
395-
gotSize, _, err := multipartPayloadSize(tc.payload)
396+
gotSize, err := multipartPayloadSize(tc.payload)
396397
if tc.errorMsg != "" {
397398
assert.Contains(t, err.Error(), tc.errorMsg)
398399
}

0 commit comments

Comments
 (0)