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

     1  /*
     2  Copyright (c) 2019 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  	"text/tabwriter"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/govc/sso"
    29  	"github.com/vmware/govmomi/ssoadmin"
    30  	"github.com/vmware/govmomi/ssoadmin/types"
    31  )
    32  
    33  type ls struct {
    34  	*flags.ClientFlag
    35  	*flags.OutputFlag
    36  
    37  	search string
    38  }
    39  
    40  func init() {
    41  	cli.Register("sso.group.ls", &ls{})
    42  }
    43  
    44  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    45  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    46  	cmd.ClientFlag.Register(ctx, f)
    47  
    48  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    49  	cmd.OutputFlag.Register(ctx, f)
    50  
    51  	f.StringVar(&cmd.search, "search", "", "Search")
    52  }
    53  
    54  func (cmd *ls) Usage() string {
    55  	return "[NAME]"
    56  }
    57  
    58  func (cmd *ls) Description() string {
    59  	return `List SSO groups.
    60  
    61  Examples:
    62    govc sso.group.ls
    63    govc sso.group.ls group-name # list groups in group-name
    64    govc sso.group.ls -search Admin # search for groups`
    65  }
    66  
    67  func (cmd *ls) Process(ctx context.Context) error {
    68  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    69  		return err
    70  	}
    71  	return cmd.OutputFlag.Process(ctx)
    72  }
    73  
    74  type groupResult []types.AdminGroup
    75  
    76  func (r groupResult) Dump() interface{} {
    77  	return []types.AdminGroup(r)
    78  }
    79  
    80  func (r groupResult) Write(w io.Writer) error {
    81  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    82  	for _, info := range r {
    83  		fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Details.Description)
    84  	}
    85  	return tw.Flush()
    86  }
    87  
    88  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    89  	return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
    90  		if f.NArg() == 0 {
    91  			info, err := c.FindGroups(ctx, cmd.search)
    92  			if err != nil {
    93  				return err
    94  			}
    95  			return cmd.WriteResult(groupResult(info))
    96  		}
    97  		info, err := c.FindGroupsInGroup(ctx, f.Arg(0), cmd.search)
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		return cmd.WriteResult(groupResult(info))
   103  	})
   104  }