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
Changes from all commits
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
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