github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/step_upload_version.go (about) 1 package common 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "log" 8 9 "github.com/hashicorp/packer/helper/multistep" 10 "github.com/hashicorp/packer/packer" 11 ) 12 13 // StepUploadVersion is a step that uploads a file containing the version of 14 // Parallels Desktop, which can be useful for various provisioning reasons. 15 // 16 // Uses: 17 // communicator packer.Communicator 18 // driver Driver 19 // ui packer.Ui 20 type StepUploadVersion struct { 21 Path string 22 } 23 24 // Run uploads a file containing the version of Parallels Desktop. 25 func (s *StepUploadVersion) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 26 comm := state.Get("communicator").(packer.Communicator) 27 driver := state.Get("driver").(Driver) 28 ui := state.Get("ui").(packer.Ui) 29 30 if s.Path == "" { 31 log.Println("ParallelsVersionFile is empty. Not uploading.") 32 return multistep.ActionContinue 33 } 34 35 version, err := driver.Version() 36 if err != nil { 37 state.Put("error", fmt.Errorf("Error reading version for metadata upload: %s", err)) 38 return multistep.ActionHalt 39 } 40 41 ui.Say(fmt.Sprintf("Uploading Parallels version info (%s)", version)) 42 var data bytes.Buffer 43 data.WriteString(version) 44 if err := comm.Upload(s.Path, &data, nil); err != nil { 45 state.Put("error", fmt.Errorf("Error uploading Parallels version: %s", err)) 46 return multistep.ActionHalt 47 } 48 49 return multistep.ActionContinue 50 } 51 52 // Cleanup does nothing. 53 func (s *StepUploadVersion) Cleanup(state multistep.StateBag) {}