Skip to content

Commit 27b51b0

Browse files
authored
Merge pull request #48 from orangekame3/add-pattern
[feat]: Add `-P` option
2 parents 2ca239f + dd8d62c commit 27b51b0

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ Flags:
261261
-m, --mfa Use Multi-Factor Authentication
262262
-n, --no-color Disable colorized output
263263
-o, --output string Send output to filename.
264+
-P, --pattern string List files that match the pattern.
264265
-p, --profile string AWS profile to use (default "default")
265266
-r, --region string AWS region to use (overrides the region specified in the profile)
266267
-s, --size Print the size of each file in bytes along with the name.
267268
-u, --username Print the owner of each file.
268269
-v, --version version for stree
269270

270-
271271
```
272272

273273
# License

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ var (
5353
dateTime bool
5454
username bool
5555
directoryOnly bool
56+
pattern string
5657
)
5758

5859
var rootCmd = &cobra.Command{
@@ -83,7 +84,7 @@ var rootCmd = &cobra.Command{
8384
if level > 0 {
8485
maxDepth = &level
8586
}
86-
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth, size, humanReadable, dateTime, username)
87+
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth, size, humanReadable, dateTime, username,pattern)
8788
if err != nil {
8889
log.Fatalf("failed to fetch S3 object keys: %v", err)
8990
return
@@ -145,6 +146,7 @@ func init() {
145146
rootCmd.Flags().BoolVarP(&dateTime, "date-time", "D", false, "Print the last modified time of each file.")
146147
rootCmd.Flags().BoolVarP(&username, "username", "u", false, "Print the owner of each file.")
147148
rootCmd.Flags().BoolVarP(&directoryOnly, "directory-only", "d", false, "List directories only.")
149+
rootCmd.Flags().StringVarP(&pattern, "pattern", "P", "", "List files that match the pattern.")
148150
}
149151

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

pkg/s3.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package pkg
44
import (
55
"context"
66
"fmt"
7+
"path/filepath"
78
"strings"
89
"time"
910

@@ -87,7 +88,7 @@ func formatBytes(b int64) string {
8788
}
8889

8990
// FetchS3ObjectKeys returns a slice of keys for all objects in the specified bucket and prefix
90-
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int, size, humanReadable bool, dateTime bool, username bool) ([][]string, error) {
91+
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int, size, humanReadable bool, dateTime bool, username bool,pattern string) ([][]string, error) {
9192
var delimiter *string
9293
var fetchOwner *bool
9394
if maxDepth != nil {
@@ -153,7 +154,14 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
153154
if len(meta) > 0 {
154155
key[len(key)-1] = fmt.Sprintf("[%7s] %s", strings.Join(meta, " "), key[len(key)-1])
155156
}
156-
keys = append(keys, key)
157+
if pattern != "" {
158+
if match, _ := filepath.Match(pattern, filepath.Base(*obj.Key)); match {
159+
keys = append(keys, key)
160+
}
161+
}else{
162+
keys = append(keys, key)
163+
}
164+
157165
}
158166

159167
if maxDepth != nil {

0 commit comments

Comments
 (0)