github.com/vmware/govmomi@v0.51.0/cli/vm/target/capabilities.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 target
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"reflect"
    13  	"text/tabwriter"
    14  
    15  	"github.com/vmware/govmomi/cli"
    16  	"github.com/vmware/govmomi/cli/flags"
    17  	"github.com/vmware/govmomi/vim25/types"
    18  )
    19  
    20  type capls struct {
    21  	flags.EnvBrowser
    22  }
    23  
    24  func init() {
    25  	cli.Register("vm.target.cap.ls", &capls{})
    26  }
    27  
    28  func (cmd *capls) Register(ctx context.Context, f *flag.FlagSet) {
    29  	cmd.EnvBrowser.Register(ctx, f)
    30  }
    31  
    32  func (cmd *capls) Description() string {
    33  	return `List VM config target capabilities.
    34  
    35  The config target data contains capabilities about the execution environment for a VM
    36  in the given CLUSTER, and optionally for a specific HOST.
    37  
    38  Examples:
    39    govc vm.target.cap.ls -cluster C0
    40    govc vm.target.cap.ls -host my_hostname
    41    govc vm.target.cap.ls -vm my_vm`
    42  }
    43  
    44  func (cmd *capls) Run(ctx context.Context, f *flag.FlagSet) error {
    45  	b, err := cmd.Browser(ctx)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	host, err := cmd.HostSystemIfSpecified()
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	cap, err := b.QueryTargetCapabilities(ctx, host)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	return cmd.VirtualMachineFlag.WriteResult(&caplsResult{cap})
    61  }
    62  
    63  type caplsResult struct {
    64  	*types.HostCapability
    65  }
    66  
    67  func (r *caplsResult) Write(w io.Writer) error {
    68  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    69  
    70  	cap := reflect.ValueOf(r.HostCapability).Elem()
    71  	kind := cap.Type()
    72  
    73  	for i := 0; i < cap.NumField(); i++ {
    74  		field := cap.Field(i)
    75  
    76  		if kind.Field(i).Anonymous {
    77  			continue
    78  		}
    79  		if field.Kind() == reflect.Pointer {
    80  			if field.IsNil() {
    81  				continue
    82  			}
    83  			field = field.Elem()
    84  		}
    85  
    86  		fmt.Fprintf(tw, "%s:\t%v\n", kind.Field(i).Name, field.Interface())
    87  	}
    88  
    89  	return tw.Flush()
    90  }
    91  
    92  func (r *caplsResult) Dump() any {
    93  	return r.HostCapability
    94  }