diff --git a/pkg/utils/filesize.go b/pkg/utils/filesize.go new file mode 100644 index 0000000..fec6f49 --- /dev/null +++ b/pkg/utils/filesize.go @@ -0,0 +1,16 @@ +package utils + +import "fmt" + +var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB"} + +// ConvertFileSize converts size in bytes down to biggest units it represents. +// Returns converted size, unit and, a concatenation of size and unit +func ConvertFileSize(size int64) (int64, string, string) { + var idx int + + for idx = 0; size >= 1024; size /= 1024 { + } + + return size, sizeSuffixes[idx], fmt.Sprint(size, sizeSuffixes[idx]) +}