| 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 |
|
| 38 |
func init() { |
| 39 |
cli.Register("sso.group.ls", &ls{}) |
| 40 |
} |
| 41 |
|
| 42 |
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { |
| 43 |
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) |
| 44 |
cmd.ClientFlag.Register(ctx, f) |
| 45 |
|
| 46 |
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) |
| 47 |
cmd.OutputFlag.Register(ctx, f) |
| 48 |
} |
| 49 |
|
| 50 |
func (cmd *ls) Description() string { |
| 51 |
return `List SSO groups. |
| 52 |
|
| 53 |
Examples: |
| 54 |
govc sso.group.ls -s` |
| 55 |
} |
| 56 |
|
| 57 |
func (cmd *ls) Process(ctx context.Context) error { |
| 58 |
if err := cmd.ClientFlag.Process(ctx); err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
return cmd.OutputFlag.Process(ctx) |
| 62 |
} |
| 63 |
|
| 64 |
type groupResult []types.AdminGroup |
| 65 |
|
| 66 |
func (r groupResult) Dump() interface{} { |
| 67 |
return []types.AdminGroup(r) |
| 68 |
} |
| 69 |
|
| 70 |
func (r groupResult) Write(w io.Writer) error { |
| 71 |
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) |
| 72 |
for _, info := range r { |
| 73 |
fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Details.Description) |
| 74 |
} |
| 75 |
return tw.Flush() |
| 76 |
} |
| 77 |
|
| 78 |
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { |
| 79 |
arg := f.Arg(0) |
| 80 |
|
| 81 |
return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error { |
| 82 |
info, err := c.FindGroups(ctx, arg) |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
|
| 87 |
return cmd.WriteResult(groupResult(info)) |
| 88 |
}) |
| 89 |
} |