Merge pull request #163 from harshavardhana/pr_out_move_back_to_functional_style_for_simplicity

This commit is contained in:
Harshavardhana
2015-03-23 15:43:21 -07:00
10 changed files with 65 additions and 71 deletions
+2 -2
View File
@@ -95,7 +95,7 @@ func doRecursiveCP(c *cli.Context, args *cmdArgs) error {
if !st.IsDir() {
return errors.New("Should be a directory")
}
s3c, err := getNewClient(c.GlobalBool("debug"), args.destination.url)
s3c, err := getNewClient(c.GlobalBool("debug"), args.destination.url.String())
if err != nil {
return err
}
@@ -113,7 +113,7 @@ func doRecursiveCP(c *cli.Context, args *cmdArgs) error {
return err
}
case args.destination.bucket == "":
s3c, err := getNewClient(c.GlobalBool("debug"), args.source.url)
s3c, err := getNewClient(c.GlobalBool("debug"), args.source.url.String())
if err != nil {
return err
}
+4 -4
View File
@@ -83,7 +83,7 @@ func firstMode(c *cli.Context, args *cmdArgs) error {
if args.destination.key == "" {
args.destination.key = args.source.key
}
s3c, err := getNewClient(c.GlobalBool("debug"), args.destination.url)
s3c, err := getNewClient(c.GlobalBool("debug"), args.destination.url.String())
if err != nil {
return err
}
@@ -105,7 +105,7 @@ func secondMode(c *cli.Context, args *cmdArgs) error {
var err error
var st os.FileInfo
s3c, err := getNewClient(c.GlobalBool("debug"), args.source.url)
s3c, err := getNewClient(c.GlobalBool("debug"), args.source.url.String())
if err != nil {
return err
}
@@ -190,7 +190,7 @@ func thirdMode(c *cli.Context, args *cmdArgs) error {
var objectSize int64
var err error
s3cSource, err := getNewClient(c.GlobalBool("debug"), args.source.url)
s3cSource, err := getNewClient(c.GlobalBool("debug"), args.source.url.String())
if err != nil {
return err
}
@@ -205,7 +205,7 @@ func thirdMode(c *cli.Context, args *cmdArgs) error {
}
// Check if the object already exists
s3cDest, err := getNewClient(c.GlobalBool("debug"), args.destination.url)
s3cDest, err := getNewClient(c.GlobalBool("debug"), args.destination.url.String())
if err != nil {
return err
}
+4 -16
View File
@@ -19,11 +19,9 @@ package main
import (
"fmt"
"os"
"strings"
"regexp"
"time"
"net/url"
"github.com/cheggaaa/pb"
"github.com/codegangsta/cli"
"github.com/minio-io/mc/pkg/client"
@@ -58,10 +56,6 @@ func printObject(date time.Time, v int64, key string) {
info(msg)
}
func validURL(urlStr string) bool {
return strings.Contains(urlStr, "http")
}
// doListCmd lists objects inside a bucket
func doListCmd(c *cli.Context) {
var items []*client.Item
@@ -73,22 +67,16 @@ func doListCmd(c *cli.Context) {
fatal(err.Error())
}
if !validURL(urlStr) {
if !isValidURL(urlStr) {
fatal(errInvalidScheme.Error())
}
URL, err := url.Parse(urlStr)
if err != nil {
fatal(err.Error())
}
u := &URLParser{URL}
bucketName, objectName, err := u.Object()
bucketName, objectName, err := url2Object(urlStr)
if err != nil {
fatal(err.Error())
}
s3c, err := getNewClient(c.GlobalBool("debug"), URL)
s3c, err := getNewClient(c.GlobalBool("debug"), urlStr)
if err != nil {
fatal(err.Error())
}
+2 -9
View File
@@ -17,8 +17,6 @@
package main
import (
"net/url"
"github.com/codegangsta/cli"
"github.com/minio-io/mc/pkg/s3"
)
@@ -29,18 +27,13 @@ func doMakeBucketCmd(c *cli.Context) {
if err != nil {
fatal(err.Error())
}
URL, err := url.Parse(urlStr)
if err != nil {
fatal(err.Error())
}
u := &URLParser{URL}
s3c, err := getNewClient(c.GlobalBool("debug"), URL)
bucket, err := url2Bucket(urlStr)
if err != nil {
fatal(err.Error())
}
bucket, err := u.Bucket()
s3c, err := getNewClient(c.GlobalBool("debug"), urlStr)
if err != nil {
fatal(err.Error())
}
+11 -1
View File
@@ -47,7 +47,7 @@ func startBar(size int64) *pb.ProgressBar {
}
// NewClient - get new client
func getNewClient(debug bool, url *url.URL) (cl client.Client, err error) {
func getNewClient(debug bool, url string) (cl client.Client, err error) {
config, err := getMcConfig()
if err != nil {
return nil, err
@@ -202,3 +202,13 @@ func parseArgs(c *cli.Context) (args *cmdArgs, err error) {
func getMcBashCompletionFilename() string {
return path.Join(getMcConfigDir(), "mc.bash_completion")
}
func isValidURL(s string) bool {
var r = "http.*://*"
ok, err := regexp.MatchString(r, s)
if err != nil {
fmt.Println(err)
return false
}
return ok
}
-6
View File
@@ -3,7 +3,6 @@ package client
import (
"io"
"net/http"
"net/url"
"time"
)
@@ -40,11 +39,6 @@ type Prefix struct {
type Meta struct {
*Auth // AWS auth credentials
Transport http.RoundTripper // or nil for the default behavior
// Supports URL in following formats
// - http://<ipaddress>/<bucketname>/<object>
// - http://<bucketname>.<domain>/<object>
URL *url.URL
}
// Auth - see http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html
+5 -5
View File
@@ -65,9 +65,9 @@ func (a bySize) Less(i, j int) bool { return a[i].Size < a[j].Size }
// ListBuckets - Get list of buckets
func (c *s3Client) ListBuckets() ([]*client.Bucket, error) {
url := fmt.Sprintf("%s://%s/", c.URL.Scheme, c.URL.Host)
url := fmt.Sprintf("%s://%s/", c.Scheme, c.Host)
req := newReq(url)
c.signRequest(req, c.URL.Host)
c.signRequest(req, c.Host)
res, err := c.Transport.RoundTrip(req)
if err != nil {
@@ -86,11 +86,11 @@ func (c *s3Client) ListBuckets() ([]*client.Bucket, error) {
func (c *s3Client) PutBucket(bucket string) error {
var url string
if IsValidBucketName(bucket) && !strings.Contains(bucket, ".") {
url = fmt.Sprintf("%s://%s/%s", c.URL.Scheme, c.URL.Host, bucket)
url = fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket)
}
req := newReq(url)
req.Method = "PUT"
c.signRequest(req, c.URL.Host)
c.signRequest(req, c.Host)
res, err := c.Transport.RoundTrip(req)
if err != nil {
return err
@@ -143,7 +143,7 @@ func (c *s3Client) ListObjects(bucket string, startAt, prefix, delimiter string,
for try := 1; try <= maxTries; try++ {
time.Sleep(time.Duration(try-1) * 100 * time.Millisecond)
req := newReq(urlReq)
c.signRequest(req, c.URL.Host)
c.signRequest(req, c.Host)
res, err := c.Transport.RoundTrip(req)
if err != nil {
if try < maxTries {
+25 -13
View File
@@ -71,15 +71,27 @@ type listBucketResults struct {
type s3Client struct {
*client.Meta
// Supports URL in following formats
// - http://<ipaddress>/<bucketname>/<object>
// - http://<bucketname>.<domain>/<object>
*url.URL
}
// GetNewClient returns an initialized s3Client structure.
func GetNewClient(auth *client.Auth, u *url.URL, transport http.RoundTripper) client.Client {
return &s3Client{&client.Meta{
Auth: auth,
Transport: GetNewTraceTransport(s3Verify{}, transport),
URL: u,
}}
func GetNewClient(auth *client.Auth, urlStr string, transport http.RoundTripper) client.Client {
u, err := url.Parse(urlStr)
if err != nil {
return nil
}
s3c := &s3Client{
&client.Meta{
Auth: auth,
Transport: GetNewTraceTransport(s3Verify{}, transport),
},
u,
}
return s3c
}
// bucketURL constructs a URL (with a trailing slash) for a given
@@ -91,16 +103,16 @@ func (c *s3Client) bucketURL(bucket string) string {
// TODO: Bucket names can contain ".". This second check should be removed.
if IsValidBucketName(bucket) && !strings.Contains(bucket, ".") {
// if localhost use PathStyle
if strings.Contains(c.URL.Host, "localhost") || strings.Contains(c.URL.Host, "127.0.0.1") {
return fmt.Sprintf("%s://%s/%s", c.URL.Scheme, c.URL.Host, bucket)
if strings.Contains(c.Host, "localhost") || strings.Contains(c.Host, "127.0.0.1") {
return fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket)
}
// Verify if its ip address, use PathStyle
host, _, _ := net.SplitHostPort(c.URL.Host)
host, _, _ := net.SplitHostPort(c.Host)
if net.ParseIP(host) != nil {
return fmt.Sprintf("%s://%s/%s", c.URL.Scheme, c.URL.Host, bucket)
return fmt.Sprintf("%s://%s/%s", c.Scheme, c.Host, bucket)
}
// For DNS hostname or amazonaws.com use subdomain style
url = fmt.Sprintf("%s://%s.%s/", c.URL.Scheme, bucket, c.URL.Host)
url = fmt.Sprintf("%s://%s.%s/", c.Scheme, bucket, c.Host)
}
return url
}
@@ -108,10 +120,10 @@ func (c *s3Client) bucketURL(bucket string) string {
// keyURL constructs a URL using bucket and object key
func (c *s3Client) keyURL(bucket, key string) string {
url := c.bucketURL(bucket)
if strings.Contains(c.URL.Host, "localhost") || strings.Contains(c.URL.Host, "127.0.0.1") {
if strings.Contains(c.Host, "localhost") || strings.Contains(c.Host, "127.0.0.1") {
return url + "/" + key
}
host, _, _ := net.SplitHostPort(c.URL.Host)
host, _, _ := net.SplitHostPort(c.Host)
if net.ParseIP(host) != nil {
return url + "/" + key
}
+4 -4
View File
@@ -75,7 +75,7 @@ func (c *s3Client) Put(bucket, key string, size int64, contents io.Reader) error
req.Body = ioutil.NopCloser(sink)
b64 := base64.StdEncoding.EncodeToString(h.Sum(nil))
req.Header.Set("Content-MD5", b64)
c.signRequest(req, c.URL.Host)
c.signRequest(req, c.Host)
res, err := c.Transport.RoundTrip(req)
if err != nil {
@@ -93,7 +93,7 @@ func (c *s3Client) Put(bucket, key string, size int64, contents io.Reader) error
func (c *s3Client) Stat(bucket, key string) (size int64, date time.Time, reterr error) {
req := newReq(c.keyURL(bucket, key))
req.Method = "HEAD"
c.signRequest(req, c.URL.Host)
c.signRequest(req, c.Host)
res, err := c.Transport.RoundTrip(req)
if err != nil {
return 0, date, err
@@ -125,7 +125,7 @@ func (c *s3Client) Stat(bucket, key string) (size int64, date time.Time, reterr
// Get - download a requested object from a given bucket
func (c *s3Client) Get(bucket, key string) (body io.ReadCloser, size int64, err error) {
req := newReq(c.keyURL(bucket, key))
c.signRequest(req, c.URL.Host)
c.signRequest(req, c.Host)
res, err := c.Transport.RoundTrip(req)
if err != nil {
return nil, 0, err
@@ -151,7 +151,7 @@ func (c *s3Client) GetPartial(bucket, key string, offset, length int64) (body io
} else {
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", offset))
}
c.signRequest(req, c.URL.Host)
c.signRequest(req, c.Host)
res, err := c.Transport.RoundTrip(req)
if err != nil {
+8 -11
View File
@@ -24,17 +24,14 @@ import (
"github.com/codegangsta/cli"
)
type URLParser struct {
*url.URL
}
// Object converts URL to bucket and objectname
func (u *URLParser) Object() (bucketName, objectName string, err error) {
if u.Path == "" {
// url2Object converts URL to bucket and objectname
func url2Object(urlStr string) (bucketName, objectName string, err error) {
url, err := url.Parse(urlStr)
if url.Path == "" {
// No bucket name passed. It is a valid case.
return "", "", nil
}
splits := strings.SplitN(u.Path, "/", 3)
splits := strings.SplitN(url.Path, "/", 3)
switch len(splits) {
case 0, 1:
bucketName = ""
@@ -49,9 +46,9 @@ func (u *URLParser) Object() (bucketName, objectName string, err error) {
return bucketName, objectName, nil
}
// Bucket converts URL to bucket name
func (u *URLParser) Bucket() (bucketName string, err error) {
bucketName, _, err = u.Object()
// url2Bucket converts URL to bucket name
func url2Bucket(urlStr string) (bucketName string, err error) {
bucketName, _, err = url2Object(urlStr)
return bucketName, err
}