Skip to content

Commit

Permalink
Implement s3 push/pull using dsn [ref #17]
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleiade committed Nov 28, 2013
1 parent c31ead8 commit 39f1369
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 50 deletions.
31 changes: 17 additions & 14 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"
"time"
"github.com/oleiade/trousseau/dsn"
)

// hasExpectedArgs checks whether the number of args are as expected.
Expand Down Expand Up @@ -49,17 +50,18 @@ func CreateAction(c *cli.Context) {
}

func PushAction(c *cli.Context) {
if !hasExpectedArgs(c.Args(), 0) {
if !hasExpectedArgs(c.Args(), 1) {
log.Fatal("Incorrect number of arguments to 'push' command")
}

switch c.String("remote-storage") {
case "s3":
bucket := c.String("s3-bucket")
remoteFilename := c.String("remote-filename")
region := c.String("s3-region")
endpointDsn, err := dsn.Parse(c.Args()[0])
if err != nil {
log.Fatal(err)
}

err := uploadUsingS3(bucket, remoteFilename, region)
switch endpointDsn.Scheme {
case "s3":
err := uploadUsingS3(endpointDsn)
if err != nil {
log.Fatal(err)
}
Expand All @@ -78,17 +80,18 @@ func PushAction(c *cli.Context) {
}

func PullAction(c *cli.Context) {
if !hasExpectedArgs(c.Args(), 0) {
if !hasExpectedArgs(c.Args(), 1) {
log.Fatal("Incorrect number of arguments to 'pull' command")
}

switch c.String("remote-storage") {
case "s3":
bucket := c.String("s3-bucket")
remoteFilename := c.String("remote-filename")
region := c.String("s3-region")
endpointDsn, err := dsn.Parse(c.Args()[0])
if err != nil {
log.Fatal(err)
}

err := DownloadUsingS3(bucket, remoteFilename, region)
switch endpointDsn.Scheme {
case "s3":
err := DownloadUsingS3(endpointDsn)
if err != nil {
log.Fatal(err)
}
Expand Down
18 changes: 8 additions & 10 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@ package trousseau
import (
"fmt"
"github.com/crowdmob/goamz/aws"
"github.com/oleiade/trousseau/dsn"
)

// downloadUsingS3 executes the whole process of pulling
// the trousseau data store file from s3 remote storage
// using the provided environment.
func DownloadUsingS3(bucket, remoteFilename, region string) error {
awsAuth, err := aws.EnvAuth()
if err != nil {
return err
}
func DownloadUsingS3(dsn *dsn.Dsn) error {
awsAuth := aws.Auth{AccessKey: dsn.Id, SecretKey: dsn.Secret}

awsRegion, ok := aws.Regions[region]
awsRegion, ok := aws.Regions[dsn.Port]
if !ok {
return fmt.Errorf("Invalid aws region supplied %s", region)
return fmt.Errorf("Invalid aws region supplied %s", dsn.Port)
}

s3Storage := NewS3Storage(awsAuth, bucket, awsRegion)
err = s3Storage.Connect()
s3Storage := NewS3Storage(awsAuth, dsn.Host, awsRegion)
err := s3Storage.Connect()
if err != nil {
fmt.Errorf("Unable to connect to S3, have you set %s env var?",
ENV_S3_BUCKET_KEY)
}

err = s3Storage.Pull(remoteFilename)
err = s3Storage.Pull(dsn.Path)
if err != nil {
return err
}
Expand Down
34 changes: 18 additions & 16 deletions dsn/dsn.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package dsn

import (
"fmt"
"regexp"
"strconv"
"errors"
)

type Dsn struct {
Expand All @@ -11,11 +12,11 @@ type Dsn struct {
Id string
Secret string
Host string
Port int
Port string
Path string
}

func extractParts(rawdsn string) []string {
func extractParts(rawdsn string) ([]string, error) {
re := regexp.MustCompile("(?P<scheme>[a-zA-Z0-9]+)" +
"?://" +
"(?P<id>[^:]+)" +
Expand All @@ -24,14 +25,23 @@ func extractParts(rawdsn string) []string {
"?@" +
"(?P<host>[a-zA-Z0-9]+)" +
"?:" +
"(?P<port>[0-9]+)" +
"(?P<port>[a-zA-Z0-9-]+)" +
"?/" +
"(?P<path>[a-zA-Z0-9/]+)")
return re.FindStringSubmatch(rawdsn)[1:]
"(?P<path>[a-zA-Z0-9/_.-]+)")

parts := re.FindStringSubmatch(rawdsn)
if len(parts) == 0 {
return nil, errors.New(fmt.Sprintf("No dsn mathched in %s", rawdsn))
}

return parts[1:], nil
}

func Parse(rawdsn string) (dsn *Dsn, err error) {
parts := extractParts(rawdsn)
parts, err := extractParts(rawdsn)
if err != nil {
return nil, err
}

// Init Dsn instance with url parsed informations
// we can thrust
Expand All @@ -41,17 +51,9 @@ func Parse(rawdsn string) (dsn *Dsn, err error) {
Id: parts[1],
Secret: parts[2],
Host: parts[3],
Port: parts[4],
Path: parts[5],
}

// Extract host and port from the parsed URL
// instance Host string
if port := parts[4]; port != "" {
dsn.Port, err = strconv.Atoi(port)
if err != nil {
return nil, err
}
}

return dsn, nil
}
18 changes: 8 additions & 10 deletions upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@ package trousseau
import (
"fmt"
"github.com/crowdmob/goamz/aws"
"github.com/oleiade/trousseau/dsn"
)

// uploadUsingS3 executes the whole process of pushing
// the trousseau data store file to s3 remote storage
// using the provided environment.
func uploadUsingS3(bucket, remoteFilename, region string) error {
awsAuth, err := aws.EnvAuth()
if err != nil {
return err
}
func uploadUsingS3(dsn *dsn.Dsn) error {
awsAuth := aws.Auth{AccessKey: dsn.Id, SecretKey: dsn.Secret}

awsRegion, ok := aws.Regions[region]
awsRegion, ok := aws.Regions[dsn.Port]
if !ok {
return fmt.Errorf("Invalid aws region supplied %s", region)
return fmt.Errorf("Invalid aws region supplied %s", dsn.Port)
}

s3Storage := NewS3Storage(awsAuth, bucket, awsRegion)
err = s3Storage.Connect()
s3Storage := NewS3Storage(awsAuth, dsn.Host, awsRegion)
err := s3Storage.Connect()
if err != nil {
return fmt.Errorf("Unable to connect to S3, have you set %s env var?",
ENV_S3_BUCKET_KEY)
}

err = s3Storage.Push(remoteFilename)
err = s3Storage.Push(dsn.Path)
if err != nil {
return err
}
Expand Down

0 comments on commit 39f1369

Please sign in to comment.