From 7259d50862e14ab975a1dd81b3abb833e4b35257 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Mon, 23 Mar 2015 01:25:23 -0700 Subject: [PATCH] url parsers --- cmd-recursive-cp.go => cmd-cp-recursive.go | 0 cmd-ls.go | 33 +++++-- common.go | 10 +-- url-parser.go | 100 +++++++++++++++++++++ 4 files changed, 129 insertions(+), 14 deletions(-) rename cmd-recursive-cp.go => cmd-cp-recursive.go (100%) create mode 100644 url-parser.go diff --git a/cmd-recursive-cp.go b/cmd-cp-recursive.go similarity index 100% rename from cmd-recursive-cp.go rename to cmd-cp-recursive.go diff --git a/cmd-ls.go b/cmd-ls.go index 577ce8b8..99efc57e 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -21,6 +21,8 @@ import ( "os" "time" + "net/url" + "github.com/cheggaaa/pb" "github.com/codegangsta/cli" "github.com/minio-io/mc/pkg/client" @@ -59,39 +61,52 @@ func printObject(date time.Time, v int64, key string) { func doListCmd(c *cli.Context) { var items []*client.Item - args, err := parseArgs(c) + // quiet := c.GlobalBool("quiet") + + urlStr, err := argParseURL(c) if err != nil { fatal(err.Error()) } - s3c, err := getNewClient(c, args.source.url) + bucketName, objectName, err := url2Object(urlStr) if err != nil { fatal(err.Error()) } + + URL, err := url.Parse(urlStr) + if err != nil { + fatal(err.Error()) + } + + s3c, err := getNewClient(c, URL) + if err != nil { + fatal(err.Error()) + } + switch true { - case args.source.bucket == "": // List all buckets + case bucketName == "": // List all buckets buckets, err := s3c.ListBuckets() if err != nil { fatal(err.Error()) } printBuckets(buckets) - case args.source.key == "": // List objects in a bucket - items, _, err = s3c.ListObjects(args.source.bucket, "", "", "", s3.MaxKeys) + case objectName == "": // List objects in a bucket + items, _, err = s3c.ListObjects(bucketName, "", "", "", s3.MaxKeys) if err != nil { fatal(err.Error()) } printObjects(items) - case args.source.key != "": // List objects matching the key prefix + case objectName != "": // List objects matching the key prefix var date time.Time var size int64 - size, date, err = s3c.Stat(args.source.bucket, args.source.key) + size, date, err = s3c.Stat(bucketName, objectName) switch err { case nil: // List a single object. Exact key - printObject(date, size, args.source.key) + printObject(date, size, objectName) case os.ErrNotExist: // List all objects matching the key prefix - items, _, err = s3c.ListObjects(args.source.bucket, "", args.source.key, "", s3.MaxKeys) + items, _, err = s3c.ListObjects(bucketName, "", objectName, "", s3.MaxKeys) if err != nil { fatal(err.Error()) } diff --git a/common.go b/common.go index d82e1691..24123394 100644 --- a/common.go +++ b/common.go @@ -1,5 +1,5 @@ /* - * Minimalist Object Storage, (C) 2014,2015 Minio, Inc. + * Minimalist Object Storage, (C) 2014, 2015 Minio, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -84,7 +84,7 @@ func parseArgs(c *cli.Context) (args *cmdArgs, err error) { switch len(c.Args()) { case 1: // only one URL - urlString, err := aliasExpand(c.Args().Get(0)) + urlString, err := aliasExpand(c.Args().First()) if err != nil { return nil, err } @@ -112,8 +112,8 @@ func parseArgs(c *cli.Context) (args *cmdArgs, err error) { } case 2: // one URL and one path||URL switch true { - case c.Args().Get(0) != "": - urlString, err := aliasExpand(c.Args().Get(0)) + case c.Args().First() != "": + urlString, err := aliasExpand(c.Args().First()) if err != nil { return nil, err } @@ -141,7 +141,7 @@ func parseArgs(c *cli.Context) (args *cmdArgs, err error) { if urlParsed.Host != "" { return nil, errInvalidScheme } - if urlParsed.Path != c.Args().Get(0) { + if urlParsed.Path != c.Args().First() { return nil, errInvalidScheme } if urlParsed.Path == "." { diff --git a/url-parser.go b/url-parser.go new file mode 100644 index 00000000..be747171 --- /dev/null +++ b/url-parser.go @@ -0,0 +1,100 @@ +/* + * Minimalist Object Storage, (C) 2014, 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "strings" + + "net/url" + + "github.com/codegangsta/cli" +) + +// url2Bucket converts URL to bucket name and object name +func url2Object(urlStr string) (bucketName, objectName string, err error) { + url, err := url.Parse(urlStr) + if err != nil { + return "", "", err + } + + if url.Path == "" { + // No bucket name passed. It is a valid case. + return "", "", nil + } + splits := strings.SplitN(url.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 +} + +// url2Bucket converts URL to bucket name +func url2Bucket(urlStr string) (bucketName string, err error) { + bucketName, _, err = url2Object(urlStr) + return bucketName, err +} + +//url2Scheme converts URL to protocol scheme (http|https|donut..) +func url2Scheme(urlStr string) (protoScheme string, err error) { + url, err := url.Parse(urlStr) + if err != nil { + return "", err + } + protoScheme = url.Scheme + return protoScheme, nil +} + +//url2Host converts URL to host:port +func url2Host(urlStr string) (hostName string, err error) { + url, err := url.Parse(urlStr) + if err != nil { + return "", err + } + hostName = url.Host + return hostName, err +} + +// argParseURL extracts URL string from a single cmd-line argument +func argParseURL(c *cli.Context) (urlStr string, err error) { + urlStr = c.Args().First() + // Use default host if no argument is passed + if urlStr == "" { + // Load config file + config, err := getMcConfig() + if err != nil { + return "", err + } + urlStr = config.DefaultHost + } + + // Check and expand Alias + urlStr, err = aliasExpand(urlStr) + + if err != nil { + return "", err + } + + return urlStr, err +}