github.com/vmware/govmomi@v0.43.0/govc/vm/target/capabilities.go (about)

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