Skip to content

Commit

Permalink
feat: Add summarize support in ls (#93)
Browse files Browse the repository at this point in the history
* feat: Add summarize support in ls

* Use go-unit to format output
  • Loading branch information
JinnyYi authored Nov 4, 2021
1 parent 8b66312 commit 6332ba4
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cmd/byctl/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"time"

"github.com/docker/go-units"
"github.com/urfave/cli/v2"
"go.uber.org/zap"

Expand All @@ -14,8 +15,9 @@ import (
)

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

var lsFlags = []cli.Flag{
Expand All @@ -27,6 +29,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 +76,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 +92,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:", units.BytesSize(float64(totalSize)))
}
return
},
}
Expand Down Expand Up @@ -118,7 +135,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

0 comments on commit 6332ba4

Please sign in to comment.