github.com/vmware/govmomi@v0.37.2/govc/device/usb/add.go (about)

     1  /*
     2  Copyright (c) 2016 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package usb
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/vmware/govmomi/govc/cli"
    26  	"github.com/vmware/govmomi/govc/flags"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type add struct {
    31  	*flags.VirtualMachineFlag
    32  
    33  	controller  string
    34  	autoConnect bool
    35  	ehciEnabled bool
    36  }
    37  
    38  func init() {
    39  	cli.Register("device.usb.add", &add{})
    40  }
    41  
    42  func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    44  	cmd.VirtualMachineFlag.Register(ctx, f)
    45  
    46  	ctypes := []string{"usb", "xhci"}
    47  	f.StringVar(&cmd.controller, "type", ctypes[0],
    48  		fmt.Sprintf("USB controller type (%s)", strings.Join(ctypes, "|")))
    49  
    50  	f.BoolVar(&cmd.autoConnect, "auto", true, "Enable ability to hot plug devices")
    51  	f.BoolVar(&cmd.ehciEnabled, "ehci", true, "Enable enhanced host controller interface (USB 2.0)")
    52  }
    53  
    54  func (cmd *add) Description() string {
    55  	return `Add USB device to VM.
    56  
    57  Examples:
    58    govc device.usb.add -vm $vm
    59    govc device.usb.add -type xhci -vm $vm
    60    govc device.info usb*`
    61  }
    62  
    63  func (cmd *add) Process(ctx context.Context) error {
    64  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    65  		return err
    66  	}
    67  	return nil
    68  }
    69  
    70  func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
    71  	vm, err := cmd.VirtualMachine()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	if vm == nil {
    77  		return flag.ErrHelp
    78  	}
    79  
    80  	var d types.BaseVirtualDevice
    81  
    82  	switch cmd.controller {
    83  	case "usb":
    84  		c := new(types.VirtualUSBController)
    85  		c.AutoConnectDevices = &cmd.autoConnect
    86  		c.EhciEnabled = &cmd.ehciEnabled
    87  		d = c
    88  	case "xhci":
    89  		c := new(types.VirtualUSBXHCIController)
    90  		c.AutoConnectDevices = &cmd.autoConnect
    91  		d = c
    92  	default:
    93  		return flag.ErrHelp
    94  	}
    95  
    96  	err = vm.AddDevice(ctx, d)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	// output name of device we just created
   102  	devices, err := vm.Device(ctx)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	devices = devices.SelectByType(d)
   108  
   109  	name := devices.Name(devices[len(devices)-1])
   110  
   111  	fmt.Println(name)
   112  
   113  	return nil
   114  }