Skip to content

Commit 7921c89

Browse files
authored
Fix linting errors after golangci-lint upgrade (#965)
Signed-off-by: Kimmo Lehto <[email protected]>
1 parent f2f266a commit 7921c89

File tree

15 files changed

+83
-50
lines changed

15 files changed

+83
-50
lines changed

.golangci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
errcheck:
6+
exclude-functions:
7+
- fmt.Fprint
8+
- fmt.Fprintf
9+
- fmt.Fprintln

cmd/apply.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/k0sproject/k0sctl/action"
1010
"github.com/k0sproject/k0sctl/phase"
1111
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
12+
log "github.com/sirupsen/logrus"
1213

1314
"github.com/urfave/cli/v2"
1415
)
@@ -80,7 +81,12 @@ var applyCommand = &cli.Command{
8081
if err != nil {
8182
return fmt.Errorf("failed to open kubeconfig-out file: %w", err)
8283
}
83-
defer out.Close()
84+
outputFile := kc
85+
defer func() {
86+
if err := out.Close(); err != nil {
87+
log.Warnf("failed to close kubeconfig-out file %s: %v", outputFile, err)
88+
}
89+
}()
8490
kubeconfigOut = out
8591
}
8692

cmd/backup.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ var backupCommand = &cli.Command{
6363
resultErr = fmt.Errorf("open local file for writing: %w", err)
6464
return resultErr
6565
}
66-
defer f.Close()
66+
backupFile := f
67+
defer func() {
68+
if err := backupFile.Close(); err != nil {
69+
log.Warnf("failed to close backup file %s: %v", localFile, err)
70+
}
71+
}()
6772
out = f
6873
}
6974

cmd/flags.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ func initConfig(ctx *cli.Context) error {
201201
if err != nil {
202202
return err
203203
}
204-
defer file.Close()
204+
cfgFile := file
205+
cfgName := f
206+
defer func() {
207+
if err := cfgFile.Close(); err != nil {
208+
log.Warnf("failed to close config file %s: %v", cfgName, err)
209+
}
210+
}()
205211

206212
content, err := io.ReadAll(file)
207213
if err != nil {
@@ -399,10 +405,10 @@ func LogFile() (*os.File, error) {
399405

400406
logFile, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_APPEND|os.O_SYNC, 0o600)
401407
if err != nil {
402-
return nil, fmt.Errorf("Failed to open log %s: %s", fn, err.Error())
408+
return nil, fmt.Errorf("failed to open log %s: %s", fn, err.Error())
403409
}
404410

405-
_, _ = fmt.Fprintf(logFile, "time=\"%s\" level=info msg=\"###### New session ######\"\n", time.Now().Format(time.RFC822))
411+
fmt.Fprintf(logFile, "time=\"%s\" level=info msg=\"###### New session ######\"\n", time.Now().Format(time.RFC822))
406412

407413
return logFile, nil
408414
}

cmd/kubeconfig.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var kubeconfigCommand = &cli.Command{
5353
return fmt.Errorf("getting kubeconfig failed - log file saved to %s: %w", ctx.Context.Value(ctxLogFileKey{}).(string), err)
5454
}
5555

56-
_, err := fmt.Fprintf(ctx.App.Writer, "%s\n", kubeconfigAction.Manager.Config.Metadata.Kubeconfig)
57-
return err
56+
fmt.Fprintf(ctx.App.Writer, "%s\n", kubeconfigAction.Manager.Config.Metadata.Kubeconfig)
57+
return nil
5858
},
5959
}

