Skip to content

Commit

Permalink
remove package name from func name's prefix around in
Browse files Browse the repository at this point in the history
  • Loading branch information
cappyzawa committed Feb 5, 2018
1 parent 9c94054 commit 2b0a6fe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
8 changes: 4 additions & 4 deletions cmd/in/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {

destinationDir := os.Args[1]

var request in.InRequest
var request in.Request
inputRequest(&request)

awsConfig := s3resource.NewAwsConfig(
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
42 changes: 21 additions & 21 deletions in/in_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@ 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,
},
}
}

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
Expand All @@ -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
Expand All @@ -82,60 +82,60 @@ 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,
},
Metadata: metadata,
}, nil
}

return InResponse{
return Response{
Version: s3resource.Version{
VersionID: versionID,
},
Metadata: metadata,
}, 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(
Expand All @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions in/in_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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-(.*)",
Expand All @@ -49,7 +49,7 @@ var _ = Describe("In Command", func() {
}

s3client = &fakes.FakeS3Client{}
command = NewInCommand(s3client)
command = NewCommand(s3client)

s3client.URLReturns("http://google.com")
})
Expand Down
4 changes: 2 additions & 2 deletions in/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
}
32 changes: 16 additions & 16 deletions integration/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2b0a6fe

Please sign in to comment.