Skip to content

Commit

Permalink
Fix size formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
torrefatto committed Jun 20, 2024
1 parent 78aa518 commit 65151da
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
32 changes: 31 additions & 1 deletion pkg/koyeb/renderer/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,37 @@ func FormatID(fullId string, full bool) string {
return fullId[:8]
}

func FormatSize(size int64) string {
type Size interface {
GetSize() int64
}

type ByteSize int64

func (b ByteSize) GetSize() int64 {
return int64(b)
}

type KBSize int64

func (k KBSize) GetSize() int64 {
return int64(k) * 1024
}

type MBSize int64

func (m MBSize) GetSize() int64 {
return int64(m) * 1024 * 1024
}

type GBSize int64

func (g GBSize) GetSize() int64 {
return int64(g) * 1024 * 1024 * 1024
}

func FormatSize(sized Size) string {
size := sized.GetSize()

switch {
case size > 1024*1024*1024:
return fmt.Sprintf("%.2fG", float64(size)/1024/1024/1024)
Expand Down
2 changes: 1 addition & 1 deletion pkg/koyeb/volumes_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *GetVolumeReply) Fields() []map[string]string {
"region": item.GetRegion(),
"status": formatVolumeStatus(item.GetStatus()),
"type": formatVolumeType(item.GetBackingStore()),
"size": renderer.FormatSize(item.GetMaxSize()),
"size": renderer.FormatSize(renderer.MBSize(item.GetMaxSize())),
"read_only": fmt.Sprintf("%t", item.GetReadOnly()),
"created_at": renderer.FormatTime(item.GetCreatedAt()),
"updated_at": renderer.FormatTime(item.GetUpdatedAt()),
Expand Down
2 changes: 1 addition & 1 deletion pkg/koyeb/volumes_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *ListVolumesReply) Fields() []map[string]string {
"region": item.GetRegion(),
"status": formatVolumeStatus(item.GetStatus()),
"type": formatVolumeType(item.GetBackingStore()),
"size": renderer.FormatSize(item.GetMaxSize()),
"size": renderer.FormatSize(renderer.MBSize(item.GetMaxSize())),
"read_only": fmt.Sprintf("%t", item.GetReadOnly()),
"created_at": renderer.FormatTime(item.GetCreatedAt()),
"updated_at": renderer.FormatTime(item.GetUpdatedAt()),
Expand Down

0 comments on commit 65151da

Please sign in to comment.