From 2f249e3ddd2c5f66bb5d8db6c53911a069f44f0a Mon Sep 17 00:00:00 2001 From: clawbot Date: Fri, 20 Feb 2026 00:26:03 -0800 Subject: [PATCH] =?UTF-8?q?fix:=20address=20review=20feedback=20=E2=80=94?= =?UTF-8?q?=20use=20helper=20wrappers,=20remove=20duplicates,=20fix=20scan?= =?UTF-8?q?Stdin=20usage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace bare fmt.Scanln with v.scanStdin() helper in snapshot.go - Remove duplicate FetchBlob from vaultik.go (canonical version in blob_fetch_stub.go) - Remove duplicate FetchAndDecryptBlob from restore.go (canonical version in blob_fetch_stub.go) - Rebase onto main, resolve all conflicts - All helper wrappers (printfStdout, printlnStdout, printfStderr, scanStdin) follow YAGNI - No bare fmt.Print*/fmt.Scan* calls remain outside helpers - make test passes: lint clean, all tests pass --- internal/vaultik/restore.go | 47 ------------------------------------ internal/vaultik/snapshot.go | 2 +- internal/vaultik/vaultik.go | 11 --------- 3 files changed, 1 insertion(+), 59 deletions(-) diff --git a/internal/vaultik/restore.go b/internal/vaultik/restore.go index d477844..d5ac3a7 100644 --- a/internal/vaultik/restore.go +++ b/internal/vaultik/restore.go @@ -479,53 +479,6 @@ func (v *Vaultik) restoreRegularFile( return nil } -// BlobFetchResult holds the result of fetching and decrypting a blob. -type BlobFetchResult struct { - Data []byte - CompressedSize int64 -} - -// FetchAndDecryptBlob downloads a blob from storage, decrypts and decompresses it. -func (v *Vaultik) FetchAndDecryptBlob(ctx context.Context, blobHash string, expectedSize int64, identity age.Identity) (*BlobFetchResult, error) { - // Construct blob path with sharding - blobPath := fmt.Sprintf("blobs/%s/%s/%s", blobHash[:2], blobHash[2:4], blobHash) - - reader, err := v.Storage.Get(ctx, blobPath) - if err != nil { - return nil, fmt.Errorf("downloading blob: %w", err) - } - defer func() { _ = reader.Close() }() - - // Read encrypted data - encryptedData, err := io.ReadAll(reader) - if err != nil { - return nil, fmt.Errorf("reading blob data: %w", err) - } - - // Decrypt and decompress - blobReader, err := blobgen.NewReader(bytes.NewReader(encryptedData), identity) - if err != nil { - return nil, fmt.Errorf("creating decryption reader: %w", err) - } - defer func() { _ = blobReader.Close() }() - - data, err := io.ReadAll(blobReader) - if err != nil { - return nil, fmt.Errorf("decrypting blob: %w", err) - } - - log.Debug("Downloaded and decrypted blob", - "hash", blobHash[:16], - "encrypted_size", humanize.Bytes(uint64(len(encryptedData))), - "decrypted_size", humanize.Bytes(uint64(len(data))), - ) - - return &BlobFetchResult{ - Data: data, - CompressedSize: int64(len(encryptedData)), - }, nil -} - // downloadBlob downloads and decrypts a blob func (v *Vaultik) downloadBlob(ctx context.Context, blobHash string, expectedSize int64, identity age.Identity) ([]byte, error) { result, err := v.FetchAndDecryptBlob(ctx, blobHash, expectedSize, identity) diff --git a/internal/vaultik/snapshot.go b/internal/vaultik/snapshot.go index 38269df..2960389 100644 --- a/internal/vaultik/snapshot.go +++ b/internal/vaultik/snapshot.go @@ -545,7 +545,7 @@ func (v *Vaultik) PurgeSnapshots(keepLatest bool, olderThan string, force bool) if !force { v.printfStdout("\nDelete %d snapshot(s)? [y/N] ", len(toDelete)) var confirm string - if _, err := fmt.Scanln(&confirm); err != nil { + if _, err := v.scanStdin(&confirm); err != nil { // Treat EOF or error as "no" v.printlnStdout("Cancelled") return nil diff --git a/internal/vaultik/vaultik.go b/internal/vaultik/vaultik.go index 38374cd..7dce62a 100644 --- a/internal/vaultik/vaultik.go +++ b/internal/vaultik/vaultik.go @@ -149,17 +149,6 @@ func (v *Vaultik) scanStdin(a ...any) (int, error) { return fmt.Fscanln(v.Stdin, a...) } -// FetchBlob downloads a blob from storage and returns a reader for the encrypted data. -func (v *Vaultik) FetchBlob(ctx context.Context, blobHash string, expectedSize int64) (io.ReadCloser, int64, error) { - blobPath := fmt.Sprintf("blobs/%s/%s/%s", blobHash[:2], blobHash[2:4], blobHash) - - reader, err := v.Storage.Get(ctx, blobPath) - if err != nil { - return nil, 0, fmt.Errorf("downloading blob: %w", err) - } - - return reader, expectedSize, nil -} // TestVaultik wraps a Vaultik with captured stdout/stderr for testing type TestVaultik struct { *Vaultik