github.com/vmware/govmomi@v0.51.0/cli/option/ls.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 option
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"text/tabwriter"
    14  
    15  	"github.com/vmware/govmomi/cli"
    16  	"github.com/vmware/govmomi/cli/flags"
    17  	"github.com/vmware/govmomi/object"
    18  	"github.com/vmware/govmomi/vim25/mo"
    19  	"github.com/vmware/govmomi/vim25/types"
    20  )
    21  
    22  type List struct {
    23  	*flags.ClientFlag
    24  	*flags.OutputFlag
    25  }
    26  
    27  func init() {
    28  	cli.Register("option.ls", &List{})
    29  }
    30  
    31  func (cmd *List) Register(ctx context.Context, f *flag.FlagSet) {
    32  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    33  	cmd.ClientFlag.Register(ctx, f)
    34  
    35  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    36  	cmd.OutputFlag.Register(ctx, f)
    37  }
    38  
    39  func (cmd *List) Process(ctx context.Context) error {
    40  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    41  		return err
    42  	}
    43  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  func (cmd *List) Usage() string {
    50  	return "[NAME]"
    51  }
    52  
    53  var ListDescription = `List option with the given NAME.
    54  
    55  If NAME ends with a dot, all options for that subtree are listed.`
    56  
    57  func (cmd *List) Description() string {
    58  	return ListDescription + `
    59  
    60  Examples:
    61    govc option.ls
    62    govc option.ls config.vpxd.sso.
    63    govc option.ls config.vpxd.sso.sts.uri`
    64  }
    65  
    66  func (cmd *List) Query(ctx context.Context, f *flag.FlagSet, m *object.OptionManager) error {
    67  	var err error
    68  	var opts []types.BaseOptionValue
    69  
    70  	if f.NArg() > 1 {
    71  		return flag.ErrHelp
    72  	}
    73  
    74  	if f.NArg() == 1 {
    75  		opts, err = m.Query(ctx, f.Arg(0))
    76  	} else {
    77  		var om mo.OptionManager
    78  		err = m.Properties(ctx, m.Reference(), []string{"setting"}, &om)
    79  		opts = om.Setting
    80  	}
    81  
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	return cmd.WriteResult(optionResult(opts))
    87  }
    88  
    89  func (cmd *List) Run(ctx context.Context, f *flag.FlagSet) error {
    90  	c, err := cmd.Client()
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	m := object.NewOptionManager(c, *c.ServiceContent.Setting)
    96  
    97  	return cmd.Query(ctx, f, m)
    98  }
    99  
   100  type optionResult []types.BaseOptionValue
   101  
   102  func (r optionResult) Write(w io.Writer) error {
   103  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
   104  	for _, opt := range r {
   105  		o := opt.GetOptionValue()
   106  		fmt.Fprintf(tw, "%s:\t%v\n", o.Key, o.Value)
   107  	}
   108  	return tw.Flush()
   109  }