Merge pull request #561 from harshavardhana/pr_out_review_code_comments_and_restructure_part_of_451

This commit is contained in:
Harshavardhana
2015-04-28 02:52:32 -07:00
5 changed files with 144 additions and 94 deletions
+91
View File
@@ -0,0 +1,91 @@
/*
* Mini Copy (C) 2015 Minio, 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 main
import (
"io"
"path/filepath"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/client/fs"
"github.com/minio-io/mc/pkg/client/s3"
"github.com/minio-io/minio/pkg/iodine"
)
// clientMethods interface for mock tests
type clientMethods interface {
cpMethods
getNewClient(urlStr string, config *hostConfig, debug bool) (clnt client.Client, err error)
}
type mcClientMethods struct{}
type sourceReader struct {
reader io.ReadCloser
length int64
md5hex string
}
// getSourceReader -
func (methods mcClientMethods) getSourceReader(sourceURL string, sourceConfig *hostConfig) (
reader io.ReadCloser, length int64, md5hex string, err error) {
sourceClnt, err := methods.getNewClient(sourceURL, sourceConfig, globalDebugFlag)
if err != nil {
return nil, 0, "", iodine.New(err, map[string]string{"failedURL": sourceURL})
}
if _, err := sourceClnt.Stat(); err != nil {
return nil, 0, "", iodine.New(err, map[string]string{"failedURL": sourceURL})
}
return sourceClnt.Get()
}
// getTargetWriter -
func (methods mcClientMethods) getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, length int64) (io.WriteCloser, error) {
targetClnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil {
return nil, iodine.New(err, nil)
}
return targetClnt.Put(md5hex, length)
}
// getNewClient gives a new client interface
func (methods mcClientMethods) getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Client, err error) {
t := client.GetType(urlStr)
switch t {
case client.Object: // Minio and S3 compatible object storage
if auth == nil {
return nil, iodine.New(errInvalidArgument{}, nil)
}
s3Config := new(s3.Config)
s3Config.AccessKeyID = auth.AccessKeyID
s3Config.SecretAccessKey = auth.SecretAccessKey
s3Config.UserAgent = mcUserAgent
s3Config.HostURL = urlStr
s3Config.Debug = debug
clnt = s3.New(s3Config)
return clnt, nil
case client.Filesystem:
clnt = fs.New(filepath.Clean(urlStr))
return clnt, nil
default:
return nil, iodine.New(errUnsupportedScheme{
scheme: client.GetTypeToString(t),
url: urlStr,
}, nil)
}
}
-93
View File
@@ -18,15 +18,9 @@ package main
import (
"net"
"path/filepath"
"time"
"io"
"github.com/cheggaaa/pb"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/client/fs"
"github.com/minio-io/mc/pkg/client/s3"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
)
@@ -67,90 +61,3 @@ func startBar(size int64) *pb.ProgressBar {
bar.Format("[=> ]")
return bar
}
// clientMethods interface for mock tests
type clientMethods interface {
getSourceReader(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length int64, md5hex string, err error)
getTargetWriter(targetURL string, targetConfig *hostConfig, md5Hex string, length int64) (io.WriteCloser, error)
getNewClient(urlStr string, config *hostConfig, debug bool) (clnt client.Client, err error)
}
type mcClientMethods struct{}
type sourceReader struct {
reader io.ReadCloser
length int64
md5hex string
}
// getSourceReader -
func (methods mcClientMethods) getSourceReader(sourceURL string, sourceConfig *hostConfig) (
reader io.ReadCloser, length int64, md5hex string, err error) {
sourceClnt, err := methods.getNewClient(sourceURL, sourceConfig, globalDebugFlag)
if err != nil {
return nil, 0, "", iodine.New(err, map[string]string{"failedURL": sourceURL})
}
if _, err := sourceClnt.Stat(); err != nil {
return nil, 0, "", iodine.New(err, map[string]string{"failedURL": sourceURL})
}
return sourceClnt.Get()
}
// getTargetWriter -
func (methods mcClientMethods) getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, length int64) (
io.WriteCloser, error) {
targetClnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil {
return nil, iodine.New(err, nil)
}
return targetClnt.Put(md5hex, length)
}
// getNewClient gives a new client interface
func (methods mcClientMethods) getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Client, err error) {
t := client.GetType(urlStr)
switch t {
case client.Object: // Minio and S3 compatible object storage
if auth == nil {
return nil, iodine.New(errInvalidArgument{}, nil)
}
s3Config := new(s3.Config)
s3Config.AccessKeyID = auth.AccessKeyID
s3Config.SecretAccessKey = auth.SecretAccessKey
s3Config.UserAgent = mcUserAgent
s3Config.HostURL = urlStr
s3Config.Debug = debug
clnt = s3.New(s3Config)
return clnt, nil
case client.Filesystem:
clnt = fs.New(filepath.Clean(urlStr))
return clnt, nil
default:
return nil, iodine.New(errUnsupportedScheme{
scheme: client.GetTypeToString(t),
url: urlStr,
}, nil)
}
}
// getSourceReaders -
func getSourceReaders(methods clientMethods, sourceURLConfigMap map[string]*hostConfig) (map[string]sourceReader, error) {
sourceURLReaderMap := make(map[string]sourceReader)
for sourceURL, sourceConfig := range sourceURLConfigMap {
reader, length, md5hex, err := methods.getSourceReader(sourceURL, sourceConfig)
if err != nil {
for _, sourceReader := range sourceURLReaderMap {
sourceReader.reader.Close()
}
return nil, iodine.New(err, nil)
}
sr := sourceReader{
reader: reader,
length: length,
md5hex: md5hex,
}
sourceURLReaderMap[sourceURL] = sr
}
return sourceURLReaderMap, nil
}
+50
View File
@@ -0,0 +1,50 @@
/*
* Mini Copy (C) 2015 Minio, 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 main
import (
"io"
"github.com/minio-io/minio/pkg/iodine"
)
// cpMethods - methods only valid for cp
type cpMethods interface {
getSourceReader(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length int64, md5hex string, err error)
getTargetWriter(targetURL string, targetConfig *hostConfig, md5Hex string, length int64) (io.WriteCloser, error)
}
// getSourceReaders -
func getSourceReaders(methods cpMethods, sourceURLConfigMap map[string]*hostConfig) (map[string]sourceReader, error) {
sourceURLReaderMap := make(map[string]sourceReader)
for sourceURL, sourceConfig := range sourceURLConfigMap {
reader, length, md5hex, err := methods.getSourceReader(sourceURL, sourceConfig)
if err != nil {
for _, sourceReader := range sourceURLReaderMap {
sourceReader.reader.Close()
}
return nil, iodine.New(err, nil)
}
sr := sourceReader{
reader: reader,
length: length,
md5hex: md5hex,
}
sourceURLReaderMap[sourceURL] = sr
}
return sourceURLReaderMap, nil
}
+2
View File
@@ -26,6 +26,8 @@ import (
"github.com/minio-io/minio/pkg/iodine"
)
/// mc cp related URL processing code
// getObjectKey - get object name from source url
func getObjectKey(sourceURL string) (objectName string) {
u, _ := url.Parse(sourceURL)
+1 -1
View File
@@ -23,7 +23,7 @@ import (
"github.com/minio-io/minio/pkg/iodine"
)
/// Copy - related internal functions
/// mc cp - related internal functions
// doCopy
func doCopy(methods clientMethods, reader io.ReadCloser, md5hex string, length int64, targetURL string, targetConfig *hostConfig) error {