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:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
17
cmd-cp.go
17
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))
|
||||
|
||||
12
cmd-ls.go
12
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})
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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()
|
||||
|
||||
26
cp.go
26
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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user