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

     1  /*
     2  Copyright (c) 2018 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 user
    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  	solution bool
    38  	group    bool
    39  	search   string
    40  }
    41  
    42  func init() {
    43  	cli.Register("sso.user.ls", &ls{})
    44  }
    45  
    46  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    47  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    48  	cmd.ClientFlag.Register(ctx, f)
    49  
    50  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    51  	cmd.OutputFlag.Register(ctx, f)
    52  
    53  	f.BoolVar(&cmd.solution, "s", false, "List solution users")
    54  	f.BoolVar(&cmd.group, "group", false, "List users in group")
    55  	f.StringVar(&cmd.search, "search", "", "Search users in group")
    56  }
    57  
    58  func (cmd *ls) Description() string {
    59  	return `List SSO users.
    60  
    61  Examples:
    62    govc sso.user.ls -s
    63    govc sso.user.ls -group group-name`
    64  }
    65  
    66  func (cmd *ls) Process(ctx context.Context) error {
    67  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    68  		return err
    69  	}
    70  	return cmd.OutputFlag.Process(ctx)
    71  }
    72  
    73  type userResult []types.AdminUser
    74  
    75  func (r userResult) Dump() interface{} {
    76  	return []types.AdminUser(r)
    77  }
    78  
    79  func (r userResult) Write(w io.Writer) error {
    80  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    81  	for _, info := range r {
    82  		fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Description)
    83  	}
    84  	return tw.Flush()
    85  }
    86  
    87  type solutionResult []types.AdminSolutionUser
    88  
    89  func (r solutionResult) Dump() interface{} {
    90  	return []types.AdminSolutionUser(r)
    91  }
    92  
    93  func (r solutionResult) Write(w io.Writer) error {
    94  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    95  	for _, info := range r {
    96  		fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Details.Description)
    97  	}
    98  	return tw.Flush()
    99  }
   100  
   101  type personResult []types.AdminPersonUser
   102  
   103  func (r personResult) Dump() interface{} {
   104  	return []types.AdminPersonUser(r)
   105  }
   106  
   107  func (r personResult) Write(w io.Writer) error {
   108  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   109  	for _, info := range r {
   110  		fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Details.Description)
   111  	}
   112  	return tw.Flush()
   113  }
   114  
   115  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
   116  	arg := f.Arg(0)
   117  
   118  	return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
   119  		if cmd.solution {
   120  			if f.NArg() != 0 {
   121  				return flag.ErrHelp
   122  			}
   123  			info, err := c.FindSolutionUsers(ctx, arg)
   124  			if err != nil {
   125  				return err
   126  			}
   127  
   128  			return cmd.WriteResult(solutionResult(info))
   129  		}
   130  		if cmd.group {
   131  			if f.NArg() == 0 {
   132  				return flag.ErrHelp
   133  			}
   134  			info, err := c.FindUsersInGroup(ctx, f.Arg(0), cmd.search)
   135  			if err != nil {
   136  				return err
   137  			}
   138  
   139  			return cmd.WriteResult(userResult(info))
   140  		}
   141  		info, err := c.FindPersonUsers(ctx, arg)
   142  		if err != nil {
   143  			return err
   144  		}
   145  
   146  		return cmd.WriteResult(personResult(info))
   147  	})
   148  }