From f224eeb34d99436ee74cf96ab9d7cf83abc52aa0 Mon Sep 17 00:00:00 2001 From: Travis Tomsu Date: Tue, 28 Mar 2023 01:15:00 +0000 Subject: [PATCH] add verbose flag --- cmd/backup.go | 2 ++ internal/backup/backup.go | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/backup.go b/cmd/backup.go index 21f1b1c..3049ec2 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -25,6 +25,8 @@ func init() { _ = viper.BindPFlag("out", backupCmd.Flags().Lookup("out")) backupCmd.Flags().Int("workers", 3, "Concurrent download workers") _ = viper.BindPFlag("workers", backupCmd.Flags().Lookup("workers")) + backupCmd.Flags().Bool("verbose", true, "Emit details of all media items") + _ = viper.BindPFlag("verbose", backupCmd.Flags().Lookup("verbose")) } var backupCmd = &cobra.Command{ diff --git a/internal/backup/backup.go b/internal/backup/backup.go index 7aa11e9..0165071 100644 --- a/internal/backup/backup.go +++ b/internal/backup/backup.go @@ -3,8 +3,6 @@ package backup import ( "context" "fmt" - "github.com/gphotosuploader/googlemirror/api/photoslibrary/v1" - "github.com/pkg/errors" "io" "net/http" "os" @@ -12,6 +10,10 @@ import ( "strings" "sync" "time" + + "github.com/gphotosuploader/googlemirror/api/photoslibrary/v1" + "github.com/pkg/errors" + "github.com/spf13/viper" ) type Session struct { @@ -95,7 +97,9 @@ func (w *worker) start(queue <-chan *photoslibrary.MediaItem) { for { select { case mi := <-queue: - fmt.Printf("Worker %v got %v of size %vw x %vh created at %v\n", w.id, mi.MimeType, mi.MediaMetadata.Width, mi.MediaMetadata.Height, mi.MediaMetadata.CreationTime) + if viper.GetBool("verbose") { + fmt.Printf("Worker %v got %v of size %vw x %vh created at %v\n", w.id, mi.MimeType, mi.MediaMetadata.Width, mi.MediaMetadata.Height, mi.MediaMetadata.CreationTime) + } miw := wrap(mi, w.baseDestDir) err := w.ensureDestExists(miw) if err != nil { @@ -116,7 +120,9 @@ func (w *worker) start(queue <-chan *photoslibrary.MediaItem) { continue } } else { - fmt.Printf("%v already exists\n", miw.shortDestFilepath()) + if viper.GetBool("verbose") { + fmt.Printf("%v already exists\n", miw.shortDestFilepath()) + } } w.wg.Done() case <-w.stop: @@ -163,7 +169,7 @@ func (w *worker) fetchItem(mi *photoslibrary.MediaItem) ([]byte, error) { func (w *worker) writeItem(miw *mediaItemWrapper, data []byte) error { defer func() { - fmt.Printf("Worker %v finished in %v\n", w.id, time.Now().Sub(miw.startTime)) + fmt.Printf("Worker %v finished %v in %v\n", w.id, miw.shortDestFilepath(), time.Since(miw.startTime)) }() if err := os.WriteFile(miw.destFilepath(), data, 0644); err != nil { return errors.Wrapf(err, "writing item %v", miw.src.Id)