Only add the folder when the error is NotFoundError

This commit is contained in:
bugbuilder 2017-07-25 23:14:59 -04:00
parent 7274bbb63d
commit fb52c42b3d
2 changed files with 21 additions and 15 deletions

View File

@ -103,7 +103,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
c, err := govmomi.NewClient(context.Background(), p.url, p.config.Insecure)
if err != nil {
return nil, true, fmt.Errorf("Error connecting to vSphere: %s", err)
return nil, false, fmt.Errorf("Error connecting to vSphere: %s", err)
}
state := new(multistep.BasicStateBag)
@ -114,13 +114,13 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
&stepChooseDatacenter{
Datacenter: p.config.Datacenter,
},
&stepCreateFolder{
Folder: p.config.Folder,
},
&stepFetchVm{
VMName: p.config.VMName,
Source: source,
},
&stepCreateFolder{
Folder: p.config.Folder,
},
&stepMarkAsTemplate{},
&stepMoveTemplate{
Folder: p.config.Folder,
@ -131,7 +131,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
runner.Run(state)
if rawErr, ok := state.GetOk("error"); ok {
return nil, true, rawErr.(error)
return nil, false, rawErr.(error)
}
return artifact, true, nil
}

View File

@ -36,18 +36,24 @@ func (s *stepCreateFolder) Run(state multistep.StateBag) multistep.StepAction {
for {
root, err = finder.Folder(context.Background(), filepath.ToSlash(filepath.Join(base, path)))
if err != nil {
_, folder := filepath.Split(path)
folders = append(folders, folder)
if i := strings.LastIndex(path, "/"); i == 0 {
root, err = finder.Folder(context.Background(), filepath.ToSlash(base))
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
if _, ok := err.(*find.NotFoundError); ok {
_, folder := filepath.Split(path)
folders = append(folders, folder)
if i := strings.LastIndex(path, "/"); i == 0 {
root, err = finder.Folder(context.Background(), filepath.ToSlash(base))
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
break
} else {
path = path[:i]
}
break
} else {
path = path[:i]
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
} else {
break