Skip to content

Commit c62df5c

Browse files
authored
Merge pull request #41 from orangekame3/add-size
[feat]: Add size option
2 parents cb95655 + 73e3490 commit c62df5c

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

33
## [v0.0.16](https://github.com/orangekame3/stree/compare/v0.0.15...v0.0.16) - 2024-07-16
4-
- Add output option by @orangekame3 in https://github.com/orangekame3/stree/pull/39
4+
5+
- Add output option by @orangekame3 in <https://github.com/orangekame3/stree/pull/39>
56

67
## [v0.0.15](https://github.com/orangekame3/stree/compare/v0.0.14...v0.0.15) - 2024-07-08
78

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,19 @@ Usage:
251251

252252
Flags:
253253
-e, --endpoint-url string AWS endpoint URL to use (useful for local testing with LocalStack)
254+
-f, --full-path Print the full path prefix for each file.
254255
-h, --help help for stree
256+
-H, --human-readable Print the size of each file but in a more human readable way, e.g. appending a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), petabytes (P) and exabytes(E).
255257
-L, --level int Descend only level directories
256258
-l, --local Use LocalStack configuration
257259
-m, --mfa Use Multi-Factor Authentication
258260
-n, --no-color Disable colorized output
261+
-o, --output string Send output to filename.
259262
-p, --profile string AWS profile to use (default "default")
260263
-r, --region string AWS region to use (overrides the region specified in the profile)
261-
-f Print the full path prefix for each file.
264+
-s, --size Print the size of each file in bytes along with the name.
265+
-v, --version version for stree
266+
262267
```
263268

264269
# License

cmd/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ var (
4848
level int
4949
fullPath bool
5050
fileName string
51+
size bool
52+
humanReadable bool
5153
)
5254

5355
var rootCmd = &cobra.Command{
@@ -78,7 +80,7 @@ var rootCmd = &cobra.Command{
7880
if level > 0 {
7981
maxDepth = &level
8082
}
81-
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth)
83+
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth,size, humanReadable)
8284
if err != nil {
8385
log.Fatalf("failed to fetch S3 object keys: %v", err)
8486
return
@@ -135,6 +137,8 @@ func init() {
135137
rootCmd.Flags().IntVarP(&level, "level", "L", 0, "Descend only level directories")
136138
rootCmd.Flags().BoolVarP(&fullPath, "full-path", "f", false, "Print the full path prefix for each file.")
137139
rootCmd.Flags().StringVarP(&fileName, "output", "o", "", "Send output to filename.")
140+
rootCmd.Flags().BoolVarP(&size, "size", "s", false, "Print the size of each file in bytes along with the name.")
141+
rootCmd.Flags().BoolVarP(&humanReadable, "human-readable", "H", false, "Print the size of each file but in a more human readable way, e.g. appending a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), petabytes (P) and exabytes(E).")
138142
}
139143

140144
func extractBucketAndPrefix(input string) (string, string, error) {

pkg/s3.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pkg
33

44
import (
55
"context"
6+
"fmt"
67
"strings"
78

89
"github.com/aws/aws-sdk-go-v2/aws"
@@ -70,8 +71,22 @@ func InitializeAWSSession(conf S3Config) (*s3.Client, error) {
7071
return s3.NewFromConfig(awsConfig), nil
7172
}
7273

74+
// ref: https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
75+
func formatBytes(b int64) string {
76+
const unit = 1000
77+
if b < unit {
78+
return fmt.Sprintf("%d", b)
79+
}
80+
div, exp := int64(unit), 0
81+
for n := b / unit; n >= unit; n /= unit {
82+
div *= unit
83+
exp++
84+
}
85+
return fmt.Sprintf("%.1f%c", float64(b)/float64(div), "KMGTPE"[exp])
86+
}
87+
7388
// FetchS3ObjectKeys returns a slice of keys for all objects in the specified bucket and prefix
74-
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int) ([][]string, error) {
89+
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int,size, humanReadable bool) ([][]string, error) {
7590
var delimiter *string
7691
if maxDepth != nil {
7792
delimiter = aws.String("/")
@@ -109,6 +124,11 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
109124

110125
for _, obj := range page.Contents {
111126
key := strings.Split(*obj.Key, "/")
127+
if humanReadable {
128+
key[len(key)-1] = fmt.Sprintf("[%7s] %s", formatBytes(*obj.Size), key[len(key)-1])
129+
} else if size {
130+
key[len(key)-1] = fmt.Sprintf("[%7d] %s", *obj.Size, key[len(key)-1])
131+
}
112132
keys = append(keys, key)
113133
}
114134

test-example.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)