Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add summarize support in ls #93

Merged
merged 3 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions cmd/byctl/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

const (
lsFlagLongName = "l"
lsFlagFormat = "format"
lsFlagLongName = "l"
lsFlagFormat = "format"
lsFlagSummarize = "summarize"
)

var lsFlags = []cli.Flag{
Expand All @@ -27,6 +28,10 @@ var lsFlags = []cli.Flag{
Name: lsFlagFormat,
Usage: "across long -l",
},
&cli.BoolFlag{
Name: lsFlagSummarize,
Usage: "display summary information",
},
}

var lsCmd = &cli.Command{
Expand Down Expand Up @@ -70,6 +75,8 @@ var lsCmd = &cli.Command{
}

isFirst := true
var totalNum int
var totalSize int64

for v := range ch {
if v.Error != nil {
Expand All @@ -84,9 +91,18 @@ var lsCmd = &cli.Command{
if isFirst {
isFirst = false
}

totalNum += 1
totalSize += oa.size
}
// End of line
fmt.Print("\n")

// display summary information
if c.Bool(lsFlagSummarize) {
fmt.Printf("\n%14s %d\n", "Total Objects:", totalNum)
fmt.Printf("%14s %s\n", "Total Size:", ByteSize(uint64(totalSize)))
}
return
},
}
Expand Down Expand Up @@ -118,7 +134,7 @@ func (oa objectAttr) shortFormat(isFirst bool) string {
if isFirst {
return oa.name
}
return oa.name + " "
return " " + oa.name
}

func (oa objectAttr) longFormat(isFirst bool) string {
Expand Down
55 changes: 55 additions & 0 deletions cmd/byctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/Xuanwo/go-bufferpool"
Expand Down Expand Up @@ -50,3 +52,56 @@ func parseLimit(text string) (types.Pair, error) {
}
}), nil
}

const (
BYTE = 1 << (10 * iota)
KILOBYTE
MEGABYTE
GIGABYTE
TERABYTE
PETABYTE
EXABYTE
)

// ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth. The following units are available:
// EiB: Exabyte
// PiB: Petabyte
// TiB: Terabyte
// GiB: Gigabyte
// MiB: Megabyte
// KiB: Kilobyte
// B: Byte
// The unit that results in the smallest number greater than or equal to 1 is always chosen.
func ByteSize(bytes uint64) string {
JinnyYi marked this conversation as resolved.
Show resolved Hide resolved
unit := ""
value := float64(bytes)

switch {
case bytes >= EXABYTE:
unit = "EiB"
value = value / EXABYTE
case bytes >= PETABYTE:
unit = "PiB"
value = value / PETABYTE
case bytes >= TERABYTE:
unit = "TiB"
value = value / TERABYTE
case bytes >= GIGABYTE:
unit = "GiB"
value = value / GIGABYTE
case bytes >= MEGABYTE:
unit = "MiB"
value = value / MEGABYTE
case bytes >= KILOBYTE:
unit = "KiB"
value = value / KILOBYTE
case bytes >= BYTE:
unit = "B"
case bytes == 0:
return "0"
}

result := strconv.FormatFloat(value, 'f', 1, 64)
result = strings.TrimSuffix(result, ".0")
return result + " " + unit
}