Skip to content

Commit

Permalink
[list] Reformat output of dataset files
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinAhlberg committed Aug 29, 2024
1 parent 1ffc0ad commit 7021ac8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a h1:saTgr5tMLFnmy/yg3qDTft4rE5DY2uJ/cCxCe3q0XTU=
github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a/go.mod h1:Bw9BbhOJVNR+t0jCqx2GC6zv0TGBsShs56Y3gfSCvl0=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/inhies/go-bytesize v0.0.0-20210819104631-275770b98743 h1:X3Xxno5Ji8idrNiUoFc7QyXpqhSYlDRYQmc7mlpMBzU=
Expand Down
19 changes: 17 additions & 2 deletions list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package list
import (
"flag"
"fmt"
"strconv"

"strings"

"github.com/NBISweden/sda-cli/download"
"github.com/NBISweden/sda-cli/helpers"
"github.com/dustin/go-humanize"
"github.com/inhies/go-bytesize"
)

Expand All @@ -16,7 +18,7 @@ import (
// Usage text that will be displayed as command line help text when using the
// `help list` command
var Usage = `
USAGE: %s list [-config <s3config-file>] [prefix] (-url <uri> --datasets) (-url <uri> --dataset <dataset-id>)
USAGE: %s list [--config <s3config-file>] [prefix] (-url <uri> --datasets) (-url <uri> --dataset <dataset-id>) [--bytes]
list:
Lists recursively all files under the user's folder in the Sensitive
Expand Down Expand Up @@ -45,6 +47,8 @@ var URL = Args.String("url", "", "The url of the sda-download server")

var datasets = Args.Bool("datasets", false, "List all datasets in the user's folder.")

var bytesFormat = Args.Bool("bytes", false, "Print file sizes in bytes (not human-readable format).")

var dataset = Args.String("dataset", "", "List all files in the specified dataset.")

// List function lists the contents of an s3
Expand Down Expand Up @@ -108,10 +112,21 @@ func DatasetFiles(token string) error {
if err != nil {
return err
}
formatedBytes := func(size int) string {
if !*bytesFormat {
return humanize.Bytes(uint64(size))
}
return strconv.Itoa(size)

Check failure on line 119 in list/list.go

View workflow job for this annotation

GitHub Actions / Lint code (1.22)

return with no blank line before (nlreturn)
}
// Loop through the files and list them
fileIDWidth, sizeWidth := 20, 10
fmt.Printf("%-*s \t %-*s \t %s\n", fileIDWidth, "FileID", sizeWidth, "Size", "Path")
datasetSize := 0
for _, file := range files {
fmt.Printf("%s \t %d \n", file.DisplayFileName, file.DecryptedFileSize)
datasetSize += file.DecryptedFileSize
fmt.Printf("%s \t %s \t %s\n", file.FileID, formatedBytes(file.DecryptedFileSize), file.FilePath)
}
fmt.Printf("Dataset size: %s\n", formatedBytes(datasetSize))

return nil
}
Expand Down

0 comments on commit 7021ac8

Please sign in to comment.