Merge pull request #387 from fkautz/pr_out_first_round_of_copy_refactoring_to_be_more_testable
This commit is contained in:
+3
-3
@@ -59,11 +59,11 @@ func mustGetMcBashCompletionFilename() string {
|
||||
|
||||
// NewClient - get new client
|
||||
// TODO refactor this to be more testable
|
||||
func getNewClient(u *urlParser, debug bool) (clnt client.Client, err error) {
|
||||
func getNewClient(u *parsedURL, debug bool) (clnt client.Client, err error) {
|
||||
if u == nil {
|
||||
return nil, iodine.New(errInvalidArgument{}, nil)
|
||||
}
|
||||
switch u.urlType {
|
||||
switch u.scheme {
|
||||
case urlObjectStorage: // Minio and S3 compatible object storage
|
||||
hostCfg, err := getHostConfig(u.String())
|
||||
if err != nil {
|
||||
@@ -83,6 +83,6 @@ func getNewClient(u *urlParser, debug bool) (clnt client.Client, err error) {
|
||||
clnt = fs.GetNewClient(u.String())
|
||||
return clnt, nil
|
||||
default:
|
||||
return nil, iodine.New(errUnsupportedScheme{scheme: u.urlType}, nil)
|
||||
return nil, iodine.New(errUnsupportedScheme{scheme: u.scheme}, nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
"github.com/minio-io/minio/pkg/utils/log"
|
||||
)
|
||||
|
||||
func getSourceReader(sourceURLParser *urlParser) (reader io.ReadCloser, length int64, md5hex string, err error) {
|
||||
func getSourceReader(sourceURLParser *parsedURL) (reader io.ReadCloser, length int64, md5hex string, err error) {
|
||||
sourceClnt, err := getNewClient(sourceURLParser, globalDebugFlag)
|
||||
if err != nil {
|
||||
return nil, 0, "", iodine.New(err, map[string]string{"sourceURL": sourceURLParser.String()})
|
||||
@@ -41,7 +41,7 @@ func getSourceReader(sourceURLParser *urlParser) (reader io.ReadCloser, length i
|
||||
return sourceClnt.Get(sourceBucket, sourceObject)
|
||||
}
|
||||
|
||||
func getTargetWriter(targetURLParser *urlParser, md5Hex string, length int64) (io.WriteCloser, error) {
|
||||
func getTargetWriter(targetURLParser *parsedURL, md5Hex string, length int64) (io.WriteCloser, error) {
|
||||
targetClnt, err := getNewClient(targetURLParser, globalDebugFlag)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, map[string]string{"failedURL": targetURLParser.String()})
|
||||
@@ -55,7 +55,7 @@ func getTargetWriter(targetURLParser *urlParser, md5Hex string, length int64) (i
|
||||
return targetClnt.Put(targetBucket, targetObject, md5Hex, length)
|
||||
}
|
||||
|
||||
func getTargetWriters(targetURLParsers []*urlParser, md5Hex string, length int64) ([]io.WriteCloser, error) {
|
||||
func getTargetWriters(targetURLParsers []*parsedURL, md5Hex string, length int64) ([]io.WriteCloser, error) {
|
||||
var targetWriters []io.WriteCloser
|
||||
for _, targetURLParser := range targetURLParsers {
|
||||
writer, err := getTargetWriter(targetURLParser, md5Hex, length)
|
||||
@@ -72,14 +72,18 @@ func getTargetWriters(targetURLParsers []*urlParser, md5Hex string, length int64
|
||||
}
|
||||
|
||||
// doCopyCmd copies objects into and from a bucket or between buckets
|
||||
func doCopyCmd(ctx *cli.Context) {
|
||||
func runCopyCmd(ctx *cli.Context) {
|
||||
if len(ctx.Args()) < 2 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "cp", 1) // last argument is exit code
|
||||
}
|
||||
if ctx.Bool("recursive") {
|
||||
doCopyCmdRecursive(ctx)
|
||||
return
|
||||
} else {
|
||||
doCopyCmd(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func doCopyCmd(ctx *cli.Context) {
|
||||
// Convert arguments to URLs: expand alias, fix format...
|
||||
urlParsers, err := parseURLs(ctx)
|
||||
if err != nil {
|
||||
|
||||
@@ -78,7 +78,7 @@ func doListCmd(ctx *cli.Context) {
|
||||
}
|
||||
|
||||
// ListBuckets() will not be called for fsClient() as its not needed.
|
||||
if targetURLParser.bucketName == "" && targetURLParser.urlType != urlFile {
|
||||
if targetURLParser.bucketName == "" && targetURLParser.scheme != urlFile {
|
||||
buckets, err := client.ListBuckets()
|
||||
if err != nil {
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
|
||||
@@ -43,7 +43,7 @@ func doMakeBucketCmd(ctx *cli.Context) {
|
||||
// this is handled differently since http based URLs cannot have
|
||||
// nested directories as buckets, buckets are a unique alphanumeric
|
||||
// name having subdirectories is only supported for fsClient
|
||||
if targetURLParser.urlType != urlFile {
|
||||
if targetURLParser.scheme != urlFile {
|
||||
if !client.IsValidBucketName(targetURLParser.bucketName) {
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalf("mc: Invalid bucket name: %s", targetURLParser.bucketName)
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ var (
|
||||
Name: "cp",
|
||||
Usage: "Copy objects and files",
|
||||
// Description: "Copy files and objects recursively across object storage and filesystems",
|
||||
Action: doCopyCmd,
|
||||
Action: runCopyCmd,
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "recursive, r",
|
||||
|
||||
+2
-2
@@ -27,7 +27,7 @@ import (
|
||||
"github.com/minio-io/minio/pkg/utils/log"
|
||||
)
|
||||
|
||||
func getSourceObjectList(sourceClnt client.Client, sourceURLParser *urlParser) ([]*client.Item, error) {
|
||||
func getSourceObjectList(sourceClnt client.Client, sourceURLParser *parsedURL) ([]*client.Item, error) {
|
||||
sourceObjects, err := sourceClnt.ListObjects(sourceURLParser.bucketName, sourceURLParser.objectName)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
@@ -35,7 +35,7 @@ func getSourceObjectList(sourceClnt client.Client, sourceURLParser *urlParser) (
|
||||
return sourceObjects, nil
|
||||
}
|
||||
|
||||
func getRecursiveTargetWriter(targetURLParser *urlParser, targetObject, md5Hex string, length int64) (io.WriteCloser, error) {
|
||||
func getRecursiveTargetWriter(targetURLParser *parsedURL, targetObject, md5Hex string, length int64) (io.WriteCloser, error) {
|
||||
targetClnt, err := getNewClient(targetURLParser, globalDebugFlag)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, map[string]string{"failedURL": targetURLParser.String()})
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Modern Copy, (C) 2014, 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 . "github.com/minio-io/check"
|
||||
|
||||
type CmdTestSuite struct {
|
||||
}
|
||||
|
||||
var _ = Suite(&CmdTestSuite{})
|
||||
|
||||
func (s *CmdTestSuite) TestFileToS3Copy(c *C) {
|
||||
c.Skip("Not implemented")
|
||||
}
|
||||
@@ -18,13 +18,12 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/cheggaaa/pb"
|
||||
"github.com/minio-io/cli"
|
||||
"github.com/minio-io/mc/pkg/console"
|
||||
|
||||
+10
-10
@@ -35,9 +35,9 @@ const (
|
||||
urlFile // POSIX compatible file systems
|
||||
)
|
||||
|
||||
type urlParser struct {
|
||||
type parsedURL struct {
|
||||
url *url.URL
|
||||
urlType urlType
|
||||
scheme urlType
|
||||
bucketName string
|
||||
objectName string
|
||||
}
|
||||
@@ -91,23 +91,23 @@ func url2Object(u *url.URL) (bucketName, objectName string) {
|
||||
return bucketName, objectName
|
||||
}
|
||||
|
||||
func newURL(urlStr string) (*urlParser, error) {
|
||||
func newURL(urlStr string) (*parsedURL, error) {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
bucketName, objectName := url2Object(u)
|
||||
parsedURL := &urlParser{
|
||||
parsedURL := &parsedURL{
|
||||
url: u,
|
||||
urlType: getURLType(u.Scheme),
|
||||
scheme: getURLType(u.Scheme),
|
||||
bucketName: bucketName,
|
||||
objectName: objectName,
|
||||
}
|
||||
return parsedURL, nil
|
||||
}
|
||||
|
||||
func (u *urlParser) String() string {
|
||||
switch u.urlType {
|
||||
func (u *parsedURL) String() string {
|
||||
switch u.scheme {
|
||||
case urlFile:
|
||||
var p string
|
||||
switch runtime.GOOS {
|
||||
@@ -124,14 +124,14 @@ func (u *urlParser) String() string {
|
||||
}
|
||||
|
||||
// parseURL extracts URL string from a single cmd-line argument
|
||||
func parseURL(arg string, aliases map[string]string) (url *urlParser, err error) {
|
||||
func parseURL(arg string, aliases map[string]string) (url *parsedURL, err error) {
|
||||
// Check and expand Alias
|
||||
urlStr, err := aliasExpand(arg, aliases)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
u, err := newURL(urlStr)
|
||||
if u.urlType == urlUnknown {
|
||||
if u.scheme == urlUnknown {
|
||||
return nil, iodine.New(errUnsupportedScheme{scheme: urlUnknown}, nil)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -141,7 +141,7 @@ func parseURL(arg string, aliases map[string]string) (url *urlParser, err error)
|
||||
}
|
||||
|
||||
// parseURL extracts multiple URL strings from a single cmd-line argument
|
||||
func parseURLs(c *cli.Context) (urlParsers []*urlParser, err error) {
|
||||
func parseURLs(c *cli.Context) (urlParsers []*parsedURL, err error) {
|
||||
config, err := getMcConfig()
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
|
||||
Reference in New Issue
Block a user