From 7021ac849a7ecc71601e0292028014f981ad8b31 Mon Sep 17 00:00:00 2001 From: MalinAhlberg Date: Wed, 28 Aug 2024 16:57:21 +0200 Subject: [PATCH] [list] Reformat output of dataset files --- go.mod | 1 + go.sum | 2 ++ list/list.go | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 3495611..4fa5ff1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c99a96b..743ac41 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/list/list.go b/list/list.go index c284a76..366bbe8 100644 --- a/list/list.go +++ b/list/list.go @@ -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" ) @@ -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 ] [prefix] (-url --datasets) (-url --dataset ) +USAGE: %s list [--config ] [prefix] (-url --datasets) (-url --dataset ) [--bytes] list: Lists recursively all files under the user's folder in the Sensitive @@ -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 @@ -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) + } // 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 }