Skip to content

Commit

Permalink
Merge pull request #94 from cappyzawa/refactor/funcName
Browse files Browse the repository at this point in the history
Change func name
  • Loading branch information
vito authored Apr 11, 2018
2 parents 245e81b + 15130b8 commit a0ae177
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 134 deletions.
22 changes: 11 additions & 11 deletions check/check_command.go → check/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"github.com/concourse/s3-resource/versions"
)

type CheckCommand struct {
type Command struct {
s3client s3resource.S3Client
}

func NewCheckCommand(s3client s3resource.S3Client) *CheckCommand {
return &CheckCommand{
func NewCommand(s3client s3resource.S3Client) *Command {
return &Command{
s3client: s3client,
}
}

func (command *CheckCommand) Run(request CheckRequest) (CheckResponse, error) {
func (command *Command) Run(request Request) (Response, error) {
if ok, message := request.Source.IsValid(); !ok {
return CheckResponse{}, errors.New(message)
return Response{}, errors.New(message)
}

if request.Source.Regexp != "" {
Expand All @@ -29,7 +29,7 @@ func (command *CheckCommand) Run(request CheckRequest) (CheckResponse, error) {
}
}

func (command *CheckCommand) checkByRegex(request CheckRequest) CheckResponse {
func (command *Command) checkByRegex(request Request) Response {
extractions := versions.GetBucketFileVersions(command.s3client, request.Source)

if len(extractions) == 0 {
Expand All @@ -44,8 +44,8 @@ func (command *CheckCommand) checkByRegex(request CheckRequest) CheckResponse {
}
}

func (command *CheckCommand) checkByVersionedFile(request CheckRequest) CheckResponse {
response := CheckResponse{}
func (command *Command) checkByVersionedFile(request Request) Response {
response := Response{}

bucketVersions, err := command.s3client.BucketFileVersions(request.Source.Bucket, request.Source.VersionedFile)

Expand Down Expand Up @@ -85,13 +85,13 @@ func (command *CheckCommand) checkByVersionedFile(request CheckRequest) CheckRes
return response
}

func latestVersion(extractions versions.Extractions) CheckResponse {
func latestVersion(extractions versions.Extractions) Response {
lastExtraction := extractions[len(extractions)-1]
return []s3resource.Version{{Path: lastExtraction.Path}}
}

func newVersions(lastVersion versions.Extraction, extractions versions.Extractions) CheckResponse {
response := CheckResponse{}
func newVersions(lastVersion versions.Extraction, extractions versions.Extractions) Response {
response := Response{}

for _, extraction := range extractions {
if extraction.Version.Compare(lastVersion.Version) >= 0 {
Expand Down
8 changes: 4 additions & 4 deletions check/check_command_test.go → check/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ var _ = Describe("Check Command", func() {
Describe("running the command", func() {
var (
tmpPath string
request CheckRequest
request Request

s3client *fakes.FakeS3Client
command *CheckCommand
command *Command
)

BeforeEach(func() {
var err error
tmpPath, err = ioutil.TempDir("", "check_command")
Ω(err).ShouldNot(HaveOccurred())

request = CheckRequest{
request = Request{
Source: s3resource.Source{
Bucket: "bucket-name",
},
}

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

s3client.BucketFilesReturns([]string{
"files/abc-0.0.1.tgz",
Expand Down
4 changes: 2 additions & 2 deletions check/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package check

import "github.com/concourse/s3-resource"

type CheckRequest struct {
type Request struct {
Source s3resource.Source `json:"source"`
Version s3resource.Version `json:"version"`
}

type CheckResponse []s3resource.Version
type Response []s3resource.Version
8 changes: 4 additions & 4 deletions cmd/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
var request check.CheckRequest
var request check.Request
inputRequest(&request)

awsConfig := s3resource.NewAwsConfig(
Expand All @@ -28,7 +28,7 @@ func main() {
request.Source.UseV2Signing,
)

command := check.NewCheckCommand(client)
command := check.NewCommand(client)
response, err := command.Run(request)
if err != nil {
s3resource.Fatal("running command", err)
Expand All @@ -37,13 +37,13 @@ func main() {
outputResponse(response)
}

func inputRequest(request *check.CheckRequest) {
func inputRequest(request *check.Request) {
if err := json.NewDecoder(os.Stdin).Decode(request); err != nil {
s3resource.Fatal("reading request from stdin", err)
}
}

func outputResponse(response check.CheckResponse) {
func outputResponse(response check.Response) {
if err := json.NewEncoder(os.Stdout).Encode(response); err != nil {
s3resource.Fatal("writing response to stdout", err)
}
Expand Down
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
8 changes: 4 additions & 4 deletions cmd/out/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
os.Exit(1)
}

var request out.OutRequest
var request out.Request
inputRequest(&request)

sourceDir := os.Args[1]
Expand All @@ -35,7 +35,7 @@ func main() {
request.Source.UseV2Signing,
)

command := out.NewOutCommand(os.Stderr, client)
command := out.NewCommand(os.Stderr, client)
response, err := command.Run(sourceDir, request)
if err != nil {
s3resource.Fatal("running command", err)
Expand All @@ -44,13 +44,13 @@ func main() {
outputResponse(response)
}

func inputRequest(request *out.OutRequest) {
func inputRequest(request *out.Request) {
if err := json.NewDecoder(os.Stdin).Decode(request); err != nil {
s3resource.Fatal("reading request from stdin", err)
}
}

func outputResponse(response out.OutResponse) {
func outputResponse(response out.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 → 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 → 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"`
}
Loading

0 comments on commit a0ae177

Please sign in to comment.