1
0
mirror of https://github.com/minio/mc.git synced 2025-11-13 12:22:45 +03:00

Revert "unstruct client functions"

This commit is contained in:
Harshavardhana
2015-04-29 21:28:28 -07:00
parent 3ec4e85f92
commit 1ceed3dc20
10 changed files with 63 additions and 54 deletions

View File

@@ -65,4 +65,3 @@ install: test-all
clean: clean:
@rm -fv cover.out @rm -fv cover.out
@rm -fv mc

View File

@@ -26,17 +26,25 @@ import (
"github.com/minio-io/minio/pkg/iodine" "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 { type sourceReader struct {
reader io.ReadCloser reader io.ReadCloser
length int64 length int64
md5hex string md5hex string
} }
*/
// getSourceReader - // getSourceReader -
func getSourceReader(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length int64, md5hex string, err error) { func (methods mcClientMethods) getSourceReader(sourceURL string, sourceConfig *hostConfig) (
sourceClnt, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag) reader io.ReadCloser, length int64, md5hex string, err error) {
sourceClnt, err := methods.getNewClient(sourceURL, sourceConfig, globalDebugFlag)
if err != nil { if err != nil {
return nil, 0, "", iodine.New(err, map[string]string{"failedURL": sourceURL}) 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 - // getTargetWriter -
func getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, length int64) (io.WriteCloser, error) { func (methods mcClientMethods) getTargetWriter(targetURL string, targetConfig *hostConfig, md5hex string, length int64) (io.WriteCloser, error) {
targetClnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) targetClnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil { if err != nil {
return nil, iodine.New(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 // 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) t := client.GetType(urlStr)
switch t { switch t {
case client.Object: // Minio and S3 compatible object storage case client.Object: // Minio and S3 compatible object storage

View File

@@ -65,7 +65,7 @@ func runAccessCmd(ctx *cli.Context) {
targetURLConfigMap[targetURL] = targetConfig targetURLConfigMap[targetURL] = targetConfig
} }
for targetURL, targetConfig := range targetURLConfigMap { 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) err = iodine.New(err, nil)
if err != nil { if err != nil {
if errorMsg == "" { 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 err error
var clnt client.Client var clnt client.Client
clnt, err = getNewClient(targetURL, targetConfig, debug) clnt, err = methods.getNewClient(targetURL, targetConfig, debug)
if err != nil { if err != nil {
err := iodine.New(err, nil) err := iodine.New(err, nil)
msg := fmt.Sprintf("Unable to initialize client for [%s]. Reason: [%s].\n", msg := fmt.Sprintf("Unable to initialize client for [%s]. Reason: [%s].\n",

View File

@@ -59,16 +59,16 @@ func runCatCmd(ctx *cli.Context) {
log.Debug.Println(iodine.New(err, nil)) 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)) 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 { if err != nil {
log.Debug.Println(iodine.New(err, nil)) log.Debug.Println(iodine.New(err, nil))
console.Fatalln(humanReadable) 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 { for url, config := range sourceURLConfigMap {
sourceClnt, err := getNewClient(url, config, debug) sourceClnt, err := methods.getNewClient(url, config, debug)
if err != nil { if err != nil {
return "Unable to create client: " + url, iodine.New(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() defer reader.Close()
stdOutClnt, err := getNewClient(targetURL, &hostConfig{}, debug) stdOutClnt, err := methods.getNewClient(targetURL, &hostConfig{}, debug)
if err != nil { if err != nil {
return "Unable to create client: " + url, iodine.New(err, nil) return "Unable to create client: " + url, iodine.New(err, nil)
} }

View File

@@ -49,15 +49,16 @@ func runCopyCmd(ctx *cli.Context) {
} }
} }
methods := mcClientMethods{}
switch len(urls) { switch len(urls) {
case 2: case 2:
runCopyCmdSingleSource(urls) runCopyCmdSingleSource(methods, urls)
default: 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 sourceURLs := urls[:len(urls)-1] // All args are source except the last one
targetURL := urls[len(urls)-1] // Last one is target targetURL := urls[len(urls)-1] // Last one is target
targetConfig, err := getHostConfig(targetURL) 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", console.Fatalf("Unable to read host configuration for the source %s from config file [%s]. Reason: [%s].\n",
newRecursiveSourceURL, mustGetMcConfigPath(), iodine.ToError(err)) newRecursiveSourceURL, mustGetMcConfigPath(), iodine.ToError(err))
} }
err = doCopySingleSourceRecursive(newRecursiveSourceURL, targetURL, newRecursiveSourceConfig, targetConfig) err = doCopySingleSourceRecursive(methods, newRecursiveSourceURL, targetURL, newRecursiveSourceConfig, targetConfig)
if err != nil { if err != nil {
log.Debug.Println(err) log.Debug.Println(err)
console.Fatalf("Failed to copy from source %s to target %s. Reason: [%s].\n", newRecursiveSourceURL, 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", console.Fatalf("Unable to read host configuration for the following sources [%s] from config file [%s]. Reason: [%s].\n",
newRegularSourceURLs, mustGetMcConfigPath(), iodine.ToError(err)) newRegularSourceURLs, mustGetMcConfigPath(), iodine.ToError(err))
} }
err = doCopyMultipleSources(newRegularSourceURLConfigMap, targetURL, targetConfig) err = doCopyMultipleSources(methods, newRegularSourceURLConfigMap, targetURL, targetConfig)
if err != nil { if err != nil {
log.Debug.Println(err) log.Debug.Println(err)
console.Fatalf("Failed to copy from following sources [%s] to target %s. Reason: [%s].\n", 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] sourceURL := urls[0]
targetURL := urls[1] targetURL := urls[1]
targetConfig, err := getHostConfig(targetURL) targetConfig, err := getHostConfig(targetURL)
@@ -128,14 +129,14 @@ func runCopyCmdSingleSource(urls []string) {
sourceURL, mustGetMcConfigPath(), iodine.ToError(err)) sourceURL, mustGetMcConfigPath(), iodine.ToError(err))
} }
if recursive { if recursive {
err = doCopySingleSourceRecursive(sourceURL, targetURL, sourceConfig, targetConfig) err = doCopySingleSourceRecursive(methods, sourceURL, targetURL, sourceConfig, targetConfig)
if err != nil { if err != nil {
log.Debug.Println(err) log.Debug.Println(err)
console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", sourceURL, targetURL, iodine.ToError(err)) console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", sourceURL, targetURL, iodine.ToError(err))
} }
return return
} }
err = doCopySingleSource(sourceURL, targetURL, sourceConfig, targetConfig) err = doCopySingleSource(methods, sourceURL, targetURL, sourceConfig, targetConfig)
if err != nil { if err != nil {
log.Debug.Println(err) log.Debug.Println(err)
console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", sourceURL, targetURL, iodine.ToError(err)) console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", sourceURL, targetURL, iodine.ToError(err))

View File

@@ -62,14 +62,14 @@ func runListCmd(ctx *cli.Context) {
if isURLRecursive(targetURL) { if isURLRecursive(targetURL) {
// if recursive strip off the "..." // if recursive strip off the "..."
targetURL = strings.TrimSuffix(targetURL, recursiveSeparator) targetURL = strings.TrimSuffix(targetURL, recursiveSeparator)
err = doListRecursiveCmd(targetURL, targetConfig, globalDebugFlag) err = doListRecursiveCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag)
err = iodine.New(err, nil) err = iodine.New(err, nil)
if err != nil { if err != nil {
log.Debug.Println(err) log.Debug.Println(err)
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err)) console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
} }
} else { } else {
err = doListCmd(targetURL, targetConfig, globalDebugFlag) err = doListCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag)
if err != nil { if err != nil {
if err != nil { if err != nil {
log.Debug.Println(err) log.Debug.Println(err)
@@ -81,8 +81,8 @@ func runListCmd(ctx *cli.Context) {
} }
// doListCmd - // doListCmd -
func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error { func doListCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error {
clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil { if err != nil {
return iodine.New(err, map[string]string{"Target": targetURL}) return iodine.New(err, map[string]string{"Target": targetURL})
} }
@@ -100,8 +100,8 @@ func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error {
} }
// doListRecursiveCmd - // doListRecursiveCmd -
func doListRecursiveCmd(targetURL string, targetConfig *hostConfig, debug bool) error { func doListRecursiveCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error {
clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag) clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil { if err != nil {
return iodine.New(err, map[string]string{"Target": targetURL}) return iodine.New(err, map[string]string{"Target": targetURL})
} }

View File

@@ -58,7 +58,7 @@ func runMakeBucketCmd(ctx *cli.Context) {
targetURLConfigMap[targetURL] = targetConfig targetURLConfigMap[targetURL] = targetConfig
} }
for targetURL, targetConfig := range targetURLConfigMap { for targetURL, targetConfig := range targetURLConfigMap {
errorMsg, err := doMakeBucketCmd(targetURL, targetConfig, globalDebugFlag) errorMsg, err := doMakeBucketCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag)
err = iodine.New(err, nil) err = iodine.New(err, nil)
if err != nil { if err != nil {
if errorMsg == "" { if errorMsg == "" {
@@ -71,10 +71,10 @@ func runMakeBucketCmd(ctx *cli.Context) {
} }
// doMakeBucketCmd - // 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 err error
var clnt client.Client var clnt client.Client
clnt, err = getNewClient(targetURL, targetConfig, debug) clnt, err = methods.getNewClient(targetURL, targetConfig, debug)
if err != nil { if err != nil {
err := iodine.New(err, nil) err := iodine.New(err, nil)
msg := fmt.Sprintf("Unable to initialize client for [%s]. Reason: [%s].\n", msg := fmt.Sprintf("Unable to initialize client for [%s]. Reason: [%s].\n",

View File

@@ -47,10 +47,11 @@ func runSyncCmd(ctx *cli.Context) {
console.Fatalf("Unable to parse arguments. Reason: [%s].\n", e) 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 sourceURL := urls[0] // first arg is source
targetURLs := urls[1:] // all other are targets targetURLs := urls[1:] // all other are targets
@@ -73,7 +74,7 @@ func runCopyCmdSingleSourceMultipleTargets(urls []string) {
} }
for targetURL, targetConfig := range targetURLConfigMap { for targetURL, targetConfig := range targetURLConfigMap {
err = doCopySingleSourceRecursive(sourceURL, targetURL, sourceConfig, targetConfig) err = doCopySingleSourceRecursive(methods, sourceURL, targetURL, sourceConfig, targetConfig)
if err != nil { if err != nil {
log.Debug.Println(err) log.Debug.Println(err)
console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n", console.Fatalf("Failed to copy from source [%s] to target %s. Reason: [%s].\n",

View File

@@ -22,17 +22,17 @@ import (
"github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/iodine"
) )
type sourceReader struct { // cpMethods - methods only valid for cp
reader io.ReadCloser type cpMethods interface {
length int64 getSourceReader(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length int64, md5hex string, err error)
md5hex string getTargetWriter(targetURL string, targetConfig *hostConfig, md5Hex string, length int64) (io.WriteCloser, error)
} }
// getSourceReaders - // 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) sourceURLReaderMap := make(map[string]sourceReader)
for sourceURL, sourceConfig := range sourceURLConfigMap { for sourceURL, sourceConfig := range sourceURLConfigMap {
reader, length, md5hex, err := getSourceReader(sourceURL, sourceConfig) reader, length, md5hex, err := methods.getSourceReader(sourceURL, sourceConfig)
if err != nil { if err != nil {
for _, sourceReader := range sourceURLReaderMap { for _, sourceReader := range sourceURLReaderMap {
sourceReader.reader.Close() sourceReader.reader.Close()

26
cp.go
View File

@@ -27,8 +27,8 @@ import (
/// mc cp - related internal functions /// mc cp - related internal functions
// doCopy // doCopy
func doCopy(reader io.ReadCloser, md5hex string, length int64, targetURL string, targetConfig *hostConfig) error { func doCopy(methods clientMethods, reader io.ReadCloser, md5hex string, length int64, targetURL string, targetConfig *hostConfig) error {
writeCloser, err := getTargetWriter(targetURL, targetConfig, md5hex, length) writeCloser, err := methods.getTargetWriter(targetURL, targetConfig, md5hex, length)
if err != nil { if err != nil {
return iodine.New(err, nil) return iodine.New(err, nil)
} }
@@ -60,8 +60,8 @@ func doCopy(reader io.ReadCloser, md5hex string, length int64, targetURL string,
} }
// doCopySingleSource // doCopySingleSource
func doCopySingleSource(sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { func doCopySingleSource(methods clientMethods, sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error {
reader, length, md5hex, err := getSourceReader(sourceURL, sourceConfig) reader, length, md5hex, err := methods.getSourceReader(sourceURL, sourceConfig)
if err != nil { if err != nil {
return iodine.New(err, nil) return iodine.New(err, nil)
} }
@@ -69,19 +69,19 @@ func doCopySingleSource(sourceURL, targetURL string, sourceConfig, targetConfig
newTargetURL, err := getNewTargetURL(targetURL, sourceURL) newTargetURL, err := getNewTargetURL(targetURL, sourceURL)
switch iodine.ToError(err).(type) { switch iodine.ToError(err).(type) {
case errIsNotFolder: case errIsNotFolder:
return doCopy(reader, md5hex, length, targetURL, targetConfig) return doCopy(methods, reader, md5hex, length, targetURL, targetConfig)
case errIsNotBucket: case errIsNotBucket:
return doCopy(reader, md5hex, length, targetURL, targetConfig) return doCopy(methods, reader, md5hex, length, targetURL, targetConfig)
case nil: case nil:
return doCopy(reader, md5hex, length, newTargetURL, targetConfig) return doCopy(methods, reader, md5hex, length, newTargetURL, targetConfig)
default: default:
return iodine.New(err, nil) return iodine.New(err, nil)
} }
} }
// doCopySingleSourceRecursive // doCopySingleSourceRecursive
func doCopySingleSourceRecursive(sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error { func doCopySingleSourceRecursive(methods clientMethods, sourceURL, targetURL string, sourceConfig, targetConfig *hostConfig) error {
sourceClnt, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag) sourceClnt, err := methods.getNewClient(sourceURL, sourceConfig, globalDebugFlag)
if err != nil { if err != nil {
return iodine.New(err, nil) return iodine.New(err, nil)
} }
@@ -90,7 +90,7 @@ func doCopySingleSourceRecursive(sourceURL, targetURL string, sourceConfig, targ
continue continue
} }
newSourceURL, newTargetURL := getNewURLRecursive(sourceURL, targetURL, itemCh.Item.Name) 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 // verify for directory related errors, if "open" failed on directories ignore those errors
switch e := iodine.ToError(err).(type) { switch e := iodine.ToError(err).(type) {
case *os.PathError: case *os.PathError:
@@ -111,8 +111,8 @@ func doCopySingleSourceRecursive(sourceURL, targetURL string, sourceConfig, targ
} }
// doCopyMultipleSources - // doCopyMultipleSources -
func doCopyMultipleSources(sourceURLConfigMap map[string]*hostConfig, targetURL string, targetConfig *hostConfig) error { func doCopyMultipleSources(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, targetURL string, targetConfig *hostConfig) error {
sourceURLReaderMap, err := getSourceReaders(sourceURLConfigMap) sourceURLReaderMap, err := getSourceReaders(methods, sourceURLConfigMap)
if err != nil { if err != nil {
return iodine.New(err, nil) return iodine.New(err, nil)
} }
@@ -121,7 +121,7 @@ func doCopyMultipleSources(sourceURLConfigMap map[string]*hostConfig, targetURL
if err != nil { if err != nil {
return iodine.New(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 { if err != nil {
return iodine.New(err, map[string]string{"Source": sourceURL}) return iodine.New(err, map[string]string{"Source": sourceURL})
} }