add directory existence check

This commit is contained in:
Megan Marsh 2020-10-23 11:04:27 -07:00
parent 7fac596b37
commit 19bd997f11
3 changed files with 17 additions and 2 deletions

View File

@ -69,9 +69,12 @@ func (s *StepRemoteUpload) uploadFile(path string, d driver.Driver, ui packer.Ui
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
if exists := ds.DirExists(remotePath); exists == false {
log.Printf("Remote directory doesn't exist; creating...")
if err := ds.MakeDirectory(remoteDirectory); err != nil {
return "", err
}
}
if err := ds.UploadFile(path, remotePath, s.Host, s.SetHostForDatastoreUploads); err != nil {
return "", err

View File

@ -16,6 +16,7 @@ import (
type Datastore interface {
Info(params ...string) (*mo.Datastore, error)
FileExists(path string) bool
DirExists(path string) bool
Name() string
ResolvePath(path string) string
UploadFile(src, dst, host string, setHost bool) error
@ -102,6 +103,14 @@ func (ds *DatastoreDriver) Info(params ...string) (*mo.Datastore, error) {
return &info, nil
}
func (ds *DatastoreDriver) DirExists(filepath string) bool {
_, err := ds.ds.Stat(ds.driver.ctx, filepath)
if _, ok := err.(object.DatastoreNoSuchDirectoryError); ok {
return false
}
return true
}
func (ds *DatastoreDriver) FileExists(path string) bool {
_, err := ds.ds.Stat(ds.driver.ctx, path)
return err == nil

View File

@ -37,6 +37,9 @@ func (ds *DatastoreMock) FileExists(path string) bool {
return ds.FileExistsReturn
}
func (ds *DatastoreMock) DirExists(path string) bool {
return true
}
func (ds *DatastoreMock) Name() string {
if ds.NameReturn == "" {
return "datastore-mock"