Skip to content

Commit

Permalink
feat(dump): Add file size to final log
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 1, 2024
1 parent c09c274 commit e72d2a0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
github.com/charmbracelet/huh v0.3.0
github.com/charmbracelet/lipgloss v0.10.0
github.com/dustin/go-humanize v1.0.1
github.com/gabe565/go-spinners v1.0.1
github.com/klauspost/pgzip v1.2.6
github.com/mattn/go-isatty v0.0.20
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/emicklei/go-restful/v3 v3.12.0 h1:y2DdzBAURM29NFF94q6RaY4vjIH1rtwDapwQtU84iWk=
github.com/emicklei/go-restful/v3 v3.12.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
5 changes: 4 additions & 1 deletion internal/actions/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (action Dump) Run(ctx context.Context) error {
pr = gzPipeReader
}

sizeW := &util.SizeWriter{}

// Begin copying export to local file
errGroup.Go(func() error {
defer func(pr io.ReadCloser) {
Expand All @@ -149,7 +151,7 @@ func (action Dump) Run(ctx context.Context) error {
}
}

if _, err := io.Copy(io.MultiWriter(f, bar), r); err != nil {
if _, err := io.Copy(io.MultiWriter(f, bar, sizeW), r); err != nil {
return err
}

Expand All @@ -168,6 +170,7 @@ func (action Dump) Run(ctx context.Context) error {

actionLog.Info().
Stringer("took", time.Since(startTime).Truncate(10*time.Millisecond)).
Stringer("size", sizeW).
Msg("dump complete")

return nil
Expand Down
5 changes: 4 additions & 1 deletion internal/actions/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ func (action Restore) Run(ctx context.Context) error {
return action.runInDatabasePod(ctx, pr, errLog, errLog, action.Format)
})

sizeW := &util.SizeWriter{}

errGroup.Go(func() error {
defer func(pw io.WriteCloser) {
_ = pw.Close()
}(pw)

w := io.MultiWriter(pw, bar)
w := io.MultiWriter(pw, sizeW, bar)

// Clean database
if action.Clean && action.Format != sqlformat.Custom {
Expand Down Expand Up @@ -153,6 +155,7 @@ func (action Restore) Run(ctx context.Context) error {

actionLog.Info().
Dur("took", time.Since(startTime).Truncate(10*time.Millisecond)).
Stringer("size", sizeW).
Msg("restore complete")
return nil
}
Expand Down
20 changes: 20 additions & 0 deletions internal/util/sizewriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

import "github.com/dustin/go-humanize"

type SizeWriter struct {
size uint64
}

func (s *SizeWriter) Write(p []byte) (int, error) {
s.size += uint64(len(p))
return len(p), nil
}

func (s *SizeWriter) Size() uint64 {
return s.size
}

func (s *SizeWriter) String() string {
return humanize.IBytes(s.size)
}

0 comments on commit e72d2a0

Please sign in to comment.