github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_prlctl.go (about) 1 package common 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 "github.com/hashicorp/packer/packer" 10 "github.com/hashicorp/packer/template/interpolate" 11 ) 12 13 type commandTemplate struct { 14 Name string 15 } 16 17 // StepPrlctl is a step that executes additional `prlctl` commands as specified. 18 // by the template. 19 // 20 // Uses: 21 // driver Driver 22 // ui packer.Ui 23 // vmName string 24 // 25 // Produces: 26 type StepPrlctl struct { 27 Commands [][]string 28 Ctx interpolate.Context 29 } 30 31 // Run executes `prlctl` commands. 32 func (s *StepPrlctl) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 33 driver := state.Get("driver").(Driver) 34 ui := state.Get("ui").(packer.Ui) 35 vmName := state.Get("vmName").(string) 36 37 if len(s.Commands) > 0 { 38 ui.Say("Executing custom prlctl commands...") 39 } 40 41 s.Ctx.Data = &commandTemplate{ 42 Name: vmName, 43 } 44 45 for _, originalCommand := range s.Commands { 46 command := make([]string, len(originalCommand)) 47 copy(command, originalCommand) 48 49 for i, arg := range command { 50 var err error 51 command[i], err = interpolate.Render(arg, &s.Ctx) 52 if err != nil { 53 err = fmt.Errorf("Error preparing prlctl command: %s", err) 54 state.Put("error", err) 55 ui.Error(err.Error()) 56 return multistep.ActionHalt 57 } 58 } 59 60 ui.Message(fmt.Sprintf("Executing: prlctl %s", strings.Join(command, " "))) 61 if err := driver.Prlctl(command...); err != nil { 62 err = fmt.Errorf("Error executing command: %s", err) 63 state.Put("error", err) 64 ui.Error(err.Error()) 65 return multistep.ActionHalt 66 } 67 } 68 69 return multistep.ActionContinue 70 } 71 72 // Cleanup does nothing. 73 func (s *StepPrlctl) Cleanup(state multistep.StateBag) {}