Skip to content

Commit

Permalink
refactor: isRaw added, readme change,syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
4o4x committed Jul 16, 2024
1 parent ad4c288 commit 2d917d9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ reasons for its fast speed, refer to [benchmarks](./README.md#Benchmarks) sectio
storage services and local filesystems.

- List buckets and objects
- Check if objects and buckets exist
- Upload, download or delete objects
- Move, copy or rename objects
- Set Server Side Encryption using AWS Key Management Service (KMS)
Expand Down Expand Up @@ -189,6 +188,14 @@ While executing the commands, `s5cmd` detects the region according to the follow
### Examples
#### Check if a bucket exists
s5cmd head s3://bucket/
#### Print a remote object's metadata
s5cmd head s3://bucket/object.gz
#### Download a single S3 object
s5cmd cp s3://bucket/object.gz .
Expand Down
9 changes: 4 additions & 5 deletions command/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ func (m HeadObjectMessage) String() string {
s += fmt.Sprintf("%15s%s", "Metadata: ", metadataString)

return s

}

return s
Expand Down Expand Up @@ -305,7 +304,7 @@ func (m HeadBucketMessage) JSON() string {

func validateHeadCommand(c *cli.Context) error {
if c.Args().Len() > 1 {
return fmt.Errorf("file or bucket name is required")
return fmt.Errorf("object or bucket name is required")
}

srcurl, err := url.New(c.Args().Get(0), url.WithVersion(c.String("version-id")),
Expand All @@ -315,14 +314,14 @@ func validateHeadCommand(c *cli.Context) error {
}

if srcurl.IsPrefix() {
return fmt.Errorf("target have to be a file or a bucket")
return fmt.Errorf("target have to be a object or a bucket")
}

if !srcurl.IsRemote() {
return fmt.Errorf("target is not a remote object: target should be remote object or bucket")
return fmt.Errorf("target should be remote object or bucket")
}

if srcurl.IsWildcard() && !c.Bool("raw") {
if srcurl.IsWildcard() && srcurl.IsRaw() {
return fmt.Errorf("remote source %q can not contain glob characters", srcurl)
}

Expand Down
21 changes: 19 additions & 2 deletions e2e/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)

Expand All @@ -18,6 +19,9 @@ func TestHead(t *testing.T) {
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Expected{ExitCode: 1})
assertLines(t, result.Stderr(), map[int]compareFunc{
0: match(`ERROR "head": target should be remote object or bucket`),
})
}

// head bucket
Expand Down Expand Up @@ -49,6 +53,10 @@ func TestHeadBucketNonExistent(t *testing.T) {
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Expected{ExitCode: 1})

assertLines(t, result.Stderr(), map[int]compareFunc{
0: contains(`ERROR "head s3://non-existent-bucket": NotFound: Not Found status code: 404`),
})
}

// --json head bucket
Expand Down Expand Up @@ -80,6 +88,7 @@ func TestHeadBucketJSONNonExistent(t *testing.T) {
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Expected{ExitCode: 1})
assert.Equal(t, strings.Contains(result.Stderr(), `"error":"NotFound: Not Found status code: 404`), true)
}

// head bucket --humanize
Expand Down Expand Up @@ -174,6 +183,9 @@ func TestHeadObjectNonExistent(t *testing.T) {
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Expected{ExitCode: 1})
assertLines(t, result.Stderr(), map[int]compareFunc{
0: contains(`non-existent-file.txt not found`),
})
}

// --json head object
Expand Down Expand Up @@ -361,9 +373,14 @@ func TestHeadObjectRaw(t *testing.T) {
func TestHeadObjectWildcardWithoutRawFlag(t *testing.T) {
t.Parallel()

_, s5cmd := setup(t)
s3client, s5cmd := setup(t)

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

putFile(t, s3client, bucket, "file.txt", "content")

cmd := s5cmd("head", "s3://bucket/file*.txt")
cmd := s5cmd("head", fmt.Sprintf("s3://%v/file*.txt", bucket))
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Expected{ExitCode: 1})
Expand Down
4 changes: 4 additions & 0 deletions storage/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ func (u *URL) IsWildcard() bool {
return !u.raw && hasGlobCharacter(u.Path)
}

func (u *URL) IsRaw() bool {
return u.raw
}

// parseBatch parses keys for wildcard operations.
// It cuts the key starting from first directory before the
// wildcard part (filter)
Expand Down

0 comments on commit 2d917d9

Please sign in to comment.