github.com/vmware/govmomi@v0.37.2/govc/cluster/group/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 group
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"strings"
    26  	"text/tabwriter"
    27  
    28  	"github.com/vmware/govmomi/govc/cli"
    29  	"github.com/vmware/govmomi/vim25/types"
    30  )
    31  
    32  type ls struct {
    33  	*InfoFlag
    34  
    35  	long bool
    36  }
    37  
    38  func init() {
    39  	cli.Register("cluster.group.ls", &ls{})
    40  }
    41  
    42  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.InfoFlag, ctx = NewInfoFlag(ctx)
    44  	cmd.InfoFlag.Register(ctx, f)
    45  
    46  	f.BoolVar(&cmd.long, "l", false, "Long listing format")
    47  }
    48  
    49  func (cmd *ls) Process(ctx context.Context) error {
    50  	return cmd.InfoFlag.Process(ctx)
    51  }
    52  
    53  func (cmd *ls) Description() string {
    54  	return `List cluster groups and group members.
    55  
    56  Examples:
    57    govc cluster.group.ls -cluster my_cluster
    58    govc cluster.group.ls -cluster my_cluster -l | grep ClusterHostGroup
    59    govc cluster.group.ls -cluster my_cluster -name my_group`
    60  }
    61  
    62  type groupResult []string
    63  
    64  func (r groupResult) Write(w io.Writer) error {
    65  	for i := range r {
    66  		fmt.Fprintln(w, r[i])
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  type groupResultLong []types.BaseClusterGroupInfo
    73  
    74  func (r groupResultLong) Write(w io.Writer) error {
    75  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
    76  
    77  	for i := range r {
    78  		info := r[i].GetClusterGroupInfo()
    79  		kind := fmt.Sprintf("%T", r[i])
    80  		kind = strings.SplitN(kind, ".", 2)[1]
    81  		_, _ = fmt.Fprintf(tw, "%s\t%s\n", kind, info.Name)
    82  	}
    83  
    84  	return tw.Flush()
    85  }
    86  
    87  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    88  	var res groupResult
    89  
    90  	if cmd.name == "" {
    91  		groups, err := cmd.Groups(ctx)
    92  		if err != nil {
    93  			return err
    94  		}
    95  
    96  		if cmd.long {
    97  			return cmd.WriteResult(groupResultLong(groups))
    98  		}
    99  
   100  		for _, g := range groups {
   101  			res = append(res, g.GetClusterGroupInfo().Name)
   102  		}
   103  	} else {
   104  		group, err := cmd.Group(ctx)
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		names, err := cmd.Names(ctx, *group.refs)
   110  		if err != nil {
   111  			return err
   112  		}
   113  
   114  		for _, ref := range *group.refs {
   115  			res = append(res, names[ref])
   116  		}
   117  	}
   118  
   119  	return cmd.WriteResult(res)
   120  }