From e79bd7a4d55145167268f6c1ee603a599c51faf0 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 28 Apr 2015 02:51:17 -0700 Subject: [PATCH] Review code comments and restructure, part of #451 --- clientmethods.go | 91 +++++++++++++++++++++++++++++++++++++++ cmd-common.go | 93 ---------------------------------------- cp-methods.go | 50 +++++++++++++++++++++ copy-url.go => cp-url.go | 2 + copy.go => cp.go | 2 +- 5 files changed, 144 insertions(+), 94 deletions(-) create mode 100644 clientmethods.go create mode 100644 cp-methods.go rename copy-url.go => cp-url.go (98%) rename copy.go => cp.go (98%) diff --git a/clientmethods.go b/clientmethods.go new file mode 100644 index 00000000..a8f46938 --- /dev/null +++ b/clientmethods.go @@ -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) + } +} diff --git a/cmd-common.go b/cmd-common.go index 6c8a2b9d..a35bd9b0 100644 --- a/cmd-common.go +++ b/cmd-common.go @@ -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 -} diff --git a/cp-methods.go b/cp-methods.go new file mode 100644 index 00000000..4b356513 --- /dev/null +++ b/cp-methods.go @@ -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 +} diff --git a/copy-url.go b/cp-url.go similarity index 98% rename from copy-url.go rename to cp-url.go index 507161b9..bf3667be 100644 --- a/copy-url.go +++ b/cp-url.go @@ -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) diff --git a/copy.go b/cp.go similarity index 98% rename from copy.go rename to cp.go index ecfdeff1..527e9394 100644 --- a/copy.go +++ b/cp.go @@ -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 {