فهرست منبع

Improve logging

Neeraj Gupta 1 سال پیش
والد
کامیت
ae1b191765
2فایلهای تغییر یافته به همراه16 افزوده شده و 1 حذف شده
  1. 2 1
      pkg/download.go
  2. 14 0
      utils/time.go

+ 2 - 1
pkg/download.go

@@ -4,6 +4,7 @@ import (
 	"archive/zip"
 	"cli-go/internal/crypto"
 	"cli-go/pkg/model"
+	"cli-go/utils"
 	"cli-go/utils/encoding"
 	"context"
 	"fmt"
@@ -21,7 +22,7 @@ func (c *ClICtrl) downloadAndDecrypt(
 ) (*string, error) {
 	dir := c.tempFolder
 	downloadPath := fmt.Sprintf("%s/%d", dir, file.ID)
-	log.Printf("Downloading file %d to %s", file.ID, downloadPath)
+	log.Printf("Downloading %s (%s)", file.GetTitle(), utils.ByteCountDecimal(file.Info.FileSize))
 	err := c.Client.DownloadFile(ctx, file.ID, downloadPath)
 	if err != nil {
 		return nil, fmt.Errorf("error downloading file %d: %w", file.ID, err)

+ 14 - 0
utils/time.go

@@ -1,6 +1,7 @@
 package utils
 
 import (
+	"fmt"
 	"log"
 	"time"
 )
@@ -9,3 +10,16 @@ func TimeTrack(start time.Time, name string) {
 	elapsed := time.Since(start)
 	log.Printf("%s took %s", name, elapsed)
 }
+
+func ByteCountDecimal(b int64) string {
+	const unit = 1000
+	if b < unit {
+		return fmt.Sprintf("%d B", b)
+	}
+	div, exp := int64(unit), 0
+	for n := b / unit; n >= unit; n /= unit {
+		div *= unit
+		exp++
+	}
+	return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
+}