Skip to content

Commit

Permalink
Merge branch 'master' into cp-rm-sync-include-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmethakanbesel committed Jul 21, 2023
2 parents 36855d7 + ef362f3 commit c21ee76
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Breaking changes
#### Features
-- Added `--show-fullpath` flag to `ls`. (#[596](https://github.com/peak/s5cmd/issues/596))
#### Improvements
#### Bugfixes
- Fixed a bug introduced with `external sort` support in `sync` command which prevents `sync` to an empty destination with `--delete` option. ([#576](https://github.com/peak/s5cmd/issues/576))
Expand Down
15 changes: 14 additions & 1 deletion command/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Examples:
10. List all versions of all objects in the bucket
> s5cmd {{.HelpName}} --all-versions "s3://bucket/*"
11. List all files only with their fullpaths
> s5cmd {{.HelpName}} --show-fullpath "s3://bucket/*"
`

func NewListCommand() *cli.Command {
Expand Down Expand Up @@ -87,6 +90,10 @@ func NewListCommand() *cli.Command {
Name: "all-versions",
Usage: "list all versions of object(s)",
},
&cli.BoolFlag{
Name: "show-fullpath",
Usage: "shows only the fullpath names of the object(s)",
},
},
Before: func(c *cli.Context) error {
err := validateLSCommand(c)
Expand Down Expand Up @@ -122,6 +129,7 @@ func NewListCommand() *cli.Command {
humanize: c.Bool("humanize"),
showStorageClass: c.Bool("storage-class"),
exclude: c.StringSlice("exclude"),
showFullPath: c.Bool("show-fullpath"),

storageOpts: NewStorageOpts(c),
}.Run(c.Context)
Expand All @@ -142,6 +150,7 @@ type List struct {
showEtag bool
humanize bool
showStorageClass bool
showFullPath bool
exclude []string

storageOpts storage.Options
Expand Down Expand Up @@ -205,6 +214,7 @@ func (l List) Run(ctx context.Context) error {
showEtag: l.showEtag,
showHumanized: l.humanize,
showStorageClass: l.showStorageClass,
showFullPath: l.showFullPath,
}

log.Info(msg)
Expand All @@ -220,6 +230,7 @@ type ListMessage struct {
showEtag bool
showHumanized bool
showStorageClass bool
showFullPath bool
}

// humanize is a helper function to humanize bytes.
Expand All @@ -239,6 +250,9 @@ const (

// String returns the string representation of ListMessage.
func (l ListMessage) String() string {
if l.showFullPath {
return l.Object.URL.String()
}
var etag string
// date and storage fiels
var listFormat = "%19s %2s"
Expand Down Expand Up @@ -278,7 +292,6 @@ func (l ListMessage) String() string {
if l.showStorageClass {
stclass = fmt.Sprintf("%v", l.Object.StorageClass)
}

s = fmt.Sprintf(
listFormat,
l.Object.ModTime.Format(dateFormat),
Expand Down
38 changes: 38 additions & 0 deletions e2e/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,44 @@ func TestListS3ObjectsAndFolders(t *testing.T) {
}, alignment(true))
}

func TestListS3ObjectsAndFoldersWithTheirFullpath(t *testing.T) {
t.Parallel()

s3client, s5cmd := setup(t)

bucket := s3BucketFromTestName(t)
createBucket(t, s3client, bucket)
putFile(t, s3client, bucket, "testfile1.txt", "content")
putFile(t, s3client, bucket, "report.gz", "content")
putFile(t, s3client, bucket, "a/testfile2.txt", "content")
putFile(t, s3client, bucket, "b/testfile3.txt", "content")
putFile(t, s3client, bucket, "b/testfile4.txt", "content")
putFile(t, s3client, bucket, "c/testfile5.gz", "content")
putFile(t, s3client, bucket, "d/foo/bar/file7.txt", "content")
putFile(t, s3client, bucket, "d/foo/bar/testfile8.txt", "content")
putFile(t, s3client, bucket, "e/txt/testfile9.txt.gz", "content")
putFile(t, s3client, bucket, "f/txt/testfile10.txt", "content")

cmd := s5cmd("ls", "--show-fullpath", "s3://"+bucket+"/*")
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Success)

// assert lexical order
assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals("s3://%v/a/testfile2.txt", bucket),
1: equals("s3://%v/b/testfile3.txt", bucket),
2: equals("s3://%v/b/testfile4.txt", bucket),
3: equals("s3://%v/c/testfile5.gz", bucket),
4: equals("s3://%v/d/foo/bar/file7.txt", bucket),
5: equals("s3://%v/d/foo/bar/testfile8.txt", bucket),
6: equals("s3://%v/e/txt/testfile9.txt.gz", bucket),
7: equals("s3://%v/f/txt/testfile10.txt", bucket),
8: equals("s3://%v/report.gz", bucket),
9: equals("s3://%v/testfile1.txt", bucket),
}, alignment(true))
}

// ls bucket/prefix
func TestListS3ObjectsAndFoldersWithPrefix(t *testing.T) {
t.Parallel()
Expand Down
12 changes: 6 additions & 6 deletions e2e/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ func TestRunWildcardCountGreaterEqualThanWorkerCount(t *testing.T) {
putFile(t, s3client, bucket, "file.txt", "content")

content := []string{
"cp s3://" + bucket + "/f*.txt .",
"cp s3://" + bucket + "/f*.txt .",
"cp s3://" + bucket + "/f*.txt .",
"cp s3://" + bucket + "/f*.txt folder1",
"cp s3://" + bucket + "/f*.txt folder2",
"cp s3://" + bucket + "/f*.txt folder3",
}
file := fs.NewFile(t, "prefix", fs.WithContent(strings.Join(content, "\n")))
defer file.Remove()
Expand All @@ -236,9 +236,9 @@ func TestRunWildcardCountGreaterEqualThanWorkerCount(t *testing.T) {
result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{
0: equals(`cp s3://%v/file.txt file.txt`, bucket),
1: equals(`cp s3://%v/file.txt file.txt`, bucket),
2: equals(`cp s3://%v/file.txt file.txt`, bucket),
0: equals(`cp s3://%v/file.txt folder1/file.txt`, bucket),
1: equals(`cp s3://%v/file.txt folder2/file.txt`, bucket),
2: equals(`cp s3://%v/file.txt folder3/file.txt`, bucket),
}, sortInput(true))

assertLines(t, result.Stderr(), map[int]compareFunc{})
Expand Down

0 comments on commit c21ee76

Please sign in to comment.