Skip to content

Commit

Permalink
add verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ttomsu committed Mar 28, 2023
1 parent caf3c1f commit f224eeb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
16 changes: 11 additions & 5 deletions internal/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package backup
import (
"context"
"fmt"
"github.com/gphotosuploader/googlemirror/api/photoslibrary/v1"
"github.com/pkg/errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/gphotosuploader/googlemirror/api/photoslibrary/v1"
"github.com/pkg/errors"
"github.com/spf13/viper"
)

type Session struct {
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f224eeb

Please sign in to comment.