Skip to content

Commit 2ca239f

Browse files
authored
Merge pull request #47 from orangekame3/add-directory-only
[feat]: Add -d option
2 parents 6f893ac + fd3c3e3 commit 2ca239f

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

README.md

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

252252
Flags:
253253
-D, --date-time Print the last modified time of each file.
254+
-d, --directory-only List directories only.
254255
-e, --endpoint-url string AWS endpoint URL to use (useful for local testing with LocalStack)
255256
-f, --full-path Print the full path prefix for each file.
256257
-h, --help help for stree
@@ -263,7 +264,9 @@ Flags:
263264
-p, --profile string AWS profile to use (default "default")
264265
-r, --region string AWS region to use (overrides the region specified in the profile)
265266
-s, --size Print the size of each file in bytes along with the name.
266-
-u, --username Print the owner of
267+
-u, --username Print the owner of each file.
268+
-v, --version version for stree
269+
267270

268271
```
269272

cmd/root.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ import (
3939
)
4040

4141
var (
42-
awsProfile string
43-
awsRegion string
44-
endpointURL string
45-
local bool
46-
noColor bool
47-
mfa bool
48-
level int
49-
fullPath bool
50-
fileName string
51-
size bool
42+
awsProfile string
43+
awsRegion string
44+
endpointURL string
45+
local bool
46+
noColor bool
47+
mfa bool
48+
level int
49+
fullPath bool
50+
fileName string
51+
size bool
5252
humanReadable bool
53-
dateTime bool
54-
username bool
53+
dateTime bool
54+
username bool
55+
directoryOnly bool
5556
)
5657

5758
var rootCmd = &cobra.Command{
@@ -82,18 +83,18 @@ var rootCmd = &cobra.Command{
8283
if level > 0 {
8384
maxDepth = &level
8485
}
85-
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth,size, humanReadable,dateTime,username)
86+
keys, err := pkg.FetchS3ObjectKeys(s3Svc, bucket, prefix, maxDepth, size, humanReadable, dateTime, username)
8687
if err != nil {
8788
log.Fatalf("failed to fetch S3 object keys: %v", err)
8889
return
8990
}
9091

9192
root := gtree.NewRoot(bucket)
9293
if noColor || fileName != "" {
93-
root = pkg.BuildTreeWithoutColor(root, bucket, keys, fullPath)
94+
root = pkg.BuildTreeWithoutColor(root, bucket, keys, fullPath, directoryOnly)
9495
} else {
9596
root = gtree.NewRoot(color.BlueString(bucket))
96-
root = pkg.BuildTreeWithColor(root, bucket, keys, fullPath)
97+
root = pkg.BuildTreeWithColor(root, bucket, keys, fullPath, directoryOnly)
9798
}
9899

99100
fileCount, dirCount := pkg.ProcessKeys(keys)
@@ -143,6 +144,7 @@ func init() {
143144
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).")
144145
rootCmd.Flags().BoolVarP(&dateTime, "date-time", "D", false, "Print the last modified time of each file.")
145146
rootCmd.Flags().BoolVarP(&username, "username", "u", false, "Print the owner of each file.")
147+
rootCmd.Flags().BoolVarP(&directoryOnly, "directory-only", "d", false, "List directories only.")
146148
}
147149

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

pkg/s3.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func formatBytes(b int64) string {
8787
}
8888

8989
// 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) {
90+
func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDepth *int, size, humanReadable bool, dateTime bool, username bool) ([][]string, error) {
9191
var delimiter *string
9292
var fetchOwner *bool
9393
if maxDepth != nil {
@@ -115,9 +115,9 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
115115
}
116116

117117
input := &s3.ListObjectsV2Input{
118-
Bucket: aws.String(bucket),
119-
Prefix: aws.String(currentPrefix),
120-
Delimiter: delimiter,
118+
Bucket: aws.String(bucket),
119+
Prefix: aws.String(currentPrefix),
120+
Delimiter: delimiter,
121121
FetchOwner: fetchOwner,
122122
}
123123

@@ -131,8 +131,7 @@ func FetchS3ObjectKeys(s3Client *s3.Client, bucket string, prefix string, maxDep
131131

132132
for _, obj := range page.Contents {
133133
key := strings.Split(*obj.Key, "/")
134-
// fmt.Println(*obj.Owner.DisplayName)
135-
134+
136135
meta := []string{}
137136

138137
if username {

pkg/tree.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@ func createFullPath(bucket string, keys [][]string) [][]string {
1818
}
1919

2020
// BuildTreeWithColor builds a tree with colored nodes
21-
func BuildTreeWithColor(root *gtree.Node, bucket string, keys [][]string, f bool) *gtree.Node {
21+
func BuildTreeWithColor(root *gtree.Node, bucket string, keys [][]string, f bool, directoryOnly bool) *gtree.Node {
2222
if f {
2323
keys = createFullPath(bucket, keys)
2424
}
2525

2626
for _, key := range keys {
27-
addNodeWithColor(root, key, 0)
27+
addNodeWithColor(root, key, 0, directoryOnly)
2828
}
2929
return root
3030
}
3131

3232
// BuildTreeWithoutColor builds a tree without colored nodes
33-
func BuildTreeWithoutColor(root *gtree.Node, bucket string, keys [][]string, f bool) *gtree.Node {
33+
func BuildTreeWithoutColor(root *gtree.Node, bucket string, keys [][]string, f bool, directoryOnly bool) *gtree.Node {
3434
if f {
3535
keys = createFullPath(bucket, keys)
3636
}
3737
for _, key := range keys {
38-
addNodeWithoutColor(root, key, 0)
38+
addNodeWithoutColor(root, key, 0, directoryOnly)
3939
}
4040
return root
4141
}
4242

43-
func addNodeWithoutColor(parent *gtree.Node, keys []string, depth int) *gtree.Node {
43+
func addNodeWithoutColor(parent *gtree.Node, keys []string, depth int, directoryOnly bool) *gtree.Node {
4444
if len(keys) == 0 {
4545
return nil
4646
}
@@ -50,13 +50,18 @@ func addNodeWithoutColor(parent *gtree.Node, keys []string, depth int) *gtree.No
5050
return nil
5151
}
5252

53+
// Skip adding the root node if the tree is being built for a single directory
54+
if directoryOnly && len(keys) == 1 {
55+
return nil
56+
}
57+
5358
// Add the current key (without color) as a node to the parent
5459
node := parent.Add(keys[0])
5560
// Recursively add the remaining keys
56-
return addNodeWithoutColor(node, keys[1:], depth+1)
61+
return addNodeWithoutColor(node, keys[1:], depth+1, directoryOnly)
5762
}
5863

59-
func addNodeWithColor(parent *gtree.Node, keys []string, depth int) *gtree.Node {
64+
func addNodeWithColor(parent *gtree.Node, keys []string, depth int, directoryOnly bool) *gtree.Node {
6065
if len(keys) == 0 {
6166
return nil
6267
}
@@ -66,11 +71,16 @@ func addNodeWithColor(parent *gtree.Node, keys []string, depth int) *gtree.Node
6671
return nil
6772
}
6873

74+
// Skip adding the root node if the tree is being built for a single directory
75+
if directoryOnly && len(keys) == 1 {
76+
return nil
77+
}
78+
6979
// Add a colored node to the tree based on whether the current key represents a file or a directory
7080
coloredKey := getColorizedKey(keys[0], len(keys) == 1)
7181
node := parent.Add(coloredKey)
7282

73-
return addNodeWithColor(node, keys[1:], depth+1)
83+
return addNodeWithColor(node, keys[1:], depth+1, directoryOnly)
7484
}
7585

7686
// getColorizedKey returns the colored representation of a key, with different colors for files and directories

0 commit comments

Comments
 (0)