Merge pull request #3692 from mohae/3474-file-mkdir-all
3474 file mkdir all
This commit is contained in:
commit
32cbb515b5
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mitchellh/packer/common"
|
"github.com/mitchellh/packer/common"
|
||||||
|
@ -95,12 +96,20 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator) error {
|
func (p *Provisioner) ProvisionDownload(ui packer.Ui, comm packer.Communicator) error {
|
||||||
for _, src := range p.config.Sources {
|
for _, src := range p.config.Sources {
|
||||||
ui.Say(fmt.Sprintf("Downloading %s => %s", src, p.config.Destination))
|
ui.Say(fmt.Sprintf("Downloading %s => %s", src, p.config.Destination))
|
||||||
|
// ensure destination dir exists. p.config.Destination may either be a file or a dir.
|
||||||
if strings.HasSuffix(p.config.Destination, "/") {
|
dir := p.config.Destination
|
||||||
err := os.MkdirAll(p.config.Destination, os.FileMode(0755))
|
// if it doesn't end with a /, set dir as the parent dir
|
||||||
|
if !strings.HasSuffix(p.config.Destination, "/") {
|
||||||
|
dir = filepath.Dir(dir)
|
||||||
|
}
|
||||||
|
if dir != "" {
|
||||||
|
err := os.MkdirAll(dir, os.FileMode(0755))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// if the config.Destination was a dir, download the dir
|
||||||
|
if !strings.HasSuffix(p.config.Destination, "/") {
|
||||||
return comm.DownloadDir(src, p.config.Destination, nil)
|
return comm.DownloadDir(src, p.config.Destination, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mitchellh/packer/packer"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testConfig() map[string]interface{} {
|
func testConfig() map[string]interface{} {
|
||||||
|
@ -139,3 +141,56 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
|
||||||
t.Fatalf("should upload with source file's data")
|
t.Fatalf("should upload with source file's data")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProvisionDownloadMkdirAll(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
}{
|
||||||
|
{"dir"},
|
||||||
|
{"dir/"},
|
||||||
|
{"dir/subdir"},
|
||||||
|
{"dir/subdir/"},
|
||||||
|
{"path/to/dir"},
|
||||||
|
{"path/to/dir/"},
|
||||||
|
}
|
||||||
|
tmpDir, err := ioutil.TempDir("", "packer-file")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error tempdir: %s", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
tf, err := ioutil.TempFile(tmpDir, "packer")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error tempfile: %s", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tf.Name())
|
||||||
|
|
||||||
|
config := map[string]interface{}{
|
||||||
|
"source": tf.Name(),
|
||||||
|
}
|
||||||
|
var p Provisioner
|
||||||
|
for _, test := range tests {
|
||||||
|
path := filepath.Join(tmpDir, test.path)
|
||||||
|
config["destination"] = filepath.Join(path, "something")
|
||||||
|
if err := p.Prepare(config); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
ui := &stubUi{}
|
||||||
|
comm := &packer.MockCommunicator{}
|
||||||
|
err = p.ProvisionDownload(ui, comm)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("should successfully provision: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(ui.sayMessages, tf.Name()) {
|
||||||
|
t.Fatalf("should print source filename")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(ui.sayMessages, "something") {
|
||||||
|
t.Fatalf("should print destination filename")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
t.Fatalf("stat of download dir should not error: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue