github.com/vmware/govmomi@v0.51.0/cli/device/ls.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  	"io"
    12  	"text/tabwriter"
    13  
    14  	"github.com/vmware/govmomi/cli"
    15  	"github.com/vmware/govmomi/cli/flags"
    16  	"github.com/vmware/govmomi/object"
    17  )
    18  
    19  type ls struct {
    20  	*flags.VirtualMachineFlag
    21  
    22  	boot bool
    23  }
    24  
    25  func init() {
    26  	cli.Register("device.ls", &ls{})
    27  }
    28  
    29  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    30  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    31  	cmd.VirtualMachineFlag.Register(ctx, f)
    32  
    33  	f.BoolVar(&cmd.boot, "boot", false, "List devices configured in the VM's boot options")
    34  }
    35  
    36  func (cmd *ls) Process(ctx context.Context) error {
    37  	if err := cmd.VirtualMachineFlag.Process(ctx); err != nil {
    38  		return err
    39  	}
    40  	return nil
    41  }
    42  
    43  func (cmd *ls) Description() string {
    44  	return `List devices for VM.
    45  
    46  Examples:
    47    govc device.ls -vm $name
    48    govc device.ls -vm $name disk-*
    49    govc device.ls -vm $name -json | jq '.devices[].name'`
    50  }
    51  
    52  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    53  	vm, err := cmd.VirtualMachine()
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	if vm == nil {
    59  		return flag.ErrHelp
    60  	}
    61  
    62  	devices, err := vm.Device(ctx)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if f.NArg() != 0 {
    68  		var matches object.VirtualDeviceList
    69  		for _, name := range f.Args() {
    70  			device := match(name, devices)
    71  			if len(device) == 0 {
    72  				return fmt.Errorf("device '%s' not found", name)
    73  			}
    74  			matches = append(matches, device...)
    75  		}
    76  		devices = matches
    77  	}
    78  
    79  	if cmd.boot {
    80  		options, err := vm.BootOptions(ctx)
    81  		if err != nil {
    82  			return err
    83  		}
    84  
    85  		devices = devices.SelectBootOrder(options.BootOrder)
    86  	}
    87  
    88  	res := lsResult{toLsList(devices), devices}
    89  	return cmd.WriteResult(&res)
    90  }
    91  
    92  type lsDevice struct {
    93  	Name    string `json:"name"`
    94  	Type    string `json:"type"`
    95  	Summary string `json:"summary"`
    96  }
    97  
    98  func toLsList(devices object.VirtualDeviceList) []lsDevice {
    99  	var res []lsDevice
   100  
   101  	for _, device := range devices {
   102  		res = append(res, lsDevice{
   103  			Name:    devices.Name(device),
   104  			Type:    devices.TypeName(device),
   105  			Summary: device.GetVirtualDevice().DeviceInfo.GetDescription().Summary,
   106  		})
   107  	}
   108  
   109  	return res
   110  }
   111  
   112  type lsResult struct {
   113  	Devices []lsDevice `json:"devices"`
   114  	list    object.VirtualDeviceList
   115  }
   116  
   117  func (r *lsResult) Write(w io.Writer) error {
   118  	tw := tabwriter.NewWriter(w, 3, 0, 2, ' ', 0)
   119  
   120  	for _, device := range r.list {
   121  		fmt.Fprintf(tw, "%s\t%s\t%s\n", r.list.Name(device), r.list.TypeName(device),
   122  			device.GetVirtualDevice().DeviceInfo.GetDescription().Summary)
   123  	}
   124  
   125  	return tw.Flush()
   126  }