Skip to content

Commit

Permalink
Refactor some in_command tests and code
Browse files Browse the repository at this point in the history
This will make adding the unpack feature easier to do.
  • Loading branch information
Kris Hicks authored and krishicks committed Jul 11, 2017
1 parent 887a453 commit fd92752
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 75 deletions.
97 changes: 32 additions & 65 deletions in/in_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,106 +45,73 @@ func (command *InCommand) Run(destinationDir string, request InRequest) (InRespo
return InResponse{}, errors.New(message)
}

err := command.createDirectory(destinationDir)
err := os.MkdirAll(destinationDir, 0755)
if err != nil {
return InResponse{}, err
}

if request.Source.Regexp != "" {
return command.inByRegex(destinationDir, request)
} else {
return command.inByVersionedFile(destinationDir, request)
}
}
var remotePath string
var versionNumber string
var versionID string

func (command *InCommand) inByRegex(destinationDir string, request InRequest) (InResponse, error) {
if request.Version.Path == "" {
return InResponse{}, ErrMissingPath
}
if request.Source.Regexp != "" {
if request.Version.Path == "" {
return InResponse{}, ErrMissingPath
}

remotePath := request.Version.Path
remotePath = request.Version.Path

extraction, ok := versions.Extract(remotePath, request.Source.Regexp)
if !ok {
return InResponse{}, fmt.Errorf("regex does not match provided version: %#v", request.Version)
extraction, ok := versions.Extract(remotePath, request.Source.Regexp)
if !ok {
return InResponse{}, fmt.Errorf("regex does not match provided version: %#v", request.Version)
}

versionNumber = extraction.VersionNumber
} else {
remotePath = request.Source.VersionedFile
versionNumber = request.Version.VersionID
versionID = request.Version.VersionID
}

err := command.writeVersionFile(extraction.VersionNumber, destinationDir)
err = command.writeVersionFile(versionNumber, destinationDir)
if err != nil {
return InResponse{}, err
}

err = command.downloadFile(
request.Source.Bucket,
remotePath,
"",
versionID,
destinationDir,
path.Base(remotePath),
)
if err != nil {
return InResponse{}, err
}

url := command.urlProvider.GetURL(request, remotePath)
err = command.writeURLFile(
destinationDir,
url,
)
if err != nil {
return InResponse{}, err
}

return InResponse{
Version: s3resource.Version{
Path: remotePath,
},
Metadata: command.metadata(remotePath, request.Source.Private, url),
}, nil
}

func (command *InCommand) inByVersionedFile(destinationDir string, request InRequest) (InResponse, error) {

err := command.writeVersionFile(request.Version.VersionID, destinationDir)
if err != nil {
return InResponse{}, err
}

remotePath := request.Source.VersionedFile

err = command.downloadFile(
request.Source.Bucket,
remotePath,
request.Version.VersionID,
destinationDir,
path.Base(remotePath),
)

if err != nil {
url := command.urlProvider.GetURL(request, remotePath)
if err = command.writeURLFile(destinationDir, url); err != nil {
return InResponse{}, err
}

url := command.urlProvider.GetURL(request, remotePath)
err = command.writeURLFile(
destinationDir,
url,
)
metadata := command.metadata(remotePath, request.Source.Private, url)

if err != nil {
return InResponse{}, err
if versionID == "" {
return InResponse{
Version: s3resource.Version{
Path: remotePath,
},
Metadata: metadata,
}, nil
}

return InResponse{
Version: s3resource.Version{
VersionID: request.Version.VersionID,
VersionID: versionID,
},
Metadata: command.metadata(remotePath, request.Source.Private, url),
Metadata: metadata,
}, nil

}

func (command *InCommand) createDirectory(destDir string) error {
return os.MkdirAll(destDir, 0755)
}

func (command *InCommand) writeURLFile(destDir string, url string) error {
Expand Down
20 changes: 10 additions & 10 deletions in/in_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ var _ = Describe("In Command", func() {
request = InRequest{
Source: s3resource.Source{
Bucket: "bucket-name",
Regexp: "files/a-file-(.*).tgz",
Regexp: "files/a-file-(.*)",
},
Version: s3resource.Version{
Path: "files/a-file-1.3.tgz",
Path: "files/a-file-1.3",
},
}

Expand Down Expand Up @@ -74,7 +74,7 @@ var _ = Describe("In Command", func() {

Context("when there is an existing version in the request", func() {
BeforeEach(func() {
request.Version.Path = "files/a-file-1.3.tgz"
request.Version.Path = "files/a-file-1.3"
})

It("downloads the existing version of the file", func() {
Expand All @@ -85,9 +85,9 @@ var _ = Describe("In Command", func() {
bucketName, remotePath, versionID, localPath := s3client.DownloadFileArgsForCall(0)

Ω(bucketName).Should(Equal("bucket-name"))
Ω(remotePath).Should(Equal("files/a-file-1.3.tgz"))
Ω(remotePath).Should(Equal("files/a-file-1.3"))
Ω(versionID).Should(BeEmpty())
Ω(localPath).Should(Equal(filepath.Join(destDir, "a-file-1.3.tgz")))
Ω(localPath).Should(Equal(filepath.Join(destDir, "a-file-1.3")))
})

It("creates a 'url' file that contains the URL", func() {
Expand All @@ -104,7 +104,7 @@ var _ = Describe("In Command", func() {

bucketName, remotePath, private, versionID := s3client.URLArgsForCall(0)
Ω(bucketName).Should(Equal("bucket-name"))
Ω(remotePath).Should(Equal("files/a-file-1.3.tgz"))
Ω(remotePath).Should(Equal("files/a-file-1.3"))
Ω(private).Should(Equal(false))
Ω(versionID).Should(BeEmpty())
})
Expand All @@ -129,7 +129,7 @@ var _ = Describe("In Command", func() {
Ω(s3client.URLCallCount()).Should(Equal(1))
bucketName, remotePath, private, versionID := s3client.URLArgsForCall(0)
Ω(bucketName).Should(Equal("bucket-name"))
Ω(remotePath).Should(Equal("files/a-file-1.3.tgz"))
Ω(remotePath).Should(Equal("files/a-file-1.3"))
Ω(private).Should(Equal(true))
Ω(versionID).Should(BeEmpty())
})
Expand All @@ -153,15 +153,15 @@ var _ = Describe("In Command", func() {
response, err := command.Run(destDir, request)
Ω(err).ShouldNot(HaveOccurred())

Ω(response.Version.Path).Should(Equal("files/a-file-1.3.tgz"))
Ω(response.Version.Path).Should(Equal("files/a-file-1.3"))
})

It("has metadata about the file", func() {
response, err := command.Run(destDir, request)
Ω(err).ShouldNot(HaveOccurred())

Ω(response.Metadata[0].Name).Should(Equal("filename"))
Ω(response.Metadata[0].Value).Should(Equal("a-file-1.3.tgz"))
Ω(response.Metadata[0].Value).Should(Equal("a-file-1.3"))

Ω(response.Metadata[1].Name).Should(Equal("url"))
Ω(response.Metadata[1].Value).Should(Equal("http://google.com"))
Expand Down Expand Up @@ -192,7 +192,7 @@ var _ = Describe("In Command", func() {
_, err := command.Run(destDir, request)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("regex does not match provided version"))
Expect(err.Error()).To(ContainSubstring("files/a-file-1.3.tgz"))
Expect(err.Error()).To(ContainSubstring("files/a-file-1.3"))
})
})
})
Expand Down

0 comments on commit fd92752

Please sign in to comment.