From 0d191ceb834a1df5bf30adb2a5a3b71ed7dfa2f7 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Wed, 29 Apr 2015 19:06:42 -0700 Subject: [PATCH] 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, 54 insertions(+), 63 deletions(-) diff --git a/Makefile b/Makefile index 89e7355d..0483246b 100644 --- a/Makefile +++ b/Makefile @@ -65,3 +65,4 @@ install: test-all clean: @rm -fv cover.out + @rm -fv mc diff --git a/clientmethods.go b/clientmethods.go index 877b0513..7c76152b 100644 --- a/clientmethods.go +++ b/clientmethods.go @@ -26,25 +26,17 @@ 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 (methods mcClientMethods) getSourceReader(sourceURL string, sourceConfig *hostConfig) ( - reader io.ReadCloser, length int64, md5hex string, err error) { - - sourceClnt, err := methods.getNewClient(sourceURL, sourceConfig, globalDebugFlag) +func getSourceReader(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length int64, md5hex string, err error) { + sourceClnt, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag) if err != nil { return nil, 0, "", iodine.New(err, map[string]string{"failedURL": sourceURL}) } @@ -55,8 +47,8 @@ func (methods mcClientMethods) getSourceReader(sourceURL string, sourceConfig *h } // getTargetWriter - -func (methods mcClientMethods) getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, length int64) (io.WriteCloser, error) { - targetClnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag) +func getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, length int64) (io.WriteCloser, error) { + targetClnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) if err != nil { return nil, iodine.New(err, nil) } @@ -64,7 +56,7 @@ func (methods mcClientMethods) getTargetWriter(targetURL string, targetConfig *h } // getNewClient gives a new client interface -func (methods mcClientMethods) getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Client, err error) { +func 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 1166b11a..2c75b2d7 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(mcClientMethods{}, targetURL, acl.String(), targetConfig, globalDebugFlag) + errorMsg, err := doUpdateAccessCmd(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(methods clientMethods, targetURL, targetACL string, targetConfig *hostConfig, debug bool) (string, error) { +func doUpdateAccessCmd(targetURL, targetACL string, targetConfig *hostConfig, debug bool) (string, error) { var err error var clnt client.Client - clnt, err = methods.getNewClient(targetURL, targetConfig, debug) + clnt, err = 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 e7bdb28e..1d98eb2e 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(mcClientMethods{}, sourceURLConfigMap, standardOutput, globalDebugFlag) + humanReadable, err := doCatCmd(sourceURLConfigMap, standardOutput, globalDebugFlag) if err != nil { log.Debug.Println(iodine.New(err, nil)) console.Fatalln(humanReadable) } } -func doCatCmd(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, targetURL string, debug bool) (string, error) { +func doCatCmd(sourceURLConfigMap map[string]*hostConfig, targetURL string, debug bool) (string, error) { for url, config := range sourceURLConfigMap { - sourceClnt, err := methods.getNewClient(url, config, debug) + sourceClnt, err := getNewClient(url, config, debug) if err != nil { return "Unable to create client: " + url, iodine.New(err, nil) } @@ -78,7 +78,7 @@ func doCatCmd(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, } defer reader.Close() - stdOutClnt, err := methods.getNewClient(targetURL, &hostConfig{}, debug) + stdOutClnt, err := 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 cca3a5ba..bb50e100 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -49,16 +49,15 @@ func runCopyCmd(ctx *cli.Context) { } } - methods := mcClientMethods{} switch len(urls) { case 2: - runCopyCmdSingleSource(methods, urls) + runCopyCmdSingleSource(urls) default: - runCopyCmdMultipleSources(methods, urls) + runCopyCmdMultipleSources(urls) } } -func runCopyCmdMultipleSources(methods clientMethods, urls []string) { +func runCopyCmdMultipleSources(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) @@ -86,7 +85,7 @@ func runCopyCmdMultipleSources(methods clientMethods, 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(methods, newRecursiveSourceURL, targetURL, newRecursiveSourceConfig, targetConfig) + err = doCopySingleSourceRecursive(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, @@ -99,7 +98,7 @@ func runCopyCmdMultipleSources(methods clientMethods, 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(methods, newRegularSourceURLConfigMap, targetURL, targetConfig) + err = doCopyMultipleSources(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", @@ -108,7 +107,7 @@ func runCopyCmdMultipleSources(methods clientMethods, urls []string) { } -func runCopyCmdSingleSource(methods clientMethods, urls []string) { +func runCopyCmdSingleSource(urls []string) { sourceURL := urls[0] targetURL := urls[1] targetConfig, err := getHostConfig(targetURL) @@ -129,14 +128,14 @@ func runCopyCmdSingleSource(methods clientMethods, urls []string) { sourceURL, mustGetMcConfigPath(), iodine.ToError(err)) } if recursive { - err = doCopySingleSourceRecursive(methods, sourceURL, targetURL, sourceConfig, targetConfig) + err = doCopySingleSourceRecursive(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(methods, sourceURL, targetURL, sourceConfig, targetConfig) + err = doCopySingleSource(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 7f50cdbc..0ce127a1 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(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag) + err = doListRecursiveCmd(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(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag) + err = doListCmd(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(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error { - clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag) +func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error { + clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) if err != nil { return iodine.New(err, map[string]string{"Target": targetURL}) } @@ -100,8 +100,8 @@ func doListCmd(methods clientMethods, targetURL string, targetConfig *hostConfig } // doListRecursiveCmd - -func doListRecursiveCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error { - clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag) +func doListRecursiveCmd(targetURL string, targetConfig *hostConfig, debug bool) error { + clnt, err := 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 a6900b51..e29ad4f0 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(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag) + errorMsg, err := doMakeBucketCmd(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(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) (string, error) { +func doMakeBucketCmd(targetURL string, targetConfig *hostConfig, debug bool) (string, error) { var err error var clnt client.Client - clnt, err = methods.getNewClient(targetURL, targetConfig, debug) + clnt, err = 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 dfac6f8e..90a7300d 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -47,11 +47,10 @@ func runSyncCmd(ctx *cli.Context) { console.Fatalf("Unable to parse arguments. Reason: [%s].\n", e) } } - methods := mcClientMethods{} - runCopyCmdSingleSourceMultipleTargets(methods, urls) + runCopyCmdSingleSourceMultipleTargets(urls) } -func runCopyCmdSingleSourceMultipleTargets(methods clientMethods, urls []string) { +func runCopyCmdSingleSourceMultipleTargets(urls []string) { sourceURL := urls[0] // first arg is source targetURLs := urls[1:] // all other are targets @@ -74,7 +73,7 @@ func runCopyCmdSingleSourceMultipleTargets(methods clientMethods, urls []string) } for targetURL, targetConfig := range targetURLConfigMap { - err = doCopySingleSourceRecursive(methods, sourceURL, targetURL, sourceConfig, targetConfig) + err = doCopySingleSourceRecursive(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 e518620b..5f8f71f2 100644 --- a/cp-methods.go +++ b/cp-methods.go @@ -22,17 +22,17 @@ import ( "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) +type sourceReader struct { + reader io.ReadCloser + length int64 + md5hex string } // getSourceReaders - -func getSourceReaders(methods cpMethods, sourceURLConfigMap map[string]*hostConfig) (map[string]sourceReader, error) { +func getSourceReaders(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) + reader, length, md5hex, err := getSourceReader(sourceURL, sourceConfig) if err != nil { for _, sourceReader := range sourceURLReaderMap { sourceReader.reader.Close() diff --git a/cp.go b/cp.go index af37d01b..1b26dba2 100644 --- a/cp.go +++ b/cp.go @@ -27,8 +27,8 @@ import ( /// mc cp - related internal functions // doCopy -func doCopy(methods clientMethods, reader io.ReadCloser, md5hex string, length int64, targetURL string, targetConfig *hostConfig) error { - writeCloser, err := methods.getTargetWriter(targetURL, targetConfig, md5hex, length) +func doCopy(reader io.ReadCloser, md5hex string, length int64, targetURL string, targetConfig *hostConfig) error { + writeCloser, err := getTargetWriter(targetURL, targetConfig, md5hex, length) if err != nil { return iodine.New(err, nil) } @@ -60,8 +60,8 @@ func doCopy(methods clientMethods, reader io.ReadCloser, md5hex string, length i } // doCopySingleSource -func doCopySingleSource(methods clientMethods, sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { - reader, length, md5hex, err := methods.getSourceReader(sourceURL, sourceConfig) +func doCopySingleSource(sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { + reader, length, md5hex, err := getSourceReader(sourceURL, sourceConfig) if err != nil { return iodine.New(err, nil) } @@ -69,19 +69,19 @@ func doCopySingleSource(methods clientMethods, sourceURL, targetURL string, sour newTargetURL, err := getNewTargetURL(targetURL, sourceURL) switch iodine.ToError(err).(type) { case errIsNotFolder: - return doCopy(methods, reader, md5hex, length, targetURL, targetConfig) + return doCopy(reader, md5hex, length, targetURL, targetConfig) case errIsNotBucket: - return doCopy(methods, reader, md5hex, length, targetURL, targetConfig) + return doCopy(reader, md5hex, length, targetURL, targetConfig) case nil: - return doCopy(methods, reader, md5hex, length, newTargetURL, targetConfig) + return doCopy(reader, md5hex, length, newTargetURL, targetConfig) default: return iodine.New(err, nil) } } // doCopySingleSourceRecursive -func doCopySingleSourceRecursive(methods clientMethods, sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { - sourceClnt, err := methods.getNewClient(sourceURL, sourceConfig, globalDebugFlag) +func doCopySingleSourceRecursive(sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { + sourceClnt, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag) if err != nil { return iodine.New(err, nil) } @@ -90,7 +90,7 @@ func doCopySingleSourceRecursive(methods clientMethods, sourceURL, targetURL str continue } newSourceURL, newTargetURL := getNewURLRecursive(sourceURL, targetURL, itemCh.Item.Name) - if err := doCopySingleSource(methods, newSourceURL, newTargetURL, sourceConfig, targetConfig); err != nil { + if err := doCopySingleSource(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(methods clientMethods, sourceURL, targetURL str } // doCopyMultipleSources - -func doCopyMultipleSources(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, targetURL string, targetConfig *hostConfig) error { - sourceURLReaderMap, err := getSourceReaders(methods, sourceURLConfigMap) +func doCopyMultipleSources(sourceURLConfigMap map[string]*hostConfig, targetURL string, targetConfig *hostConfig) error { + sourceURLReaderMap, err := getSourceReaders(sourceURLConfigMap) if err != nil { return iodine.New(err, nil) } @@ -121,7 +121,7 @@ func doCopyMultipleSources(methods clientMethods, sourceURLConfigMap map[string] if err != nil { return iodine.New(err, nil) } - err = doCopy(methods, sourceReader.reader, sourceReader.md5hex, sourceReader.length, newTargetURL, targetConfig) + err = doCopy(sourceReader.reader, sourceReader.md5hex, sourceReader.length, newTargetURL, targetConfig) if err != nil { return iodine.New(err, map[string]string{"Source": sourceURL}) }