phase/download_binaries.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ func (b binary) downloadTo(path string) error {
147147
if err != nil {
148148
return err
149149
}
150-
defer resp.Body.Close()
150+
respBody := resp.Body
151+
defer func() {
152+
if err := respBody.Close(); err != nil {
153+
log.Warnf("failed to close download response body for %s: %v", b.url(), err)
154+
}
155+
}()
151156

152157
if resp.StatusCode != http.StatusOK {
153158
return fmt.Errorf("failed to get k0s binary (http %d)", resp.StatusCode)
@@ -158,7 +163,7 @@ func (b binary) downloadTo(path string) error {
158163
return err
159164
}
160165

161-
if err = f.Close(); err == nil {
166+
if err = f.Close(); err != nil {
162167
return err
163168
}
164169

phase/gather_k0s_facts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (p *GatherK0sFacts) ShouldRun() bool {
7171

7272
// Run the phase
7373
func (p *GatherK0sFacts) Run(ctx context.Context) error {
74-
var controllers cluster.Hosts = p.hosts.Controllers()
74+
controllers := p.hosts.Controllers()
7575
if err := p.parallelDo(ctx, controllers, p.investigateK0s); err != nil {
7676
return err
7777
}
@@ -86,7 +86,7 @@ func (p *GatherK0sFacts) Run(ctx context.Context) error {
8686
return err
8787
}
8888

89-
var workers cluster.Hosts = p.hosts.Workers()
89+
workers := p.hosts.Workers()
9090
if err := p.parallelDo(ctx, workers, p.investigateK0s); err != nil {
9191
return err
9292
}

phase/get_kubeconfig_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestGetKubeconfig(t *testing.T) {
5656
require.NoError(t, err)
5757
require.Equal(t, "https://10.0.0.1:6443", conf.Clusters["k0s"].Server)
5858

59-
cfg.Spec.Hosts[0].Connection.SSH.Address = "abcd:efgh:ijkl:mnop"
59+
cfg.Spec.Hosts[0].SSH.Address = "abcd:efgh:ijkl:mnop"
6060
p.APIAddress = ""
6161
require.NoError(t, p.Run(context.Background()))
6262
conf, err = clientcmd.Load([]byte(cfg.Metadata.Kubeconfig))

phase/lock.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
gos "os"
87
"sync"
98
"time"
109

@@ -31,7 +30,7 @@ func (p *Lock) Prepare(c *v1beta1.Cluster) error {
3130
if err != nil {
3231
hn = "unknown"
3332
}
34-
p.instanceID = fmt.Sprintf("%s-%d", hn, gos.Getpid())
33+
p.instanceID = fmt.Sprintf("%s-%d", hn, os.Getpid())
3534
return nil
3635
}
3736

phase/manager.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,19 @@ type withmanager interface {
9595
}
9696

9797
type withDryRun interface {
98-
DryRun() error
98+
DryRun() error
9999
}
100100

101101
// In-phase hooks for phases to run logic immediately before/after Run().
102102
// These are strictly internal hooks for phases themselves and are separate
103103
// from user-configured lifecycle hooks handled by the RunHooks phase.
104104
type withBefore interface {
105-
Before() error
105+
Before() error
106106
}
107107
type withAfter interface {
108-
After() error
108+
After() error
109109
}
110110

111-
112-
113111
// Manager executes phases to construct the cluster
114112
type Manager struct {
115113
phases Phases
@@ -247,14 +245,14 @@ func (m *Manager) Run(ctx context.Context) error {
247245
}
248246
}
249247

250-
// Run in-phase before hook if implemented.
251-
if bp, ok := p.(withBefore); ok {
252-
log.Debugf("running before for phase '%s'", p.Title())
253-
if err := bp.Before(); err != nil {
254-
log.Debugf("before failed '%s'", err.Error())
255-
return err
256-
}
257-
}
248+
// Run in-phase before hook if implemented.
249+
if bp, ok := p.(withBefore); ok {
250+
log.Debugf("running before for phase '%s'", p.Title())
251+
if err := bp.Before(); err != nil {
252+
log.Debugf("before failed '%s'", err.Error())
253+
return err
254+
}
255+
}
258256

259257
text := Colorize.Green("==> Running phase: %s").String()
260258
log.Infof(text, title)
@@ -266,24 +264,24 @@ func (m *Manager) Run(ctx context.Context) error {
266264
continue
267265
}
268266

269-
result = p.Run(ctx)
270-
ran = append(ran, p)
271-
272-
// Only run in-phase After hook if Run() succeeded.
273-
// If After() fails after a successful Run(), return the After() error.
274-
if result == nil {
275-
if ap, ok := p.(withAfter); ok {
276-
log.Debugf("running after for phase '%s'", p.Title())
277-
if herr := ap.After(); herr != nil {
278-
return herr
279-
}
280-
}
281-
}
282-
283-
if result != nil {
284-
return result
285-
}
286-
}
267+
result = p.Run(ctx)
268+
ran = append(ran, p)
269+
270+
// Only run in-phase After hook if Run() succeeded.
271+
// If After() fails after a successful Run(), return the After() error.
272+
if result == nil {
273+
if ap, ok := p.(withAfter); ok {
274+
log.Debugf("running after for phase '%s'", p.Title())
275+
if herr := ap.After(); herr != nil {
276+
return herr
277+
}
278+
}
279+
}
280+
281+
if result != nil {
282+
return result
283+
}
284+
}
287285

288286
return nil
289287
}

0 commit comments

Comments
 (0)