github.com/vmware/govmomi@v0.37.2/govc/option/ls.go (about)

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