From 43b73654c63674ac672bb58d75787c0fc7ce9cb3 Mon Sep 17 00:00:00 2001 From: ostempel Date: Wed, 23 Oct 2024 15:20:58 +0200 Subject: [PATCH] fix error on file reading and db-check --- cmd/internal/encryption/encryption.go | 7 ++----- cmd/internal/encryption/encryption_test.go | 2 +- cmd/internal/initializer/initializer.go | 24 +++++++++++----------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/cmd/internal/encryption/encryption.go b/cmd/internal/encryption/encryption.go index 486c7b6..65b0c85 100644 --- a/cmd/internal/encryption/encryption.go +++ b/cmd/internal/encryption/encryption.go @@ -10,7 +10,6 @@ import ( "log/slog" "os" "path/filepath" - "strconv" "strings" "unicode" @@ -177,8 +176,7 @@ func (e *Encrypter) encryptFile(infile, outfile afero.File, block cipher.Block, break } if err != nil { - e.log.Info("read %d bytes: %s", strconv.Itoa(n), err) - break + return fmt.Errorf("error reading from file (%d bytes read): %w", n, err) } } @@ -232,8 +230,7 @@ func (e *Encrypter) decryptFile(infile, outfile afero.File, block cipher.Block, break } if err != nil { - e.log.Info("read %d bytes: %s", strconv.Itoa(n), err) - break + return fmt.Errorf("error reading from file (%d bytes read): %w", n, err) } } diff --git a/cmd/internal/encryption/encryption_test.go b/cmd/internal/encryption/encryption_test.go index 899ea6f..bbc3823 100644 --- a/cmd/internal/encryption/encryption_test.go +++ b/cmd/internal/encryption/encryption_test.go @@ -16,7 +16,7 @@ func TestEncrypter(t *testing.T) { require.EqualError(t, err, "key length: 11 invalid, must be 32 bytes") // Key too long - _, err = New(slog.Default(), &EncrypterConfig{Key: "tooloooonoooooooooooooooooooooooooooongkey", FS: fs}) + _, err = New(slog.Default(), &EncrypterConfig{Key: "toolooooooooooooooooooooooooooooooooongkey", FS: fs}) require.EqualError(t, err, "key length: 42 invalid, must be 32 bytes") _, err = New(slog.Default(), &EncrypterConfig{Key: "äöüäöüäöüäöüäöüä", FS: fs}) diff --git a/cmd/internal/initializer/initializer.go b/cmd/internal/initializer/initializer.go index ec43167..2f11fbb 100644 --- a/cmd/internal/initializer/initializer.go +++ b/cmd/internal/initializer/initializer.go @@ -149,6 +149,18 @@ func (i *Initializer) initialize(ctx context.Context) error { i.currentStatus.Status = v1.StatusResponse_CHECKING i.currentStatus.Message = "checking database" + needsBackup, err := i.db.Check(ctx) + if err != nil { + return fmt.Errorf("unable to check data of database: %w", err) + } + + if !needsBackup { + i.log.Info("database does not need to be restored") + return nil + } + + i.log.Info("database potentially needs to be restored, looking for backup") + versions, err := i.bp.ListBackups(ctx) if err != nil { return fmt.Errorf("unable retrieve backup versions: %w", err) @@ -166,18 +178,6 @@ func (i *Initializer) initialize(ctx context.Context) error { } } - needsBackup, err := i.db.Check(ctx) - if err != nil { - return fmt.Errorf("unable to check data of database: %w", err) - } - - if !needsBackup { - i.log.Info("database does not need to be restored") - return nil - } - - i.log.Info("database potentially needs to be restored, looking for backup") - err = i.Restore(ctx, latestBackup) if err != nil { return fmt.Errorf("unable to restore database: %w", err)