diff --git a/cmd/in/main.go b/cmd/in/main.go index 3db3aa43..3d9417ce 100644 --- a/cmd/in/main.go +++ b/cmd/in/main.go @@ -21,7 +21,7 @@ func main() { destinationDir := os.Args[1] - var request in.InRequest + var request in.Request inputRequest(&request) awsConfig := s3resource.NewAwsConfig( @@ -56,7 +56,7 @@ func main() { request.Source.UseV2Signing, ) - command := in.NewInCommand(client) + command := in.NewCommand(client) response, err := command.Run(destinationDir, request) if err != nil { @@ -66,13 +66,13 @@ func main() { outputResponse(response) } -func inputRequest(request *in.InRequest) { +func inputRequest(request *in.Request) { if err := json.NewDecoder(os.Stdin).Decode(request); err != nil { s3resource.Fatal("reading request from stdin", err) } } -func outputResponse(response in.InResponse) { +func outputResponse(response in.Response) { if err := json.NewEncoder(os.Stdout).Encode(response); err != nil { s3resource.Fatal("writing response to stdout", err) } diff --git a/in/in_command.go b/in/in_command.go index c7846aad..a5120cec 100644 --- a/in/in_command.go +++ b/in/in_command.go @@ -18,21 +18,21 @@ type RequestURLProvider struct { s3Client s3resource.S3Client } -func (up *RequestURLProvider) GetURL(request InRequest, remotePath string) string { +func (up *RequestURLProvider) GetURL(request Request, remotePath string) string { return up.s3URL(request, remotePath) } -func (up *RequestURLProvider) s3URL(request InRequest, remotePath string) string { +func (up *RequestURLProvider) s3URL(request Request, remotePath string) string { return up.s3Client.URL(request.Source.Bucket, remotePath, request.Source.Private, request.Version.VersionID) } -type InCommand struct { +type Command struct { s3client s3resource.S3Client urlProvider RequestURLProvider } -func NewInCommand(s3client s3resource.S3Client) *InCommand { - return &InCommand{ +func NewCommand(s3client s3resource.S3Client) *Command { + return &Command{ s3client: s3client, urlProvider: RequestURLProvider{ s3Client: s3client, @@ -40,14 +40,14 @@ func NewInCommand(s3client s3resource.S3Client) *InCommand { } } -func (command *InCommand) Run(destinationDir string, request InRequest) (InResponse, error) { +func (command *Command) Run(destinationDir string, request Request) (Response, error) { if ok, message := request.Source.IsValid(); !ok { - return InResponse{}, errors.New(message) + return Response{}, errors.New(message) } err := os.MkdirAll(destinationDir, 0755) if err != nil { - return InResponse{}, err + return Response{}, err } var remotePath string @@ -56,14 +56,14 @@ func (command *InCommand) Run(destinationDir string, request InRequest) (InRespo if request.Source.Regexp != "" { if request.Version.Path == "" { - return InResponse{}, ErrMissingPath + return Response{}, ErrMissingPath } 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) + return Response{}, fmt.Errorf("regex does not match provided version: %#v", request.Version) } versionNumber = extraction.VersionNumber @@ -82,36 +82,36 @@ func (command *InCommand) Run(destinationDir string, request InRequest) (InRespo ) if err != nil { - return InResponse{}, err + return Response{}, err } if request.Params.Unpack { destinationPath := filepath.Join(destinationDir, path.Base(remotePath)) mime := archiveMimetype(destinationPath) if mime == "" { - return InResponse{}, fmt.Errorf("not an archive: %s", destinationPath) + return Response{}, fmt.Errorf("not an archive: %s", destinationPath) } err = extractArchive(mime, destinationPath) if err != nil { - return InResponse{}, err + return Response{}, err } } url := command.urlProvider.GetURL(request, remotePath) if err = command.writeURLFile(destinationDir, url); err != nil { - return InResponse{}, err + return Response{}, err } err = command.writeVersionFile(versionNumber, destinationDir) if err != nil { - return InResponse{}, err + return Response{}, err } metadata := command.metadata(remotePath, request.Source.Private, url) if versionID == "" { - return InResponse{ + return Response{ Version: s3resource.Version{ Path: remotePath, }, @@ -119,7 +119,7 @@ func (command *InCommand) Run(destinationDir string, request InRequest) (InRespo }, nil } - return InResponse{ + return Response{ Version: s3resource.Version{ VersionID: versionID, }, @@ -127,15 +127,15 @@ func (command *InCommand) Run(destinationDir string, request InRequest) (InRespo }, nil } -func (command *InCommand) writeURLFile(destDir string, url string) error { +func (command *Command) writeURLFile(destDir string, url string) error { return ioutil.WriteFile(filepath.Join(destDir, "url"), []byte(url), 0644) } -func (command *InCommand) writeVersionFile(versionNumber string, destDir string) error { +func (command *Command) writeVersionFile(versionNumber string, destDir string) error { return ioutil.WriteFile(filepath.Join(destDir, "version"), []byte(versionNumber), 0644) } -func (command *InCommand) downloadFile(bucketName string, remotePath string, versionID string, destinationDir string, destinationFile string) error { +func (command *Command) downloadFile(bucketName string, remotePath string, versionID string, destinationDir string, destinationFile string) error { localPath := filepath.Join(destinationDir, destinationFile) return command.s3client.DownloadFile( @@ -146,7 +146,7 @@ func (command *InCommand) downloadFile(bucketName string, remotePath string, ver ) } -func (command *InCommand) metadata(remotePath string, private bool, url string) []s3resource.MetadataPair { +func (command *Command) metadata(remotePath string, private bool, url string) []s3resource.MetadataPair { remoteFilename := filepath.Base(remotePath) metadata := []s3resource.MetadataPair{ diff --git a/in/in_command_test.go b/in/in_command_test.go index 2b75b838..161e6354 100644 --- a/in/in_command_test.go +++ b/in/in_command_test.go @@ -26,10 +26,10 @@ var _ = Describe("In Command", func() { var ( tmpPath string destDir string - request InRequest + request Request s3client *fakes.FakeS3Client - command *InCommand + command *Command ) BeforeEach(func() { @@ -38,7 +38,7 @@ var _ = Describe("In Command", func() { Ω(err).ShouldNot(HaveOccurred()) destDir = filepath.Join(tmpPath, "destination") - request = InRequest{ + request = Request{ Source: s3resource.Source{ Bucket: "bucket-name", Regexp: "files/a-file-(.*)", @@ -49,7 +49,7 @@ var _ = Describe("In Command", func() { } s3client = &fakes.FakeS3Client{} - command = NewInCommand(s3client) + command = NewCommand(s3client) s3client.URLReturns("http://google.com") }) diff --git a/in/models.go b/in/models.go index c5ef98e9..1eebbc7b 100644 --- a/in/models.go +++ b/in/models.go @@ -2,7 +2,7 @@ package in import "github.com/concourse/s3-resource" -type InRequest struct { +type Request struct { Source s3resource.Source `json:"source"` Version s3resource.Version `json:"version"` Params Params `json:"params"` @@ -12,7 +12,7 @@ type Params struct { Unpack bool `json:"unpack"` } -type InResponse struct { +type Response struct { Version s3resource.Version `json:"version"` Metadata []s3resource.MetadataPair `json:"metadata"` } diff --git a/integration/in_test.go b/integration/in_test.go index b596e805..3decfacf 100644 --- a/integration/in_test.go +++ b/integration/in_test.go @@ -55,10 +55,10 @@ var _ = Describe("in", func() { }) Context("with a versioned_file and a regex", func() { - var inRequest in.InRequest + var inRequest in.Request BeforeEach(func() { - inRequest = in.InRequest{ + inRequest = in.Request{ Source: s3resource.Source{ AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey, @@ -84,12 +84,12 @@ var _ = Describe("in", func() { }) Context("when the given version only has a path", func() { - var inRequest in.InRequest + var inRequest in.Request var directoryPrefix string BeforeEach(func() { directoryPrefix = "in-request-files" - inRequest = in.InRequest{ + inRequest = in.Request{ Source: s3resource.Source{ AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey, @@ -133,11 +133,11 @@ var _ = Describe("in", func() { It("downloads the file", func() { reader := bytes.NewBuffer(session.Out.Contents()) - var response in.InResponse + var response in.Response err := json.NewDecoder(reader).Decode(&response) Ω(err).ShouldNot(HaveOccurred()) - Ω(response).Should(Equal(in.InResponse{ + Ω(response).Should(Equal(in.Response{ Version: s3resource.Version{ Path: "in-request-files/some-file-2", }, @@ -171,13 +171,13 @@ var _ = Describe("in", func() { }) Context("when the given version has a versionID and path", func() { - var inRequest in.InRequest + var inRequest in.Request var directoryPrefix string var expectedVersion string BeforeEach(func() { directoryPrefix = "in-request-files-versioned" - inRequest = in.InRequest{ + inRequest = in.Request{ Source: s3resource.Source{ AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey, @@ -226,10 +226,10 @@ var _ = Describe("in", func() { It("downloads the file", func() { reader := bytes.NewBuffer(session.Out.Contents()) - var response in.InResponse + var response in.Response err := json.NewDecoder(reader).Decode(&response) - Ω(response).Should(Equal(in.InResponse{ + Ω(response).Should(Equal(in.Response{ Version: s3resource.Version{ VersionID: expectedVersion, }, @@ -263,7 +263,7 @@ var _ = Describe("in", func() { }) Context("when cloudfront_url is set", func() { - var inRequest in.InRequest + var inRequest in.Request var directoryPrefix string BeforeEach(func() { @@ -272,7 +272,7 @@ var _ = Describe("in", func() { } directoryPrefix = "in-request-cloudfront-files" - inRequest = in.InRequest{ + inRequest = in.Request{ Source: s3resource.Source{ AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey, @@ -316,11 +316,11 @@ var _ = Describe("in", func() { It("downloads the file from CloudFront", func() { reader := bytes.NewBuffer(session.Out.Contents()) - var response in.InResponse + var response in.Response err := json.NewDecoder(reader).Decode(&response) Ω(err).ShouldNot(HaveOccurred()) - Ω(response).Should(Equal(in.InResponse{ + Ω(response).Should(Equal(in.Response{ Version: s3resource.Version{ Path: "in-request-cloudfront-files/some-file-2", }, @@ -349,10 +349,10 @@ var _ = Describe("in", func() { }) Context("when cloudfront_url is set but has too few dots", func() { - var inRequest in.InRequest + var inRequest in.Request BeforeEach(func() { - inRequest = in.InRequest{ + inRequest = in.Request{ Source: s3resource.Source{ AccessKeyID: accessKeyID, SecretAccessKey: secretAccessKey,