mirror of
https://github.com/offen/docker-volume-backup.git
synced 2026-04-19 15:22:40 +02:00
Update golangci integration (#637)
* Update golangci integration * Fix newly discovered errcheck complaints * Increase timeout value
This commit is contained in:
@@ -87,7 +87,7 @@ func (b *dropboxStorage) Name() string {
|
||||
}
|
||||
|
||||
// Copy copies the given file to the WebDav storage backend.
|
||||
func (b *dropboxStorage) Copy(file string) error {
|
||||
func (b *dropboxStorage) Copy(file string) (returnErr error) {
|
||||
_, name := path.Split(file)
|
||||
|
||||
folderArg := files.NewCreateFolderArg(b.DestinationPath)
|
||||
@@ -95,19 +95,24 @@ func (b *dropboxStorage) Copy(file string) error {
|
||||
switch err := err.(type) {
|
||||
case files.CreateFolderV2APIError:
|
||||
if err.EndpointError.Path.Tag != files.WriteErrorConflict {
|
||||
return errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
|
||||
returnErr = errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
|
||||
return
|
||||
}
|
||||
b.Log(storage.LogLevelInfo, b.Name(), "Destination path '%s' already exists, no new directory required.", b.DestinationPath)
|
||||
default:
|
||||
return errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
|
||||
returnErr = errwrap.Wrap(err, fmt.Sprintf("error creating directory '%s'", b.DestinationPath))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
r, err := os.Open(file)
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error opening the file to be uploaded")
|
||||
returnErr = errwrap.Wrap(err, "error opening the file to be uploaded")
|
||||
return
|
||||
}
|
||||
defer r.Close()
|
||||
defer func() {
|
||||
returnErr = r.Close()
|
||||
}()
|
||||
|
||||
// Start new upload session and get session id
|
||||
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 {
|
||||
uploadSessionStartArg := files.NewUploadSessionStartArg()
|
||||
uploadSessionStartArg.SessionType = &files.UploadSessionType{Tagged: dropbox.Tagged{Tag: files.UploadSessionTypeConcurrent}}
|
||||
if res, err := b.client.UploadSessionStart(uploadSessionStartArg, nil); err != nil {
|
||||
return errwrap.Wrap(err, "error starting the upload session")
|
||||
returnErr = errwrap.Wrap(err, "error starting the upload session")
|
||||
return
|
||||
} else {
|
||||
sessionId = res.SessionId
|
||||
}
|
||||
@@ -197,7 +203,8 @@ loop:
|
||||
files.NewCommitInfo(path.Join(b.DestinationPath, name)),
|
||||
), nil)
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error finishing the upload session")
|
||||
returnErr = errwrap.Wrap(err, "error finishing the upload session")
|
||||
return
|
||||
}
|
||||
|
||||
b.Log(storage.LogLevelInfo, b.Name(), "Uploaded a copy of backup '%s' at path '%s'.", file, b.DestinationPath)
|
||||
|
||||
@@ -11,14 +11,14 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"crypto/tls"
|
||||
"github.com/offen/docker-volume-backup/internal/errwrap"
|
||||
"github.com/offen/docker-volume-backup/internal/storage"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
"google.golang.org/api/drive/v3"
|
||||
"google.golang.org/api/option"
|
||||
"golang.org/x/oauth2"
|
||||
"net/http"
|
||||
"crypto/tls"
|
||||
)
|
||||
|
||||
type googleDriveStorage struct {
|
||||
@@ -84,15 +84,18 @@ func (b *googleDriveStorage) Name() string {
|
||||
}
|
||||
|
||||
// Copy copies the given file to the Google Drive storage backend.
|
||||
func (b *googleDriveStorage) Copy(file string) error {
|
||||
func (b *googleDriveStorage) Copy(file string) (returnErr error) {
|
||||
_, name := filepath.Split(file)
|
||||
b.Log(storage.LogLevelInfo, b.Name(), "Starting upload for backup '%s'.", name)
|
||||
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, fmt.Sprintf("failed to open file %s", file))
|
||||
returnErr = errwrap.Wrap(err, fmt.Sprintf("failed to open file %s", file))
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
defer func() {
|
||||
returnErr = f.Close()
|
||||
}()
|
||||
|
||||
driveFile := &drive.File{Name: name}
|
||||
if b.DestinationPath != "" {
|
||||
@@ -104,7 +107,8 @@ func (b *googleDriveStorage) Copy(file string) error {
|
||||
createCall := b.client.Files.Create(driveFile).SupportsAllDrives(true).Fields("id")
|
||||
created, err := createCall.Media(f).Do()
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, fmt.Sprintf("failed to upload %s", name))
|
||||
returnErr = errwrap.Wrap(err, fmt.Sprintf("failed to upload %s", name))
|
||||
return
|
||||
}
|
||||
|
||||
b.Log(storage.LogLevelInfo, b.Name(), "Finished upload for %s. File ID: %s", name, created.Id)
|
||||
|
||||
@@ -55,7 +55,9 @@ func (b *localStorage) Copy(file string) error {
|
||||
if b.latestSymlink != "" {
|
||||
symlink := path.Join(b.DestinationPath, b.latestSymlink)
|
||||
if _, err := os.Lstat(symlink); err == nil {
|
||||
os.Remove(symlink)
|
||||
if err := os.Remove(symlink); err != nil {
|
||||
return errwrap.Wrap(err, "error removing existing symlink")
|
||||
}
|
||||
}
|
||||
if err := os.Symlink(name, symlink); err != nil {
|
||||
return errwrap.Wrap(err, "error creating latest symlink")
|
||||
@@ -146,22 +148,25 @@ func (b *localStorage) Prune(deadline time.Time, pruningPrefix string) (*storage
|
||||
}
|
||||
|
||||
// copy creates a copy of the file located at `dst` at `src`.
|
||||
func copyFile(src, dst string) error {
|
||||
func copyFile(src, dst string) (returnErr error) {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
returnErr = err
|
||||
return
|
||||
}
|
||||
defer in.Close()
|
||||
defer func() {
|
||||
returnErr = in.Close()
|
||||
}()
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
returnErr = err
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(out, in)
|
||||
if err != nil {
|
||||
out.Close()
|
||||
return err
|
||||
return errors.Join(err, out.Close())
|
||||
}
|
||||
return out.Close()
|
||||
}
|
||||
|
||||
@@ -106,19 +106,25 @@ func (b *sshStorage) Name() string {
|
||||
}
|
||||
|
||||
// Copy copies the given file to the SSH storage backend.
|
||||
func (b *sshStorage) Copy(file string) error {
|
||||
func (b *sshStorage) Copy(file string) (returnErr error) {
|
||||
source, err := os.Open(file)
|
||||
_, name := path.Split(file)
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, " error reading the file to be uploaded")
|
||||
returnErr = errwrap.Wrap(err, " error reading the file to be uploaded")
|
||||
return
|
||||
}
|
||||
defer source.Close()
|
||||
defer func() {
|
||||
returnErr = source.Close()
|
||||
}()
|
||||
|
||||
destination, err := b.sftpClient.Create(path.Join(b.DestinationPath, name))
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error creating file")
|
||||
returnErr = errwrap.Wrap(err, "error creating file")
|
||||
return
|
||||
}
|
||||
defer destination.Close()
|
||||
defer func() {
|
||||
returnErr = destination.Close()
|
||||
}()
|
||||
|
||||
chunk := make([]byte, 1e9)
|
||||
for {
|
||||
@@ -126,27 +132,32 @@ func (b *sshStorage) Copy(file string) error {
|
||||
if err == io.EOF {
|
||||
tot, err := destination.Write(chunk[:num])
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error uploading the file")
|
||||
returnErr = errwrap.Wrap(err, "error uploading the file")
|
||||
return
|
||||
}
|
||||
|
||||
if tot != len(chunk[:num]) {
|
||||
return errwrap.Wrap(nil, "failed to write stream")
|
||||
returnErr = errwrap.Wrap(nil, "failed to write stream")
|
||||
return
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error uploading the file")
|
||||
returnErr = errwrap.Wrap(err, "error uploading the file")
|
||||
return
|
||||
}
|
||||
|
||||
tot, err := destination.Write(chunk[:num])
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error uploading the file")
|
||||
returnErr = errwrap.Wrap(err, "error uploading the file")
|
||||
return
|
||||
}
|
||||
|
||||
if tot != len(chunk[:num]) {
|
||||
return errwrap.Wrap(nil, "failed to write stream")
|
||||
returnErr = errwrap.Wrap(nil, "failed to write stream")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user