From 1ceed3dc206090420411f4904440cea2376a792c Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 29 Apr 2015 21:28:28 -0700 Subject: [PATCH] Revert "unstruct client functions" --- Makefile | 1 - clientmethods.go | 22 +++++++++++++++------- cmd-access.go | 6 +++--- cmd-cat.go | 8 ++++---- cmd-cp.go | 17 +++++++++-------- cmd-ls.go | 12 ++++++------ cmd-mb.go | 6 +++--- cmd-sync.go | 7 ++++--- cp-methods.go | 12 ++++++------ cp.go | 26 +++++++++++++------------- 10 files changed, 63 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index 0483246b..89e7355d 100644 --- a/Makefile +++ b/Makefile @@ -65,4 +65,3 @@ install: test-all clean: @rm -fv cover.out - @rm -fv mc diff --git a/clientmethods.go b/clientmethods.go index 7c76152b..877b0513 100644 --- a/clientmethods.go +++ b/clientmethods.go @@ -26,17 +26,25 @@ import ( "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 getSourceReader(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length int64, md5hex string, err error) { - sourceClnt, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag) +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}) } @@ -47,8 +55,8 @@ func getSourceReader(sourceURL string, sourceConfig *hostConfig) (reader io.Read } // getTargetWriter - -func getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, length int64) (io.WriteCloser, error) { - targetClnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) +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) } @@ -56,7 +64,7 @@ func getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, } // getNewClient gives a new client interface -func getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Client, err error) { +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 diff --git a/cmd-access.go b/cmd-access.go index 2c75b2d7..1166b11a 100644 --- a/cmd-access.go +++ b/cmd-access.go @@ -65,7 +65,7 @@ func runAccessCmd(ctx *cli.Context) { targetURLConfigMap[targetURL] = targetConfig } for targetURL, targetConfig := range targetURLConfigMap { - errorMsg, err := doUpdateAccessCmd(targetURL, acl.String(), targetConfig, globalDebugFlag) + errorMsg, err := doUpdateAccessCmd(mcClientMethods{}, targetURL, acl.String(), targetConfig, globalDebugFlag) err = iodine.New(err, nil) if err != nil { if errorMsg == "" { @@ -77,10 +77,10 @@ func runAccessCmd(ctx *cli.Context) { } } -func doUpdateAccessCmd(targetURL, targetACL string, targetConfig *hostConfig, debug bool) (string, error) { +func doUpdateAccessCmd(methods clientMethods, targetURL, targetACL string, targetConfig *hostConfig, debug bool) (string, error) { var err error var clnt client.Client - clnt, err = getNewClient(targetURL, targetConfig, debug) + clnt, err = methods.getNewClient(targetURL, targetConfig, debug) if err != nil { err := iodine.New(err, nil) msg := fmt.Sprintf("Unable to initialize client for [%s]. Reason: [%s].\n", diff --git a/cmd-cat.go b/cmd-cat.go index 1d98eb2e..e7bdb28e 100644 --- a/cmd-cat.go +++ b/cmd-cat.go @@ -59,16 +59,16 @@ func runCatCmd(ctx *cli.Context) { log.Debug.Println(iodine.New(err, nil)) console.Fatalf("reading host config for URL [%s] failed with following reason: [%s]\n", sourceURLs, iodine.ToError(err)) } - humanReadable, err := doCatCmd(sourceURLConfigMap, standardOutput, globalDebugFlag) + humanReadable, err := doCatCmd(mcClientMethods{}, sourceURLConfigMap, standardOutput, globalDebugFlag) if err != nil { log.Debug.Println(iodine.New(err, nil)) console.Fatalln(humanReadable) } } -func doCatCmd(sourceURLConfigMap map[string]*hostConfig, targetURL string, debug bool) (string, error) { +func doCatCmd(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, targetURL string, debug bool) (string, error) { for url, config := range sourceURLConfigMap { - sourceClnt, err := getNewClient(url, config, debug) + sourceClnt, err := methods.getNewClient(url, config, debug) if err != nil { return "Unable to create client: " + url, iodine.New(err, nil) } @@ -78,7 +78,7 @@ func doCatCmd(sourceURLConfigMap map[string]*hostConfig, targetURL string, debug } defer reader.Close() - stdOutClnt, err := getNewClient(targetURL, &hostConfig{}, debug) + stdOutClnt, err := methods.getNewClient(targetURL, &hostConfig{}, debug) if err != nil { return "Unable to create client: " + url, iodine.New(err, nil) } diff --git a/cmd-cp.go b/cmd-cp.go index bb50e100..cca3a5ba 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -49,15 +49,16 @@ func runCopyCmd(ctx *cli.Context) { } } + methods := mcClientMethods{} switch len(urls) { case 2: - runCopyCmdSingleSource(urls) + runCopyCmdSingleSource(methods, urls) default: - runCopyCmdMultipleSources(urls) + runCopyCmdMultipleSources(methods, urls) } } -func runCopyCmdMultipleSources(urls []string) { +func runCopyCmdMultipleSources(methods clientMethods, urls []string) { sourceURLs := urls[:len(urls)-1] // All args are source except the last one targetURL := urls[len(urls)-1] // Last one is target targetConfig, err := getHostConfig(targetURL) @@ -85,7 +86,7 @@ func runCopyCmdMultipleSources(urls []string) { console.Fatalf("Unable to read host configuration for the source %s from config file [%s]. Reason: [%s].\n", newRecursiveSourceURL, mustGetMcConfigPath(), iodine.ToError(err)) } - err = doCopySingleSourceRecursive(newRecursiveSourceURL, targetURL, newRecursiveSourceConfig, targetConfig) + err = doCopySingleSourceRecursive(methods, newRecursiveSourceURL, targetURL, newRecursiveSourceConfig, targetConfig) if err != nil { log.Debug.Println(err) console.Fatalf("Failed to copy from source %s to target %s. Reason: [%s].\n", newRecursiveSourceURL, @@ -98,7 +99,7 @@ func runCopyCmdMultipleSources(urls []string) { console.Fatalf("Unable to read host configuration for the following sources [%s] from config file [%s]. Reason: [%s].\n", newRegularSourceURLs, mustGetMcConfigPath(), iodine.ToError(err)) } - err = doCopyMultipleSources(newRegularSourceURLConfigMap, targetURL, targetConfig) + err = doCopyMultipleSources(methods, newRegularSourceURLConfigMap, targetURL, targetConfig) if err != nil { log.Debug.Println(err) console.Fatalf("Failed to copy from following sources [%s] to target %s. Reason: [%s].\n", @@ -107,7 +108,7 @@ func runCopyCmdMultipleSources(urls []string) { } -func runCopyCmdSingleSource(urls []string) { +func runCopyCmdSingleSource(methods clientMethods, urls []string) { sourceURL := urls[0] targetURL := urls[1] targetConfig, err := getHostConfig(targetURL) @@ -128,14 +129,14 @@ func runCopyCmdSingleSource(urls []string) { sourceURL, mustGetMcConfigPath(), iodine.ToError(err)) } if recursive { - err = doCopySingleSourceRecursive(sourceURL, targetURL, sourceConfig, targetConfig) + err = doCopySingleSourceRecursive(methods, sourceURL, targetURL, sourceConfig, targetConfig) if err != nil { log.Debug.Println(err) console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", sourceURL, targetURL, iodine.ToError(err)) } return } - err = doCopySingleSource(sourceURL, targetURL, sourceConfig, targetConfig) + err = doCopySingleSource(methods, sourceURL, targetURL, sourceConfig, targetConfig) if err != nil { log.Debug.Println(err) console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", sourceURL, targetURL, iodine.ToError(err)) diff --git a/cmd-ls.go b/cmd-ls.go index 0ce127a1..7f50cdbc 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -62,14 +62,14 @@ func runListCmd(ctx *cli.Context) { if isURLRecursive(targetURL) { // if recursive strip off the "..." targetURL = strings.TrimSuffix(targetURL, recursiveSeparator) - err = doListRecursiveCmd(targetURL, targetConfig, globalDebugFlag) + err = doListRecursiveCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag) err = iodine.New(err, nil) if err != nil { log.Debug.Println(err) console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err)) } } else { - err = doListCmd(targetURL, targetConfig, globalDebugFlag) + err = doListCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag) if err != nil { if err != nil { log.Debug.Println(err) @@ -81,8 +81,8 @@ func runListCmd(ctx *cli.Context) { } // doListCmd - -func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error { - clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) +func doListCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error { + clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag) if err != nil { return iodine.New(err, map[string]string{"Target": targetURL}) } @@ -100,8 +100,8 @@ func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error { } // doListRecursiveCmd - -func doListRecursiveCmd(targetURL string, targetConfig *hostConfig, debug bool) error { - clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) +func doListRecursiveCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error { + clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag) if err != nil { return iodine.New(err, map[string]string{"Target": targetURL}) } diff --git a/cmd-mb.go b/cmd-mb.go index e29ad4f0..a6900b51 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -58,7 +58,7 @@ func runMakeBucketCmd(ctx *cli.Context) { targetURLConfigMap[targetURL] = targetConfig } for targetURL, targetConfig := range targetURLConfigMap { - errorMsg, err := doMakeBucketCmd(targetURL, targetConfig, globalDebugFlag) + errorMsg, err := doMakeBucketCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag) err = iodine.New(err, nil) if err != nil { if errorMsg == "" { @@ -71,10 +71,10 @@ func runMakeBucketCmd(ctx *cli.Context) { } // doMakeBucketCmd - -func doMakeBucketCmd(targetURL string, targetConfig *hostConfig, debug bool) (string, error) { +func doMakeBucketCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) (string, error) { var err error var clnt client.Client - clnt, err = getNewClient(targetURL, targetConfig, debug) + clnt, err = methods.getNewClient(targetURL, targetConfig, debug) if err != nil { err := iodine.New(err, nil) msg := fmt.Sprintf("Unable to initialize client for [%s]. Reason: [%s].\n", diff --git a/cmd-sync.go b/cmd-sync.go index 90a7300d..dfac6f8e 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -47,10 +47,11 @@ func runSyncCmd(ctx *cli.Context) { console.Fatalf("Unable to parse arguments. Reason: [%s].\n", e) } } - runCopyCmdSingleSourceMultipleTargets(urls) + methods := mcClientMethods{} + runCopyCmdSingleSourceMultipleTargets(methods, urls) } -func runCopyCmdSingleSourceMultipleTargets(urls []string) { +func runCopyCmdSingleSourceMultipleTargets(methods clientMethods, urls []string) { sourceURL := urls[0] // first arg is source targetURLs := urls[1:] // all other are targets @@ -73,7 +74,7 @@ func runCopyCmdSingleSourceMultipleTargets(urls []string) { } for targetURL, targetConfig := range targetURLConfigMap { - err = doCopySingleSourceRecursive(sourceURL, targetURL, sourceConfig, targetConfig) + err = doCopySingleSourceRecursive(methods, sourceURL, targetURL, sourceConfig, targetConfig) if err != nil { log.Debug.Println(err) console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", diff --git a/cp-methods.go b/cp-methods.go index 5f8f71f2..e518620b 100644 --- a/cp-methods.go +++ b/cp-methods.go @@ -22,17 +22,17 @@ import ( "github.com/minio-io/minio/pkg/iodine" ) -type sourceReader struct { - reader io.ReadCloser - length int64 - md5hex string +// 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(sourceURLConfigMap map[string]*hostConfig) (map[string]sourceReader, error) { +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 := getSourceReader(sourceURL, sourceConfig) + reader, length, md5hex, err := methods.getSourceReader(sourceURL, sourceConfig) if err != nil { for _, sourceReader := range sourceURLReaderMap { sourceReader.reader.Close() diff --git a/cp.go b/cp.go index 1b26dba2..af37d01b 100644 --- a/cp.go +++ b/cp.go @@ -27,8 +27,8 @@ import ( /// mc cp - related internal functions // doCopy -func doCopy(reader io.ReadCloser, md5hex string, length int64, targetURL string, targetConfig *hostConfig) error { - writeCloser, err := getTargetWriter(targetURL, targetConfig, md5hex, length) +func doCopy(methods clientMethods, reader io.ReadCloser, md5hex string, length int64, targetURL string, targetConfig *hostConfig) error { + writeCloser, err := methods.getTargetWriter(targetURL, targetConfig, md5hex, length) if err != nil { return iodine.New(err, nil) } @@ -60,8 +60,8 @@ func doCopy(reader io.ReadCloser, md5hex string, length int64, targetURL string, } // doCopySingleSource -func doCopySingleSource(sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { - reader, length, md5hex, err := getSourceReader(sourceURL, sourceConfig) +func doCopySingleSource(methods clientMethods, sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { + reader, length, md5hex, err := methods.getSourceReader(sourceURL, sourceConfig) if err != nil { return iodine.New(err, nil) } @@ -69,19 +69,19 @@ func doCopySingleSource(sourceURL, targetURL string, sourceConfig, targetConfig newTargetURL, err := getNewTargetURL(targetURL, sourceURL) switch iodine.ToError(err).(type) { case errIsNotFolder: - return doCopy(reader, md5hex, length, targetURL, targetConfig) + return doCopy(methods, reader, md5hex, length, targetURL, targetConfig) case errIsNotBucket: - return doCopy(reader, md5hex, length, targetURL, targetConfig) + return doCopy(methods, reader, md5hex, length, targetURL, targetConfig) case nil: - return doCopy(reader, md5hex, length, newTargetURL, targetConfig) + return doCopy(methods, reader, md5hex, length, newTargetURL, targetConfig) default: return iodine.New(err, nil) } } // doCopySingleSourceRecursive -func doCopySingleSourceRecursive(sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { - sourceClnt, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag) +func doCopySingleSourceRecursive(methods clientMethods, sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { + sourceClnt, err := methods.getNewClient(sourceURL, sourceConfig, globalDebugFlag) if err != nil { return iodine.New(err, nil) } @@ -90,7 +90,7 @@ func doCopySingleSourceRecursive(sourceURL, targetURL string, sourceConfig, targ continue } newSourceURL, newTargetURL := getNewURLRecursive(sourceURL, targetURL, itemCh.Item.Name) - if err := doCopySingleSource(newSourceURL, newTargetURL, sourceConfig, targetConfig); err != nil { + if err := doCopySingleSource(methods, newSourceURL, newTargetURL, sourceConfig, targetConfig); err != nil { // verify for directory related errors, if "open" failed on directories ignore those errors switch e := iodine.ToError(err).(type) { case *os.PathError: @@ -111,8 +111,8 @@ func doCopySingleSourceRecursive(sourceURL, targetURL string, sourceConfig, targ } // doCopyMultipleSources - -func doCopyMultipleSources(sourceURLConfigMap map[string]*hostConfig, targetURL string, targetConfig *hostConfig) error { - sourceURLReaderMap, err := getSourceReaders(sourceURLConfigMap) +func doCopyMultipleSources(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, targetURL string, targetConfig *hostConfig) error { + sourceURLReaderMap, err := getSourceReaders(methods, sourceURLConfigMap) if err != nil { return iodine.New(err, nil) } @@ -121,7 +121,7 @@ func doCopyMultipleSources(sourceURLConfigMap map[string]*hostConfig, targetURL if err != nil { return iodine.New(err, nil) } - err = doCopy(sourceReader.reader, sourceReader.md5hex, sourceReader.length, newTargetURL, targetConfig) + err = doCopy(methods, sourceReader.reader, sourceReader.md5hex, sourceReader.length, newTargetURL, targetConfig) if err != nil { return iodine.New(err, map[string]string{"Source": sourceURL}) }