Skip to content

Commit

Permalink
Fix scheme of s3Path string (#86)
Browse files Browse the repository at this point in the history
* Fix scheme of s3Path string

* Add unit tests
  • Loading branch information
at-wat authored Oct 28, 2020
1 parent 429b229 commit bf93abb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 20 deletions.
42 changes: 42 additions & 0 deletions s3path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2019 SEQSENSE, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package s3sync

import (
"errors"
"net/url"
"strings"
)

var errNoBucketName = errors.New("s3 url is missing bucket name")

type s3Path struct {
bucket string
bucketPrefix string
}

func urlToS3Path(url *url.URL) (*s3Path, error) {
if url.Host == "" {
return nil, errNoBucketName
}

return &s3Path{
bucket: url.Host,
bucketPrefix: strings.TrimPrefix(url.Path, "/"),
}, nil
}

func (p *s3Path) String() string {
return "s3://" + p.bucket + "/" + p.bucketPrefix
}
54 changes: 54 additions & 0 deletions s3path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2019 SEQSENSE, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package s3sync

import (
"net/url"
"testing"
)

func TestURLToS3Path(t *testing.T) {
t.Run("NoBucketName", func(t *testing.T) {
_, err := urlToS3Path(&url.URL{
Host: "",
Path: "test",
})
if err != errNoBucketName {
t.Fatalf("Expected error %v, got %v", errNoBucketName, err)
}
})
t.Run("Normal", func(t *testing.T) {
p, err := urlToS3Path(&url.URL{
Host: "bucket",
Path: "test",
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if p.bucket != "bucket" || p.bucketPrefix != "test" {
t.Fatalf("Unexpected s3Path: %v", p)
}
})
}

func TestS3Path_String(t *testing.T) {
p := &s3Path{
bucket: "bucket",
bucketPrefix: "test",
}
const expected = "s3://bucket/test"
if s := p.String(); s != expected {
t.Fatalf("Expected %s, got %s", expected, s)
}
}
20 changes: 0 additions & 20 deletions s3sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ type Manager struct {
contentType *string
}

type s3Path struct {
bucket string
bucketPrefix string
}

type operation int

const (
Expand All @@ -67,21 +62,6 @@ type fileOp struct {
op operation
}

func urlToS3Path(url *url.URL) (*s3Path, error) {
if url.Host == "" {
return nil, errors.New("s3 url is missing bucket name")
}

return &s3Path{
bucket: url.Host,
bucketPrefix: strings.TrimPrefix(url.Path, "/"),
}, nil
}

func (p *s3Path) String() string {
return "s://" + p.bucket + "/" + p.bucketPrefix
}

// New returns a new Manager.
func New(sess *session.Session, options ...Option) *Manager {
m := &Manager{
Expand Down

0 comments on commit bf93abb

Please sign in to comment.