Skip to content

Commit

Permalink
fix: ls differentiate exit codes for empty and non-existent buckets (#1)
Browse files Browse the repository at this point in the history
Previously, s5cmd ls returned an exit code of 1 for both empty buckets and non-existent buckets, making it difficult to differentiate between the two cases. This change updates the behavior to:

Return exit code 0 for empty buckets, similar to the behavior of s3cmd.
Return exit code 1 for non-existent buckets, providing a clear distinction.
Example behavior after the change:

s5cmd ls s3://empty-bucket returns exit code 0. It does not print anything.
s5cmd ls s3://non-existent-bucket returns exit code 1 with an appropriate error message.
Also, select command with --all-versions true flag no longer prints "ERROR " s3://bucket/": no object found" message for empty buckets.
Similarly, du command used to print
"ERROR "du s3://empty-bucket": no object found
0 bytes in 0 objects: s3://empty-bucket"
Now it will omit "ERROR "du s3://bucket": no object found" part. They both exits with 0 without any change.
  • Loading branch information
tarikozyurtt authored Jul 4, 2024
1 parent c13b860 commit 2dbc567
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions e2e/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,20 @@ func TestListNestedLocalFolders(t *testing.T) {
2: match(filepath.ToSlash("file.txt")),
}, trimMatch(dateRe), alignment(true))
}

// ls empty bucket
func TestEmptyBucket(t *testing.T) {
t.Parallel()

s3client, s5cmd := setup(t)

bucket := s3BucketFromTestName(t)
createBucket(t, s3client, bucket)

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

result.Assert(t, icmd.Success)

assertLines(t, result.Stdout(), map[int]compareFunc{}, alignment(true))
}
6 changes: 3 additions & 3 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (s *S3) listObjectVersions(ctx context.Context, url *url.URL) <-chan *Objec
return
}

if !objectFound {
if !objectFound && !url.IsBucket() {
objCh <- &Object{Err: ErrNoObjectFound}
}
}()
Expand Down Expand Up @@ -378,7 +378,7 @@ func (s *S3) listObjectsV2(ctx context.Context, url *url.URL) <-chan *Object {
return
}

if !objectFound {
if !objectFound && !url.IsBucket() {
objCh <- &Object{Err: ErrNoObjectFound}
}
}()
Expand Down Expand Up @@ -470,7 +470,7 @@ func (s *S3) listObjects(ctx context.Context, url *url.URL) <-chan *Object {
return
}

if !objectFound {
if !objectFound && !url.IsBucket() {
objCh <- &Object{Err: ErrNoObjectFound}
}
}()
Expand Down

0 comments on commit 2dbc567

Please sign in to comment.