github.com/vmware/govmomi@v0.43.0/govc/vm/option/ls.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 option
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"flag"
    23  	"fmt"
    24  	"io"
    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 ls struct {
    33  	flags.EnvBrowser
    34  }
    35  
    36  func init() {
    37  	cli.Register("vm.option.ls", &ls{})
    38  }
    39  
    40  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.EnvBrowser.Register(ctx, f)
    42  }
    43  
    44  func (cmd *ls) Description() string {
    45  	return `List VM config option keys for CLUSTER.
    46  
    47  Examples:
    48    govc vm.option.ls -cluster C0`
    49  }
    50  
    51  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    52  	b, err := cmd.Browser(ctx)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	opts, err := b.QueryConfigOptionDescriptor(ctx)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	return cmd.VirtualMachineFlag.WriteResult(&lsResult{opts})
    63  }
    64  
    65  type lsResult struct {
    66  	opts []types.VirtualMachineConfigOptionDescriptor
    67  }
    68  
    69  func (r *lsResult) Write(w io.Writer) error {
    70  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    71  
    72  	for _, d := range r.opts {
    73  		_, _ = fmt.Fprintf(tw, "%s\t%s\n", d.Key, d.Description)
    74  	}
    75  
    76  	return tw.Flush()
    77  }
    78  
    79  func (r *lsResult) Dump() interface{} {
    80  	return r.opts
    81  }
    82  
    83  func (r *lsResult) MarshalJSON() ([]byte, error) {
    84  	return json.Marshal(r.opts)
    85  }