Skip to content

Commit

Permalink
Getters can determine the client mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuber committed Jan 5, 2017
1 parent 2fbd997 commit 8b0d441
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 4 deletions.
5 changes: 2 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ func (c *Client) Get() error {
checksumValue = b
}

// For now, any means file. In the future, we'll ask the getter
// what it thinks it is.
if mode == ClientModeAny {
mode = ClientModeFile
// Ask the getter which client mode to use
mode = g.ClientMode(u)

// Destination is the base name of the URL path
dst = filepath.Join(dst, filepath.Base(u.Path))
Expand Down
4 changes: 4 additions & 0 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Getter interface {
// reference a single file. If possible, the Getter should check if
// the remote end contains the same file and no-op this operation.
GetFile(string, *url.URL) error

// ClientMode returns the mode based on the given URL. This is used to
// allow clients to let the getters decide which mode to use.
ClientMode(*url.URL) ClientMode
}

// Getters is the mapping of scheme to the Getter implementation that will
Expand Down
6 changes: 6 additions & 0 deletions get_file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package getter

import "net/url"

// FileGetter is a Getter implementation that will download a module from
// a file scheme.
type FileGetter struct {
// Copy, if set to true, will copy data instead of using a symlink
Copy bool
}

func (g *FileGetter) ClientMode(_ *url.URL) ClientMode {
return ClientModeFile
}
2 changes: 1 addition & 1 deletion get_file_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (g *FileGetter) GetFile(dst string, u *url.URL) error {
path = u.RawPath
}

// The source path must exist and be a directory to be usable.
// The source path must exist and be a file to be usable.
if fi, err := os.Stat(path); err != nil {
return fmt.Errorf("source path error: %s", err)
} else if fi.IsDir() {
Expand Down
4 changes: 4 additions & 0 deletions get_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
// a git repository.
type GitGetter struct{}

func (g *GitGetter) ClientMode(_ *url.URL) ClientMode {
return ClientModeDir
}

func (g *GitGetter) Get(dst string, u *url.URL) error {
if _, err := exec.LookPath("git"); err != nil {
return fmt.Errorf("git must be available and on the PATH")
Expand Down
4 changes: 4 additions & 0 deletions get_hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
// a Mercurial repository.
type HgGetter struct{}

func (g *HgGetter) ClientMode(_ *url.URL) ClientMode {
return ClientModeDir
}

func (g *HgGetter) Get(dst string, u *url.URL) error {
if _, err := exec.LookPath("hg"); err != nil {
return fmt.Errorf("hg must be available and on the PATH")
Expand Down
7 changes: 7 additions & 0 deletions get_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ type HttpGetter struct {
Netrc bool
}

func (g *HttpGetter) ClientMode(u *url.URL) ClientMode {
if strings.HasSuffix(u.Path, "/") {
return ClientModeDir
}
return ClientModeFile
}

func (g *HttpGetter) Get(dst string, u *url.URL) error {
// Copy the URL so we can modify it
var newU url.URL = *u
Expand Down
7 changes: 7 additions & 0 deletions get_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ func (g *MockGetter) GetFile(dst string, u *url.URL) error {
}
return g.GetFileErr
}

func (g *MockGetter) ClientMode(u *url.URL) ClientMode {
if u.Path[len(u.Path)-1:] == "/" {
return ClientModeDir
}
return ClientModeFile
}
4 changes: 4 additions & 0 deletions get_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
// a S3 bucket.
type S3Getter struct{}

func (g *S3Getter) ClientMode(_ *url.URL) ClientMode {
return ClientModeDir
}

func (g *S3Getter) Get(dst string, u *url.URL) error {
// Parse URL
region, bucket, path, _, creds, err := g.parseUrl(u)
Expand Down

0 comments on commit 8b0d441

Please sign in to comment.