Skip to content

Commit 3b9e5fe

Browse files
committed
misc golangci-lint fixes
1 parent e303a2b commit 3b9e5fe

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

.golangci.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ linters-settings:
1010
mnd:
1111
ignored-functions:
1212
- strings.SplitN
13+
- os.WriteFile # perms is ok as numbers
1314
depguard:
1415
rules:
1516
main:
@@ -62,9 +63,12 @@ issues:
6263
- text: "ifElseChain: rewrite if-else to switch statement"
6364
linters:
6465
- gocritic
65-
- text: "should check returned error before deferring file.Close()"
66+
- text: "should check error returned from os.Open() before deferring file.Close()"
6667
linters:
6768
- staticcheck
6869
- path: '(.+)_test\.go'
6970
linters:
7071
- lll
72+
- text: "d.GetOkExists is deprecated"
73+
linters:
74+
- staticcheck

libvirt/domain.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const (
2525
domainStateConfLeaseDone = resourceStateConfDone
2626
)
2727

28-
func domainLeaseStateRefreshFunc(ctx context.Context, virConn *libvirt.Libvirt, domain libvirt.Domain, waitForLeases []*libvirtxml.DomainInterface, rd *schema.ResourceData) retry.StateRefreshFunc {
28+
func domainLeaseStateRefreshFunc(_ context.Context,
29+
virConn *libvirt.Libvirt, domain libvirt.Domain,
30+
waitForLeases []*libvirtxml.DomainInterface, rd *schema.ResourceData) retry.StateRefreshFunc {
2931
return func() (interface{}, string, error) {
3032
state, err := domainGetState(virConn, domain)
3133
if err != nil {
@@ -63,7 +65,9 @@ func domainLeaseStateRefreshFunc(ctx context.Context, virConn *libvirt.Libvirt,
6365
}
6466
}
6567

66-
func waitForStateDomainLeaseDone(ctx context.Context, virConn *libvirt.Libvirt, domain libvirt.Domain, waitForLeases []*libvirtxml.DomainInterface, rd *schema.ResourceData,
68+
func waitForStateDomainLeaseDone(ctx context.Context,
69+
virConn *libvirt.Libvirt, domain libvirt.Domain,
70+
waitForLeases []*libvirtxml.DomainInterface, rd *schema.ResourceData,
6771
) error {
6872
stateConf := &retry.StateChangeConf{
6973
Pending: []string{domainStateConfLeaseWaiting},
@@ -85,7 +89,7 @@ func waitForStateDomainLeaseDone(ctx context.Context, virConn *libvirt.Libvirt,
8589
"IMPORTANT: This error is not a terraform libvirt-provider" +
8690
" error, but an error caused by your KVM/libvirt" +
8791
" infrastructure configuration/setup"
88-
return fmt.Errorf("couldn't retrieve IP address of domain id: %s. %s \n %s", rd.Id(), ipNotFoundMsg, err)
92+
return fmt.Errorf("couldn't retrieve IP address of domain id: %s. %s \n %w", rd.Id(), ipNotFoundMsg, err)
8993
}
9094
log.Print("[DEBUG] got lease")
9195
return nil

libvirt/resource_libvirt_pool_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func testAccCheckLibvirtPoolExists(name string, pool *libvirt.StoragePool) resource.TestCheckFunc {
@@ -180,17 +181,14 @@ func TestAccLibvirtPool_ManuallyDestroyed(t *testing.T) {
180181
// delete the pool out of band (from terraform)
181182
client := testAccProvider.Meta().(*Client)
182183

183-
if err := client.libvirt.StoragePoolDestroy(pool); err != nil {
184-
t.Errorf(err.Error())
185-
}
184+
err := client.libvirt.StoragePoolDestroy(pool)
185+
require.NoError(t, err)
186186

187-
if err := client.libvirt.StoragePoolDelete(pool, libvirt.StoragePoolDeleteNormal); err != nil {
188-
t.Errorf(err.Error())
189-
}
187+
err = client.libvirt.StoragePoolDelete(pool, libvirt.StoragePoolDeleteNormal)
188+
require.NoError(t, err)
190189

191-
if err := client.libvirt.StoragePoolUndefine(pool); err != nil {
192-
t.Errorf(err.Error())
193-
}
190+
err = client.libvirt.StoragePoolUndefine(pool)
191+
require.NoError(t, err)
194192
},
195193
},
196194
},

libvirt/volume_image.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ func (i *localImage) Size() (uint64, error) {
4949
//nolint:mnd
5050
func (i *localImage) IsQCOW2() (bool, error) {
5151
file, err := os.Open(i.path)
52-
defer file.Close()
5352
if err != nil {
5453
return false, fmt.Errorf("error while opening %s: %w", i.path, err)
5554
}
55+
defer file.Close()
56+
5657
buf := make([]byte, 8)
5758
_, err = io.ReadAtLeast(file, buf, 8)
5859
if err != nil {
@@ -63,10 +64,11 @@ func (i *localImage) IsQCOW2() (bool, error) {
6364

6465
func (i *localImage) Import(uploader func(io.Reader) error, vol libvirtxml.StorageVolume) error {
6566
file, err := os.Open(i.path)
66-
defer file.Close()
6767
if err != nil {
6868
return fmt.Errorf("error while opening %s: %w", i.path, err)
6969
}
70+
defer file.Close()
71+
7072

7173
fi, err := file.Stat()
7274
if err != nil {

0 commit comments

Comments
 (0)