github.com/vmware/govmomi@v0.51.0/cli/device/disconnect.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package device 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 12 "github.com/vmware/govmomi/cli" 13 "github.com/vmware/govmomi/cli/flags" 14 ) 15 16 type disconnect struct { 17 *flags.VirtualMachineFlag 18 } 19 20 func init() { 21 cli.Register("device.disconnect", &disconnect{}) 22 } 23 24 func (cmd *disconnect) Register(ctx context.Context, f *flag.FlagSet) { 25 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 26 cmd.VirtualMachineFlag.Register(ctx, f) 27 } 28 29 func (cmd *disconnect) Process(ctx context.Context) error { 30 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 31 return err 32 } 33 return nil 34 } 35 36 func (cmd *disconnect) Usage() string { 37 return "DEVICE..." 38 } 39 40 func (cmd *disconnect) Description() string { 41 return `Disconnect DEVICE on VM. 42 43 Examples: 44 govc device.disconnect -vm $name cdrom-3000` 45 } 46 47 func (cmd *disconnect) Run(ctx context.Context, f *flag.FlagSet) error { 48 vm, err := cmd.VirtualMachine() 49 if err != nil { 50 return err 51 } 52 53 if vm == nil { 54 return flag.ErrHelp 55 } 56 57 devices, err := vm.Device(ctx) 58 if err != nil { 59 return err 60 } 61 62 for _, name := range f.Args() { 63 device := devices.Find(name) 64 if device == nil { 65 return fmt.Errorf("device '%s' not found", name) 66 } 67 68 if err = devices.Disconnect(device); err != nil { 69 return err 70 } 71 72 if err = vm.EditDevice(ctx, device); err != nil { 73 return err 74 } 75 } 76 77 return nil 78 }