Move major url handling into client as part of #425
This commit is contained in:
+6
-6
@@ -61,7 +61,7 @@ func (manager mcClientManager) getSourceReader(urlStr string) (reader io.ReadClo
|
||||
return nil, 0, "", iodine.New(err, map[string]string{"sourceURL": urlStr})
|
||||
}
|
||||
// Get a reader for the source object
|
||||
bucket, object, err := url2Object(urlStr)
|
||||
bucket, object, err := client.URL2Object(urlStr)
|
||||
if err != nil {
|
||||
return nil, 0, "", iodine.New(err, map[string]string{"sourceURL": urlStr})
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func (manager mcClientManager) getTargetWriter(urlStr string, md5Hex string, len
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
|
||||
bucket, object, err := url2Object(urlStr)
|
||||
bucket, object, err := client.URL2Object(urlStr)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
@@ -96,8 +96,8 @@ func (manager mcClientManager) getNewClient(urlStr string, debug bool) (clnt cli
|
||||
if err != nil {
|
||||
return nil, iodine.New(errInvalidURL{url: urlStr}, nil)
|
||||
}
|
||||
switch getURLType(urlStr) {
|
||||
case urlS3: // Minio and S3 compatible object storage
|
||||
switch client.GetURLType(urlStr) {
|
||||
case client.URLObject: // Minio and S3 compatible object storage
|
||||
hostCfg, err := getHostConfig(u.String())
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
@@ -114,7 +114,7 @@ func (manager mcClientManager) getNewClient(urlStr string, debug bool) (clnt cli
|
||||
}
|
||||
clnt = s3.GetNewClient(urlStr, auth, mcUserAgent, debug)
|
||||
return clnt, nil
|
||||
case urlFS:
|
||||
case client.URLFilesystem:
|
||||
var absURLStr string
|
||||
var err error
|
||||
if u.IsAbs() {
|
||||
@@ -131,7 +131,7 @@ func (manager mcClientManager) getNewClient(urlStr string, debug bool) (clnt cli
|
||||
clnt = fs.GetNewClient(absURLStr)
|
||||
return clnt, nil
|
||||
default:
|
||||
return nil, iodine.New(errUnsupportedScheme{scheme: getURLType(urlStr)}, nil)
|
||||
return nil, iodine.New(errUnsupportedScheme{scheme: client.GetURLType(urlStr)}, nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -29,7 +29,7 @@ import (
|
||||
|
||||
// getSourceObjectList - get list of all source objects to copy
|
||||
func getSourceObjectList(sourceClnt client.Client, urlStr string) (bucket string, objects []*client.Item, err error) {
|
||||
bucket, object, err := url2Object(urlStr)
|
||||
bucket, object, err := client.URL2Object(urlStr)
|
||||
if err != nil {
|
||||
return "", nil, iodine.New(err, nil)
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func getRecursiveTargetWriter(manager clientManager, urlStr, md5Hex string, leng
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, map[string]string{"URL": urlStr})
|
||||
}
|
||||
bucket, object, err := url2Object(urlStr)
|
||||
bucket, object, err := client.URL2Object(urlStr)
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, map[string]string{"URL": urlStr})
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func runListCmd(ctx *cli.Context) {
|
||||
switch iodine.ToError(err).(type) {
|
||||
case errUnsupportedScheme:
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalf("mc: parsing URL [%s] failed, %s\n", arg, guessPossibleURL(arg))
|
||||
console.Fatalf("mc: parsing URL [%s] failed, %s\n", arg, client.GuessPossibleURL(arg))
|
||||
default:
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalf("mc: parsing URL [%s] failed with following reason: [%s]\n", arg, iodine.ToError(err))
|
||||
@@ -119,14 +119,14 @@ func doListCmd(manager clientManager, u string, debug bool) {
|
||||
console.Fatalf("mc: instantiating a new client for URL [%s] failed with following reason: [%s]\n", u, iodine.ToError(err))
|
||||
}
|
||||
|
||||
bucket, object, err := url2Object(u)
|
||||
bucket, object, err := client.URL2Object(u)
|
||||
if err != nil {
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalf("mc: decoding bucket and object name from the URL [%s] failed\n", u)
|
||||
}
|
||||
|
||||
// ListBuckets() will not be called for fsClient() as its not needed.
|
||||
if bucket == "" && getURLType(u) != urlFS {
|
||||
if bucket == "" && client.GetURLType(u) != client.URLFilesystem {
|
||||
doListBuckets(clnt, u)
|
||||
} else {
|
||||
doListObjects(clnt, bucket, object, u)
|
||||
|
||||
@@ -45,7 +45,7 @@ func doMakeBucketCmd(ctx *cli.Context) {
|
||||
switch iodine.ToError(err).(type) {
|
||||
case errUnsupportedScheme:
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalf("mc: Unable to parse URL [%s], %s\n", arg, guessPossibleURL(arg))
|
||||
console.Fatalf("mc: Unable to parse URL [%s], %s\n", arg, client.GuessPossibleURL(arg))
|
||||
default:
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalf("mc: Unable to parse URL [%s]\n", arg)
|
||||
@@ -59,7 +59,7 @@ func doMakeBucketCmd(ctx *cli.Context) {
|
||||
console.Fatalf("mc: instantiating a new client for URL [%s] failed with following reason: [%s]\n", u, iodine.ToError(err))
|
||||
}
|
||||
|
||||
bucket, _, err := url2Object(u)
|
||||
bucket, _, err := client.URL2Object(u)
|
||||
if err != nil {
|
||||
log.Debug.Println(iodine.New(err, nil))
|
||||
console.Fatalf("mc: decoding bucket and object from URL [%s] failed\n", u)
|
||||
@@ -68,7 +68,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 getURLType(u) != urlFS {
|
||||
if client.GetURLType(u) != client.URLFilesystem {
|
||||
if bucket == "" {
|
||||
err := iodine.New(errBucketNameEmpty{}, nil)
|
||||
log.Debug.Println(err)
|
||||
|
||||
@@ -16,7 +16,11 @@
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/minio-io/mc/pkg/client"
|
||||
)
|
||||
|
||||
type errInvalidArgument struct{}
|
||||
|
||||
@@ -25,7 +29,7 @@ func (e errInvalidArgument) Error() string {
|
||||
}
|
||||
|
||||
type errUnsupportedScheme struct {
|
||||
scheme urlType
|
||||
scheme client.URLType
|
||||
}
|
||||
|
||||
func (e errUnsupportedScheme) Error() string {
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/minio-io/minio/pkg/iodine"
|
||||
)
|
||||
|
||||
// URLType - enum of different url types
|
||||
type URLType int
|
||||
|
||||
// enum types
|
||||
const (
|
||||
URLUnknown URLType = iota // Unknown type
|
||||
URLObject // Minio and S3 compatible object storage
|
||||
URLFilesystem // POSIX compatible file systems
|
||||
)
|
||||
|
||||
// GuessPossibleURL - provide guesses for possible mistakes in user input url
|
||||
func GuessPossibleURL(urlStr string) string {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if u.Scheme == "file" || !strings.Contains(urlStr, ":///") {
|
||||
possibleURL := u.Scheme + ":///" + u.Host + u.Path
|
||||
guess := fmt.Sprintf("Did you mean? %s", possibleURL)
|
||||
return guess
|
||||
}
|
||||
// TODO(y4m4) - add more guesses if possible
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetURLType returns the type of URL.
|
||||
func GetURLType(urlStr string) URLType {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return URLUnknown
|
||||
}
|
||||
|
||||
if u.Scheme == "http" || u.Scheme == "https" {
|
||||
return URLObject
|
||||
}
|
||||
|
||||
// while Scheme file, host should be empty
|
||||
if u.Scheme == "file" && u.Host == "" && strings.Contains(urlStr, ":///") {
|
||||
return URLFilesystem
|
||||
}
|
||||
|
||||
// MS Windows OS: Match drive letters
|
||||
if runtime.GOOS == "windows" {
|
||||
if regexp.MustCompile(`^[a-zA-Z]?$`).MatchString(u.Scheme) {
|
||||
return URLFilesystem
|
||||
}
|
||||
}
|
||||
|
||||
// local path, without the file:///
|
||||
if u.Scheme == "" {
|
||||
return URLFilesystem
|
||||
}
|
||||
|
||||
return URLUnknown
|
||||
}
|
||||
|
||||
// URL2Object converts URL to bucket and objectname
|
||||
func URL2Object(urlStr string) (bucketName, objectName string, err error) {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return "", "", iodine.New(err, nil)
|
||||
}
|
||||
|
||||
switch GetURLType(urlStr) {
|
||||
case URLFilesystem:
|
||||
if runtime.GOOS == "windows" {
|
||||
bucketName, objectName = filepath.Split(u.String())
|
||||
} else {
|
||||
bucketName, objectName = filepath.Split(u.Path)
|
||||
}
|
||||
default:
|
||||
splits := strings.SplitN(u.Path, "/", 3)
|
||||
switch len(splits) {
|
||||
case 0, 1:
|
||||
bucketName = ""
|
||||
objectName = ""
|
||||
case 2:
|
||||
bucketName = splits[1]
|
||||
objectName = ""
|
||||
case 3:
|
||||
bucketName = splits[1]
|
||||
objectName = splits[2]
|
||||
}
|
||||
}
|
||||
return bucketName, objectName, nil
|
||||
}
|
||||
+3
-93
@@ -17,102 +17,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/minio-io/mc/pkg/client"
|
||||
"github.com/minio-io/minio/pkg/iodine"
|
||||
)
|
||||
|
||||
type urlType int
|
||||
|
||||
const (
|
||||
urlUnknown urlType = iota // Unknown type
|
||||
urlS3 // Minio and S3 compatible object storage
|
||||
urlFS // POSIX compatible file systems
|
||||
)
|
||||
|
||||
// guessPossibleURL - provide guesses for possible mistakes in user input url
|
||||
func guessPossibleURL(urlStr string) string {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if u.Scheme == "file" || !strings.Contains(urlStr, ":///") {
|
||||
possibleURL := u.Scheme + ":///" + u.Host + u.Path
|
||||
guess := fmt.Sprintf("Did you mean? %s", possibleURL)
|
||||
return guess
|
||||
}
|
||||
// TODO(y4m4) - add more guesses if possible
|
||||
return ""
|
||||
}
|
||||
|
||||
// getURLType returns the type of URL.
|
||||
func getURLType(urlStr string) urlType {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return urlUnknown
|
||||
}
|
||||
|
||||
if u.Scheme == "http" || u.Scheme == "https" {
|
||||
return urlS3
|
||||
}
|
||||
|
||||
// while Scheme file, host should be empty
|
||||
if u.Scheme == "file" && u.Host == "" && strings.Contains(urlStr, ":///") {
|
||||
return urlFS
|
||||
}
|
||||
|
||||
// MS Windows OS: Match drive letters
|
||||
if runtime.GOOS == "windows" {
|
||||
if regexp.MustCompile(`^[a-zA-Z]?$`).MatchString(u.Scheme) {
|
||||
return urlFS
|
||||
}
|
||||
}
|
||||
|
||||
// local path, without the file:///
|
||||
if u.Scheme == "" {
|
||||
return urlFS
|
||||
}
|
||||
|
||||
return urlUnknown
|
||||
}
|
||||
|
||||
// url2Object converts URL to bucket and objectname
|
||||
func url2Object(urlStr string) (bucketName, objectName string, err error) {
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return "", "", iodine.New(err, nil)
|
||||
}
|
||||
|
||||
switch getURLType(urlStr) {
|
||||
case urlFS:
|
||||
if runtime.GOOS == "windows" {
|
||||
bucketName, objectName = filepath.Split(u.String())
|
||||
} else {
|
||||
bucketName, objectName = filepath.Split(u.Path)
|
||||
}
|
||||
default:
|
||||
splits := strings.SplitN(u.Path, "/", 3)
|
||||
switch len(splits) {
|
||||
case 0, 1:
|
||||
bucketName = ""
|
||||
objectName = ""
|
||||
case 2:
|
||||
bucketName = splits[1]
|
||||
objectName = ""
|
||||
case 3:
|
||||
bucketName = splits[1]
|
||||
objectName = splits[2]
|
||||
}
|
||||
}
|
||||
return bucketName, objectName, nil
|
||||
}
|
||||
|
||||
// parseURL extracts URL string from a single cmd-line argument
|
||||
func parseURL(arg string, aliases map[string]string) (urlStr string, err error) {
|
||||
_, err = url.Parse(arg)
|
||||
@@ -125,8 +35,8 @@ func parseURL(arg string, aliases map[string]string) (urlStr string, err error)
|
||||
if err != nil {
|
||||
return "", iodine.New(err, nil)
|
||||
}
|
||||
if getURLType(urlStr) == urlUnknown {
|
||||
return "", iodine.New(errUnsupportedScheme{scheme: urlUnknown}, map[string]string{"URL": urlStr})
|
||||
if client.GetURLType(urlStr) == client.URLUnknown {
|
||||
return "", iodine.New(errUnsupportedScheme{scheme: client.URLUnknown}, map[string]string{"URL": urlStr})
|
||||
}
|
||||
return urlStr, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user