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

     1  /*
     2  Copyright (c) 2016 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 role
    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/permissions"
    28  	"github.com/vmware/govmomi/object"
    29  	"github.com/vmware/govmomi/vim25/types"
    30  )
    31  
    32  type ls struct {
    33  	*permissions.PermissionFlag
    34  }
    35  
    36  func init() {
    37  	cli.Register("role.ls", &ls{})
    38  }
    39  
    40  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.PermissionFlag, ctx = permissions.NewPermissionFlag(ctx)
    42  	cmd.PermissionFlag.Register(ctx, f)
    43  }
    44  
    45  func (cmd *ls) Process(ctx context.Context) error {
    46  	if err := cmd.PermissionFlag.Process(ctx); err != nil {
    47  		return err
    48  	}
    49  	return nil
    50  }
    51  
    52  func (cmd *ls) Usage() string {
    53  	return "[NAME]"
    54  }
    55  
    56  func (cmd *ls) Description() string {
    57  	return `List authorization roles.
    58  
    59  If NAME is provided, list privileges for the role.
    60  
    61  Examples:
    62    govc role.ls
    63    govc role.ls Admin`
    64  }
    65  
    66  type lsRoleList object.AuthorizationRoleList
    67  
    68  func (rl lsRoleList) Write(w io.Writer) error {
    69  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    70  
    71  	for _, role := range rl {
    72  		fmt.Fprintf(tw, "%s\t%s\n", role.Name, role.Info.GetDescription().Summary)
    73  	}
    74  
    75  	return tw.Flush()
    76  }
    77  
    78  type lsRole types.AuthorizationRole
    79  
    80  func (r lsRole) Write(w io.Writer) error {
    81  	for _, p := range r.Privilege {
    82  		fmt.Println(p)
    83  	}
    84  	return nil
    85  }
    86  
    87  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    88  	if f.NArg() > 1 {
    89  		return flag.ErrHelp
    90  	}
    91  
    92  	_, err := cmd.Manager(ctx)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	if f.NArg() == 1 {
    98  		role, err := cmd.Role(f.Arg(0))
    99  		if err != nil {
   100  			return err
   101  		}
   102  
   103  		return cmd.WriteResult(lsRole(*role))
   104  	}
   105  
   106  	return cmd.WriteResult(lsRoleList(cmd.Roles))
   107  }