Skip to content

Commit e7ba504

Browse files
committed
Fix newly discovered errcheck complaints
1 parent be1145d commit e7ba504

4 files changed

Lines changed: 53 additions & 30 deletions

File tree

cmd/backup/archive.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ func getCompressionWriter(file *os.File, algo string, concurrency int) (io.Write
121121
}
122122
}
123123

124-
func writeTarball(path string, tarWriter *tar.Writer, prefix string) error {
124+
func writeTarball(path string, tarWriter *tar.Writer, prefix string) (returnErr error) {
125125
fileInfo, err := os.Lstat(path)
126126
if err != nil {
127-
return errwrap.Wrap(err, fmt.Sprintf("error getting file info for %s", path))
127+
returnErr = errwrap.Wrap(err, fmt.Sprintf("error getting file info for %s", path))
128+
return
128129
}
129130

130131
if fileInfo.Mode()&os.ModeSocket == os.ModeSocket {
@@ -135,19 +136,22 @@ func writeTarball(path string, tarWriter *tar.Writer, prefix string) error {
135136
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
136137
var err error
137138
if link, err = os.Readlink(path); err != nil {
138-
return errwrap.Wrap(err, fmt.Sprintf("error resolving symlink %s", path))
139+
returnErr = errwrap.Wrap(err, fmt.Sprintf("error resolving symlink %s", path))
140+
return
139141
}
140142
}
141143

142144
header, err := tar.FileInfoHeader(fileInfo, link)
143145
if err != nil {
144-
return errwrap.Wrap(err, "error getting file info header")
146+
returnErr = errwrap.Wrap(err, "error getting file info header")
147+
return
145148
}
146149
header.Name = strings.TrimPrefix(path, prefix)
147150

148151
err = tarWriter.WriteHeader(header)
149152
if err != nil {
150-
return errwrap.Wrap(err, "error writing file info header")
153+
returnErr = errwrap.Wrap(err, "error writing file info header")
154+
return
151155
}
152156

153157
if !fileInfo.Mode().IsRegular() {
@@ -156,13 +160,17 @@ func writeTarball(path string, tarWriter *tar.Writer, prefix string) error {
156160

157161
file, err := os.Open(path)
158162
if err != nil {
159-
return errwrap.Wrap(err, fmt.Sprintf("error opening %s", path))
163+
returnErr = errwrap.Wrap(err, fmt.Sprintf("error opening %s", path))
164+
return
160165
}
161-
defer file.Close()
166+
defer func() {
167+
returnErr = file.Close()
168+
}()
162169

163170
_, err = io.Copy(tarWriter, file)
164171
if err != nil {
165-
return errwrap.Wrap(err, fmt.Sprintf("error copying %s to tar writer", path))
172+
returnErr = errwrap.Wrap(err, fmt.Sprintf("error copying %s to tar writer", path))
173+
return
166174
}
167175

168176
return nil

cmd/backup/stop_restart.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"time"
1414

1515
"github.com/docker/cli/cli/command/service/progress"
16-
"github.com/docker/docker/api/types/container"
1716
ctr "github.com/docker/docker/api/types/container"
1817
"github.com/docker/docker/api/types/filters"
1918
"github.com/docker/docker/api/types/swarm"
@@ -66,7 +65,7 @@ func awaitContainerCountForService(cli *client.Client, serviceID string, count i
6665
),
6766
)
6867
case <-poll.C:
69-
containers, err := cli.ContainerList(context.Background(), container.ListOptions{
68+
containers, err := cli.ContainerList(context.Background(), ctr.ListOptions{
7069
Filters: filters.NewArgs(filters.KeyValuePair{
7170
Key: "label",
7271
Value: fmt.Sprintf("com.docker.swarm.service.id=%s", serviceID),
@@ -124,11 +123,11 @@ func (s *script) stopContainersAndServices() (func() error, error) {
124123
labelValue,
125124
)
126125

127-
allContainers, err := s.cli.ContainerList(context.Background(), container.ListOptions{})
126+
allContainers, err := s.cli.ContainerList(context.Background(), ctr.ListOptions{})
128127
if err != nil {
129128
return noop, errwrap.Wrap(err, "error querying for containers")
130129
}
131-
containersToStop, err := s.cli.ContainerList(context.Background(), container.ListOptions{
130+
containersToStop, err := s.cli.ContainerList(context.Background(), ctr.ListOptions{
132131
Filters: filters.NewArgs(filters.KeyValuePair{
133132
Key: "label",
134133
Value: filterMatchLabel,
@@ -215,7 +214,7 @@ func (s *script) stopContainersAndServices() (func() error, error) {
215214
)
216215
}
217216

218-
var stoppedContainers []container.Summary
217+
var stoppedContainers []ctr.Summary
219218
var stopErrors []error
220219
for _, container := range containersToStop {
221220
if err := s.cli.ContainerStop(context.Background(), container.ID, ctr.StopOptions{}); err != nil {

internal/storage/local/local.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func (b *localStorage) Copy(file string) error {
5555
if b.latestSymlink != "" {
5656
symlink := path.Join(b.DestinationPath, b.latestSymlink)
5757
if _, err := os.Lstat(symlink); err == nil {
58-
os.Remove(symlink)
58+
if err := os.Remove(symlink); err != nil {
59+
return errwrap.Wrap(err, "error removing existing symlink")
60+
}
5961
}
6062
if err := os.Symlink(name, symlink); err != nil {
6163
return errwrap.Wrap(err, "error creating latest symlink")
@@ -146,22 +148,25 @@ func (b *localStorage) Prune(deadline time.Time, pruningPrefix string) (*storage
146148
}
147149

148150
// copy creates a copy of the file located at `dst` at `src`.
149-
func copyFile(src, dst string) error {
151+
func copyFile(src, dst string) (returnErr error) {
150152
in, err := os.Open(src)
151153
if err != nil {
152-
return err
154+
returnErr = err
155+
return
153156
}
154-
defer in.Close()
157+
defer func() {
158+
returnErr = in.Close()
159+
}()
155160

156161
out, err := os.Create(dst)
157162
if err != nil {
158-
return err
163+
returnErr = err
164+
return
159165
}
160166

161167
_, err = io.Copy(out, in)
162168
if err != nil {
163-
out.Close()
164-
return err
169+
return errors.Join(err, out.Close())
165170
}
166171
return out.Close()
167172
}

internal/storage/ssh/ssh.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,47 +106,58 @@ func (b *sshStorage) Name() string {
106106
}
107107

108108
// Copy copies the given file to the SSH storage backend.
109-
func (b *sshStorage) Copy(file string) error {
109+
func (b *sshStorage) Copy(file string) (returnErr error) {
110110
source, err := os.Open(file)
111111
_, name := path.Split(file)
112112
if err != nil {
113-
return errwrap.Wrap(err, " error reading the file to be uploaded")
113+
returnErr = errwrap.Wrap(err, " error reading the file to be uploaded")
114+
return
114115
}
115-
defer source.Close()
116+
defer func() {
117+
returnErr = source.Close()
118+
}()
116119

117120
destination, err := b.sftpClient.Create(path.Join(b.DestinationPath, name))
118121
if err != nil {
119-
return errwrap.Wrap(err, "error creating file")
122+
returnErr = errwrap.Wrap(err, "error creating file")
123+
return
120124
}
121-
defer destination.Close()
125+
defer func() {
126+
returnErr = destination.Close()
127+
}()
122128

123129
chunk := make([]byte, 1e9)
124130
for {
125131
num, err := source.Read(chunk)
126132
if err == io.EOF {
127133
tot, err := destination.Write(chunk[:num])
128134
if err != nil {
129-
return errwrap.Wrap(err, "error uploading the file")
135+
returnErr = errwrap.Wrap(err, "error uploading the file")
136+
return
130137
}
131138

132139
if tot != len(chunk[:num]) {
133-
return errwrap.Wrap(nil, "failed to write stream")
140+
returnErr = errwrap.Wrap(nil, "failed to write stream")
141+
return
134142
}
135143

136144
break
137145
}
138146

139147
if err != nil {
140-
return errwrap.Wrap(err, "error uploading the file")
148+
returnErr = errwrap.Wrap(err, "error uploading the file")
149+
return
141150
}
142151

143152
tot, err := destination.Write(chunk[:num])
144153
if err != nil {
145-
return errwrap.Wrap(err, "error uploading the file")
154+
returnErr = errwrap.Wrap(err, "error uploading the file")
155+
return
146156
}
147157

148158
if tot != len(chunk[:num]) {
149-
return errwrap.Wrap(nil, "failed to write stream")
159+
returnErr = errwrap.Wrap(nil, "failed to write stream")
160+
return
150161
}
151162
}
152163

0 commit comments

Comments
 (0)