Humanize file sizes in gen output, clean up temp on signal

- Humanize file sizes in verbose file listing (e.g., "76.8 MiB" not "76836984 bytes")
- Add signal handler to clean up temp file on Ctrl-C/SIGTERM during gen
This commit is contained in:
Jeffrey Paul 2025-12-17 16:26:32 -08:00
parent 1d37dd9748
commit 07db5d434f
2 changed files with 20 additions and 1 deletions

View File

@ -2,8 +2,11 @@ package cli
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
"github.com/dustin/go-humanize"
@ -84,9 +87,24 @@ func (mfa *CLIApp) generateManifestOperation(ctx *cli.Context) error {
return fmt.Errorf("failed to create temp file: %w", err)
}
// Set up signal handler to clean up temp file on Ctrl-C
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
sig, ok := <-sigChan
if !ok || sig == nil {
return // Channel closed normally, not a signal
}
_ = outFile.Close()
_ = mfa.Fs.Remove(tmpPath)
os.Exit(1)
}()
// Clean up temp file on any error or interruption
success := false
defer func() {
signal.Stop(sigChan)
close(sigChan)
_ = outFile.Close()
if !success {
_ = mfa.Fs.Remove(tmpPath)

View File

@ -10,6 +10,7 @@ import (
"sync"
"time"
"github.com/dustin/go-humanize"
"github.com/spf13/afero"
"sneak.berlin/go/mfer/internal/log"
"sneak.berlin/go/mfer/mfer"
@ -354,7 +355,7 @@ func (s *Scanner) ToManifest(ctx context.Context, w io.Writer, progress chan<- S
return err
}
log.Verbosef("+ %s (%d bytes)", entry.Path, bytesRead)
log.Verbosef("+ %s (%s)", entry.Path, humanize.IBytes(uint64(bytesRead)))
scannedFiles++
scannedBytes += bytesRead