Skip to content

Commit 104b284

Browse files
authored
Merge pull request #43 from orangekame3/add-datetime
[feat]: Add datetime
2 parents da00b8d + 9fb46f1 commit 104b284

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ Usage:
250250
stree [bucket/prefix] [flags]
251251

252252
Flags:
253+
-D, --date-time Print the last modified time of each file.
253254
-e, --endpoint-url string AWS endpoint URL to use (useful for local testing with LocalStack)
254255
-f, --full-path Print the full path prefix for each file.
255256
-h, --help help for stree
@@ -262,7 +263,7 @@ Flags:
262263
-p, --profile string AWS profile to use (default "default")
263264
-r, --region string AWS region to use (overrides the region specified in the profile)
264265
-s, --size Print the size of each file in bytes along with the name.
265-
-v, --version version for stree
266+
266267

267268
```
268269

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var (
5050
fileName string
5151
size bool
5252
humanReadable bool
53+
dateTime bool
5354
)
5455

5556
var rootCmd = &cobra.Command{
@@ -80,7 +81,7 @@ var rootCmd = &cobra.Command{
8081
if level > 0 {
8182
maxDepth = &level
8283
}
83-
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth,size, humanReadable)
84+
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth,size, humanReadable,dateTime)
8485
if err != nil {
8586
log.Fatalf("failed to fetch S3 object keys: %v", err)
8687
return
@@ -139,6 +140,7 @@ func init() {
139140
rootCmd.Flags().StringVarP(&fileName, "output", "o", "", "Send output to filename.")
140141
rootCmd.Flags().BoolVarP(&size, "size", "s", false, "Print the size of each file in bytes along with the name.")
141142
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).")
143+
rootCmd.Flags().BoolVarP(&dateTime, "date-time", "D", false, "Print the last modified time of each file.")
142144
}
143145

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

pkg/s3.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"strings"
8+
"time"
89

910
"github.com/aws/aws-sdk-go-v2/aws"
1011
"github.com/aws/aws-sdk-go-v2/config"
@@ -86,7 +87,7 @@ func formatBytes(b int64) string {
8687
}
8788

8889
// FetchS3ObjectKeys returns a slice of keys for all objects in the specified bucket and prefix
89-
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int,size, humanReadable bool) ([][]string, error) {
90+
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int,size, humanReadable bool,dateTime bool) ([][]string, error) {
9091
var delimiter *string
9192
if maxDepth != nil {
9293
delimiter = aws.String("/")
@@ -124,10 +125,22 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
124125

125126
for _, obj := range page.Contents {
126127
key := strings.Split(*obj.Key, "/")
128+
129+
meta := []string{}
127130
if humanReadable {
128-
key[len(key)-1] = fmt.Sprintf("[%7s] %s", formatBytes(*obj.Size), key[len(key)-1])
131+
meta = append(meta, formatBytes(*obj.Size))
129132
} else if size {
130-
key[len(key)-1] = fmt.Sprintf("[%7d] %s", *obj.Size, key[len(key)-1])
133+
meta = append(meta, fmt.Sprintf("%d", *obj.Size))
134+
}
135+
if dateTime {
136+
t := *obj.LastModified
137+
layout := "Jan 2 15:04"
138+
formatted := t.In(time.Local).Format(layout)
139+
fmt.Println(formatted)
140+
meta = append(meta, formatted)
141+
}
142+
if len(meta) > 0 {
143+
key[len(key)-1] = fmt.Sprintf("[%7s] %s", strings.Join(meta, " "), key[len(key)-1])
131144
}
132145
keys = append(keys, key)
133146
}

0 commit comments

Comments
 (0)