Skip to content

Commit 6d7bee0

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

9 files changed

Lines changed: 90 additions & 50 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/config_provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ func source(path string) (map[string]string, error) {
153153
currentValue, currentOk := os.LookupEnv(key)
154154
defer func() {
155155
if currentOk {
156-
os.Setenv(key, currentValue)
156+
_ = os.Setenv(key, currentValue)
157157
return
158158
}
159-
os.Unsetenv(key)
159+
_ = os.Unsetenv(key)
160160
}()
161161
result[key] = value
162-
os.Setenv(key, value)
162+
_ = os.Setenv(key, value)
163163
}
164164
}
165165
return result, nil

cmd/backup/config_provider_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ func TestSource(t *testing.T) {
6060
},
6161
}
6262

63-
os.Setenv("QUX", "yyy")
64-
defer os.Unsetenv("QUX")
63+
_ = os.Setenv("QUX", "yyy")
64+
defer func() {
65+
_ = os.Unsetenv("QUX")
66+
}()
6567

6668
for _, test := range tests {
6769
t.Run(test.name, func(t *testing.T) {

cmd/backup/exec.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ func (s *script) runLabeledCommands(label string) error {
177177
s.logger.Info(fmt.Sprintf("Running %s command %s for container %s", label, cmd, strings.TrimPrefix(c.Names[0], "/")))
178178
stdout, stderr, err := s.exec(c.ID, cmd, user)
179179
if s.c.ExecForwardOutput {
180-
os.Stderr.Write(stderr)
181-
os.Stdout.Write(stdout)
180+
if _, err := os.Stderr.Write(stderr); err != nil {
181+
return errwrap.Wrap(err, "error writing to stderr")
182+
}
183+
if _, err := os.Stdout.Write(stdout); err != nil {
184+
return errwrap.Wrap(err, "error writing to stdout")
185+
}
182186
}
183187
if err != nil {
184188
return errwrap.Wrap(err, "error executing command")

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/dropbox/dropbox.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,32 @@ func (b *dropboxStorage) Name() string {
8787
}
8888

8989
// Copy copies the given file to the WebDav storage backend.
90-
func (b *dropboxStorage) Copy(file string) error {
90+
func (b *dropboxStorage) Copy(file string) (returnErr error) {
9191
_, name := path.Split(file)
9292

9393
folderArg := files.NewCreateFolderArg(b.DestinationPath)
9494
if _, err := b.client.CreateFolderV2(folderArg); err != nil {
9595
switch err := err.(type) {
9696
case files.CreateFolderV2APIError:
9797
if err.EndpointError.Path.Tag != files.WriteErrorConflict {
98-
return errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
98+
returnErr = errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
99+
return
99100
}
100101
b.Log(storage.LogLevelInfo, b.Name(), "Destination path '%s' already exists, no new directory required.", b.DestinationPath)
101102
default:
102-
return errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
103+
returnErr = errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
104+
return
103105
}
104106
}
105107

106108
r, err := os.Open(file)
107109
if err != nil {
108-
return errwrap.Wrap(err, "error opening the file to be uploaded")
110+
returnErr = errwrap.Wrap(err, "error opening the file to be uploaded")
111+
return
109112
}
110-
defer r.Close()
113+
defer func() {
114+
returnErr = r.Close()
115+
}()
111116

112117
// Start new upload session and get session id
113118
b.Log(storage.LogLevelInfo, b.Name(), "Starting upload session for backup '%s' at path '%s'.", file, b.DestinationPath)
@@ -116,7 +121,8 @@ func (b *dropboxStorage) Copy(file string) error {
116121
uploadSessionStartArg := files.NewUploadSessionStartArg()
117122
uploadSessionStartArg.SessionType = &files.UploadSessionType{Tagged: dropbox.Tagged{Tag: files.UploadSessionTypeConcurrent}}
118123
if res, err := b.client.UploadSessionStart(uploadSessionStartArg, nil); err != nil {
119-
return errwrap.Wrap(err, "error starting the upload session")
124+
returnErr = errwrap.Wrap(err, "error starting the upload session")
125+
return
120126
} else {
121127
sessionId = res.SessionId
122128
}
@@ -197,7 +203,8 @@ loop:
197203
files.NewCommitInfo(path.Join(b.DestinationPath, name)),
198204
), nil)
199205
if err != nil {
200-
return errwrap.Wrap(err, "error finishing the upload session")
206+
returnErr = errwrap.Wrap(err, "error finishing the upload session")
207+
return
201208
}
202209

203210
b.Log(storage.LogLevelInfo, b.Name(), "Uploaded a copy of backup '%s' at path '%s'.", file, b.DestinationPath)

internal/storage/googledrive/googledrive.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
"strings"
1212
"time"
1313

14+
"crypto/tls"
1415
"github.com/offen/docker-volume-backup/internal/errwrap"
1516
"github.com/offen/docker-volume-backup/internal/storage"
17+
"golang.org/x/oauth2"
1618
"golang.org/x/oauth2/google"
1719
"google.golang.org/api/drive/v3"
1820
"google.golang.org/api/option"
19-
"golang.org/x/oauth2"
2021
"net/http"
21-
"crypto/tls"
2222
)
2323

2424
type googleDriveStorage struct {
@@ -84,15 +84,18 @@ func (b *googleDriveStorage) Name() string {
8484
}
8585

8686
// Copy copies the given file to the Google Drive storage backend.
87-
func (b *googleDriveStorage) Copy(file string) error {
87+
func (b *googleDriveStorage) Copy(file string) (returnErr error) {
8888
_, name := filepath.Split(file)
8989
b.Log(storage.LogLevelInfo, b.Name(), "Starting upload for backup '%s'.", name)
9090

9191
f, err := os.Open(file)
9292
if err != nil {
93-
return errwrap.Wrap(err, fmt.Sprintf("failed to open file %s", file))
93+
returnErr = errwrap.Wrap(err, fmt.Sprintf("failed to open file %s", file))
94+
return
9495
}
95-
defer f.Close()
96+
defer func() {
97+
returnErr = f.Close()
98+
}()
9699

97100
driveFile := &drive.File{Name: name}
98101
if b.DestinationPath != "" {
@@ -104,7 +107,8 @@ func (b *googleDriveStorage) Copy(file string) error {
104107
createCall := b.client.Files.Create(driveFile).SupportsAllDrives(true).Fields("id")
105108
created, err := createCall.Media(f).Do()
106109
if err != nil {
107-
return errwrap.Wrap(err, fmt.Sprintf("failed to upload %s", name))
110+
returnErr = errwrap.Wrap(err, fmt.Sprintf("failed to upload %s", name))
111+
return
108112
}
109113

110114
b.Log(storage.LogLevelInfo, b.Name(), "Finished upload for %s. File ID: %s", name, created.Id)

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)