Skip to content

Commit 0800552

Browse files
authored
Merge pull request #49 from orangekame3/add-inverse-pattern
[feat]: add -I option
2 parents 27b51b0 + 971a736 commit 0800552

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,25 @@ Usage:
250250
stree [bucket/prefix] [flags]
251251

252252
Flags:
253-
-D, --date-time Print the last modified time of each file.
254-
-d, --directory-only List directories only.
255-
-e, --endpoint-url string AWS endpoint URL to use (useful for local testing with LocalStack)
256-
-f, --full-path Print the full path prefix for each file.
257-
-h, --help help for stree
258-
-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).
259-
-L, --level int Descend only level directories
260-
-l, --local Use LocalStack configuration
261-
-m, --mfa Use Multi-Factor Authentication
262-
-n, --no-color Disable colorized output
263-
-o, --output string Send output to filename.
264-
-P, --pattern string List files that match the pattern.
265-
-p, --profile string AWS profile to use (default "default")
266-
-r, --region string AWS region to use (overrides the region specified in the profile)
267-
-s, --size Print the size of each file in bytes along with the name.
268-
-u, --username Print the owner of each file.
269-
-v, --version version for stree
253+
-D, --date-time Print the last modified time of each file.
254+
-d, --directory-only List directories only.
255+
-e, --endpoint-url string AWS endpoint URL to use (useful for local testing with LocalStack)
256+
-f, --full-path Print the full path prefix for each file.
257+
-h, --help help for stree
258+
-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).
259+
-I, --inverse-pattern string List files that do not match the pattern.
260+
-L, --level int Descend only level directories
261+
-l, --local Use LocalStack configuration
262+
-m, --mfa Use Multi-Factor Authentication
263+
-n, --no-color Disable colorized output
264+
-o, --output string Send output to filename.
265+
-P, --pattern string List files that match the pattern.
266+
-p, --profile string AWS profile to use (default "default")
267+
-r, --region string AWS region to use (overrides the region specified in the profile)
268+
-s, --size Print the size of each file in bytes along with the name.
269+
-u, --username Print the owner of each file.
270+
-v, --version version for stree
271+
270272

271273
```
272274

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var (
5454
username bool
5555
directoryOnly bool
5656
pattern string
57+
inversePattern string
5758
)
5859

5960
var rootCmd = &cobra.Command{
@@ -84,7 +85,7 @@ var rootCmd = &cobra.Command{
8485
if level > 0 {
8586
maxDepth = &level
8687
}
87-
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth, size, humanReadable, dateTime, username,pattern)
88+
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth, size, humanReadable, dateTime, username,pattern,inversePattern)
8889
if err != nil {
8990
log.Fatalf("failed to fetch S3 object keys: %v", err)
9091
return
@@ -147,6 +148,8 @@ func init() {
147148
rootCmd.Flags().BoolVarP(&username, "username", "u", false, "Print the owner of each file.")
148149
rootCmd.Flags().BoolVarP(&directoryOnly, "directory-only", "d", false, "List directories only.")
149150
rootCmd.Flags().StringVarP(&pattern, "pattern", "P", "", "List files that match the pattern.")
151+
rootCmd.Flags().StringVarP(&inversePattern, "inverse-pattern", "I", "", "List files that do not match the pattern.")
152+
150153
}
151154

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

pkg/s3.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func formatBytes(b int64) string {
8888
}
8989

9090
// FetchS3ObjectKeys returns a slice of keys for all objects in the specified bucket and prefix
91-
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int, size, humanReadable bool, dateTime bool, username bool,pattern string) ([][]string, error) {
91+
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int, size, humanReadable bool, dateTime bool, username bool,pattern string,inversePattern string) ([][]string, error) {
9292
var delimiter *string
9393
var fetchOwner *bool
9494
if maxDepth != nil {
@@ -154,11 +154,17 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
154154
if len(meta) > 0 {
155155
key[len(key)-1] = fmt.Sprintf("[%7s] %s", strings.Join(meta, " "), key[len(key)-1])
156156
}
157+
include := true
157158
if pattern != "" {
158-
if match, _ := filepath.Match(pattern, filepath.Base(*obj.Key)); match {
159-
keys = append(keys, key)
159+
include, _ = filepath.Match(pattern, filepath.Base(*obj.Key))
160+
}
161+
if inversePattern != "" {
162+
exclude, _ := filepath.Match(inversePattern, filepath.Base(*obj.Key))
163+
if exclude {
164+
include = false
160165
}
161-
}else{
166+
}
167+
if include {
162168
keys = append(keys, key)
163169
}
164170

0 commit comments

Comments
 (0)