Skip to content

Commit

Permalink
run Detect automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 25, 2015
1 parent 2463fe5 commit 95dfbcb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
33 changes: 29 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,43 @@ type Client struct {
// Dst is the path to save the downloaded thing as. If Dir is set to
// true, then this should be a directory. If the directory doesn't exist,
// it will be created for you.
//
// Pwd is the working directory for detection. If this isn't set, some
// detection may fail. Client will not default pwd to the current
// working directory for security reasons.
Src string
Dst string
Pwd string

// Dir, if true, tells the Client it is downloading a directory (versus
// a single file). This distinction is necessary since filenames and
// directory names follow the same format so disambiguating is impossible
// without knowing ahead of time.
Dir bool

// Getters is the map of protocols supported by this client. Use
// the global Getters variable for the built-in defaults.
// Detectors is the list of detectors that are tried on the source.
// If this is nil, then the default Detectors will be used.
Detectors []Detector

// Getters is the map of protocols supported by this client. If this
// is nil, then the default Getters variable will be used.
Getters map[string]Getter
}

// Get downloads the configured source to the destination.
func (c *Client) Get() error {
force, src := getForcedGetter(c.Src)
// Detect the URL. This is safe if it is already detected.
detectors := c.Detectors
if detectors == nil {
detectors = Detectors
}
src, err := Detect(c.Src, c.Pwd, detectors)
if err != nil {
return err
}

// Determine if we have a forced protocol, i.e. "git::http://..."
force, src := getForcedGetter(src)

// If there is a subdir component, then we download the root separately
// and then copy over the proper subdir.
Expand Down Expand Up @@ -65,7 +85,12 @@ func (c *Client) Get() error {
force = u.Scheme
}

g, ok := c.Getters[force]
getters := c.Getters
if getters == nil {
getters = Getters
}

g, ok := getters[force]
if !ok {
return fmt.Errorf(
"download not supported for scheme '%s'", force)
Expand Down
25 changes: 25 additions & 0 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ func TestGet_file(t *testing.T) {
}
}

func TestGet_fileDetect(t *testing.T) {
dst := tempDir(t)
u := filepath.Join("./test-fixtures", "basic")
pwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}

client := &Client{
Src: u,
Dst: dst,
Pwd: pwd,
Dir: true,
}

if err := client.Get(); err != nil {
t.Fatalf("err: %s", err)
}

mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestGet_fileForced(t *testing.T) {
dst := tempDir(t)
u := testModule("basic")
Expand Down

0 comments on commit 95dfbcb

Please sign in to comment.