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

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