(ctx context.Context, flags *RootFlags)
| 39 | } |
| 40 | |
| 41 | func (c *GroupsListCmd) Run(ctx context.Context, flags *RootFlags) error { |
| 42 | u := ui.FromContext(ctx) |
| 43 | if c.Max <= 0 { |
| 44 | return usage("max must be > 0") |
| 45 | } |
| 46 | account, err := requireGroupsAccount(flags) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | svc, err := cloudIdentityService(ctx, account) |
| 52 | if err != nil { |
| 53 | return wrapCloudIdentityError(err, account) |
| 54 | } |
| 55 | |
| 56 | // Search for all groups the user belongs to |
| 57 | // Using "groups/-" as parent searches across all groups |
| 58 | fetch := func(pageToken string) ([]*cloudidentity.GroupRelation, string, error) { |
| 59 | call := svc.Groups.Memberships.SearchTransitiveGroups("groups/-"). |
| 60 | Query(searchTransitiveGroupsQuery(account)). |
| 61 | PageSize(c.Max). |
| 62 | Context(ctx) |
| 63 | if strings.TrimSpace(pageToken) != "" { |
| 64 | call = call.PageToken(pageToken) |
| 65 | } |
| 66 | resp, callErr := call.Do() |
| 67 | if callErr != nil { |
| 68 | return nil, "", wrapCloudIdentityError(callErr, account) |
| 69 | } |
| 70 | return resp.Memberships, resp.NextPageToken, nil |
| 71 | } |
| 72 | |
| 73 | memberships, nextPageToken, err := loadPagedItems(c.Page, c.All, fetch) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | if outfmt.IsJSON(ctx) { |
| 79 | type item struct { |
| 80 | GroupName string `json:"groupName"` |
| 81 | DisplayName string `json:"displayName,omitempty"` |
| 82 | Role string `json:"role,omitempty"` |
| 83 | } |
| 84 | items := make([]item, 0, len(memberships)) |
| 85 | for _, m := range memberships { |
| 86 | if m == nil { |
| 87 | continue |
| 88 | } |
| 89 | items = append(items, item{ |
| 90 | GroupName: m.GroupKey.Id, |
| 91 | DisplayName: m.DisplayName, |
| 92 | Role: getRelationType(m.RelationType), |
| 93 | }) |
| 94 | } |
| 95 | if err := outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{ |
| 96 | "groups": items, |
| 97 | "nextPageToken": nextPageToken, |
| 98 | }); err != nil { |
nothing calls this directly
no test coverage detected