github.com/vmware/govmomi@v0.43.0/govc/vm/option/info.go (about)

     1  /*
     2  Copyright (c) 2019-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 option
    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/vim25/types"
    29  )
    30  
    31  type info struct {
    32  	flags.EnvBrowser
    33  
    34  	key string
    35  }
    36  
    37  func init() {
    38  	cli.Register("vm.option.info", &info{})
    39  }
    40  
    41  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.EnvBrowser.Register(ctx, f)
    43  
    44  	f.StringVar(&cmd.key, "id", "", "Option descriptor key")
    45  }
    46  
    47  func (cmd *info) Usage() string {
    48  	return "[GUEST_ID]..."
    49  }
    50  
    51  func (cmd *info) Description() string {
    52  	return `VM config options for CLUSTER.
    53  
    54  The config option data contains information about the execution environment for a VM
    55  in the given CLUSTER, and optionally for a specific HOST.
    56  
    57  By default, supported guest OS IDs and full name are listed.
    58  
    59  Examples:
    60    govc vm.option.info -cluster C0
    61    govc vm.option.info -cluster C0 -dump ubuntu64Guest
    62    govc vm.option.info -cluster C0 -json | jq .guestOSDescriptor[].id
    63    govc vm.option.info -host my_hostname
    64    govc vm.option.info -vm my_vm`
    65  }
    66  
    67  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    68  	b, err := cmd.Browser(ctx)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	host, err := cmd.HostSystemIfSpecified()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	var req *types.EnvironmentBrowserConfigOptionQuerySpec
    79  
    80  	spec := func() *types.EnvironmentBrowserConfigOptionQuerySpec {
    81  		if req == nil {
    82  			req = new(types.EnvironmentBrowserConfigOptionQuerySpec)
    83  		}
    84  		return req
    85  	}
    86  
    87  	if f.NArg() != 0 {
    88  		spec().GuestId = f.Args()
    89  	}
    90  
    91  	if host != nil {
    92  		spec().Host = types.NewReference(host.Reference())
    93  	}
    94  
    95  	if cmd.key != "" {
    96  		spec().Key = cmd.key
    97  	}
    98  
    99  	opt, err := b.QueryConfigOption(ctx, req)
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	return cmd.VirtualMachineFlag.WriteResult(&infoResult{opt})
   105  }
   106  
   107  type infoResult struct {
   108  	*types.VirtualMachineConfigOption
   109  }
   110  
   111  func (r *infoResult) Write(w io.Writer) error {
   112  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   113  
   114  	for _, d := range r.GuestOSDescriptor {
   115  		_, _ = fmt.Fprintf(tw, "%s\t%s\n", d.Id, d.FullName)
   116  	}
   117  
   118  	return tw.Flush()
   119  }
   120  
   121  func (r *infoResult) Dump() interface{} {
   122  	return r.VirtualMachineConfigOption
   123  }