From 2d37e08743c1547f79fdda30e084501bf0c5e9ee Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Thu, 2 Feb 2023 21:07:25 +0100 Subject: [PATCH] Use go 1.20, join errors using stdlib (#182) * Use go 1.20, join errors using stdlib * Use go 1.20 proper --- Dockerfile | 2 +- cmd/backup/hooks.go | 5 ++--- cmd/backup/notifications.go | 4 ++-- cmd/backup/script.go | 6 +++--- internal/storage/azure/azure.go | 10 +++++----- internal/storage/local/local.go | 4 ++-- internal/storage/s3/s3.go | 3 +-- internal/utilities/util.go | 24 ------------------------ 8 files changed, 16 insertions(+), 42 deletions(-) delete mode 100644 internal/utilities/util.go diff --git a/Dockerfile b/Dockerfile index 990ba86..a5ef6b5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # Copyright 2021 - Offen Authors # SPDX-License-Identifier: MPL-2.0 -FROM golang:1.19-alpine as builder +FROM golang:1.20-alpine as builder WORKDIR /app COPY . . diff --git a/cmd/backup/hooks.go b/cmd/backup/hooks.go index 777f396..24f8042 100644 --- a/cmd/backup/hooks.go +++ b/cmd/backup/hooks.go @@ -4,10 +4,9 @@ package main import ( + "errors" "fmt" "sort" - - "github.com/offen/docker-volume-backup/internal/utilities" ) // hook contains a queued action that can be trigger them when the script @@ -52,7 +51,7 @@ func (s *script) runHooks(err error) error { } } if len(actionErrors) != 0 { - return utilities.Join(actionErrors...) + return errors.Join(actionErrors...) } return nil } diff --git a/cmd/backup/notifications.go b/cmd/backup/notifications.go index ee1b6ea..9739418 100644 --- a/cmd/backup/notifications.go +++ b/cmd/backup/notifications.go @@ -6,13 +6,13 @@ package main import ( "bytes" _ "embed" + "errors" "fmt" "os" "text/template" "time" sTypes "github.com/containrrr/shoutrrr/pkg/types" - "github.com/offen/docker-volume-backup/internal/utilities" ) //go:embed notifications.tmpl @@ -69,7 +69,7 @@ func (s *script) sendNotification(title, body string) error { } } if len(errs) != 0 { - return fmt.Errorf("sendNotification: error sending message: %w", utilities.Join(errs...)) + return fmt.Errorf("sendNotification: error sending message: %w", errors.Join(errs...)) } return nil } diff --git a/cmd/backup/script.go b/cmd/backup/script.go index c4d34ac..6d7a2a8 100644 --- a/cmd/backup/script.go +++ b/cmd/backup/script.go @@ -5,6 +5,7 @@ package main import ( "context" + "errors" "fmt" "io" "io/fs" @@ -20,7 +21,6 @@ import ( "github.com/offen/docker-volume-backup/internal/storage/s3" "github.com/offen/docker-volume-backup/internal/storage/ssh" "github.com/offen/docker-volume-backup/internal/storage/webdav" - "github.com/offen/docker-volume-backup/internal/utilities" "github.com/containrrr/shoutrrr" "github.com/containrrr/shoutrrr/pkg/router" @@ -329,7 +329,7 @@ func (s *script) stopContainers() (func() error, error) { stopError = fmt.Errorf( "stopContainers: %d error(s) stopping containers: %w", len(stopErrors), - utilities.Join(stopErrors...), + errors.Join(stopErrors...), ) } @@ -380,7 +380,7 @@ func (s *script) stopContainers() (func() error, error) { return fmt.Errorf( "stopContainers: %d error(s) restarting containers and services: %w", len(restartErrors), - utilities.Join(restartErrors...), + errors.Join(restartErrors...), ) } s.logger.Infof( diff --git a/internal/storage/azure/azure.go b/internal/storage/azure/azure.go index c6c1d80..4f55fcd 100644 --- a/internal/storage/azure/azure.go +++ b/internal/storage/azure/azure.go @@ -6,6 +6,7 @@ package azure import ( "bytes" "context" + "errors" "fmt" "os" "path/filepath" @@ -18,7 +19,6 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container" "github.com/offen/docker-volume-backup/internal/storage" - "github.com/offen/docker-volume-backup/internal/utilities" ) type azureBlobStorage struct { @@ -135,21 +135,21 @@ func (b *azureBlobStorage) Prune(deadline time.Time, pruningPrefix string) (*sto if err := b.DoPrune(b.Name(), len(matches), int(totalCount), "Azure Blob Storage backup(s)", func() error { wg := sync.WaitGroup{} wg.Add(len(matches)) - var errors []error + var errs []error for _, match := range matches { name := match go func() { _, err := b.client.DeleteBlob(context.Background(), b.containerName, name, nil) if err != nil { - errors = append(errors, err) + errs = append(errs, err) } wg.Done() }() } wg.Wait() - if len(errors) != 0 { - return utilities.Join(errors...) + if len(errs) != 0 { + return errors.Join(errs...) } return nil }); err != nil { diff --git a/internal/storage/local/local.go b/internal/storage/local/local.go index 4edcff8..d749eb6 100644 --- a/internal/storage/local/local.go +++ b/internal/storage/local/local.go @@ -4,6 +4,7 @@ package local import ( + "errors" "fmt" "io" "os" @@ -12,7 +13,6 @@ import ( "time" "github.com/offen/docker-volume-backup/internal/storage" - "github.com/offen/docker-volume-backup/internal/utilities" ) type localStorage struct { @@ -127,7 +127,7 @@ func (b *localStorage) Prune(deadline time.Time, pruningPrefix string) (*storage return fmt.Errorf( "(*localStorage).Prune: %d error(s) deleting local files, starting with: %w", len(removeErrors), - utilities.Join(removeErrors...), + errors.Join(removeErrors...), ) } return nil diff --git a/internal/storage/s3/s3.go b/internal/storage/s3/s3.go index d542d46..f753bd1 100644 --- a/internal/storage/s3/s3.go +++ b/internal/storage/s3/s3.go @@ -15,7 +15,6 @@ import ( "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/offen/docker-volume-backup/internal/storage" - "github.com/offen/docker-volume-backup/internal/utilities" ) type s3Storage struct { @@ -159,7 +158,7 @@ func (b *s3Storage) Prune(deadline time.Time, pruningPrefix string) (*storage.Pr } } if len(removeErrors) != 0 { - return utilities.Join(removeErrors...) + return errors.Join(removeErrors...) } return nil }); err != nil { diff --git a/internal/utilities/util.go b/internal/utilities/util.go deleted file mode 100644 index 17debbe..0000000 --- a/internal/utilities/util.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2022 - Offen Authors -// SPDX-License-Identifier: MPL-2.0 - -package utilities - -import ( - "errors" - "strings" -) - -// Join takes a list of errors and joins them into a single error -func Join(errs ...error) error { - if len(errs) == 1 { - return errs[0] - } - var msgs []string - for _, err := range errs { - if err == nil { - continue - } - msgs = append(msgs, err.Error()) - } - return errors.New("[" + strings.Join(msgs, ", ") + "]") -}