github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/space_users_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/v7action"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     6  	"code.cloudfoundry.org/cli/command/flag"
     7  	"code.cloudfoundry.org/cli/resources"
     8  )
     9  
    10  type SpaceUsersCommand struct {
    11  	BaseCommand
    12  
    13  	RequiredArgs    flag.SpaceUsersArgs `positional-args:"yes"`
    14  	usage           interface{}         `usage:"CF_NAME space-users ORG SPACE"`
    15  	relatedCommands interface{}         `related_commands:"org-users, orgs, set-space-role, spaces, unset-space-role"`
    16  }
    17  
    18  func (cmd *SpaceUsersCommand) Execute(args []string) error {
    19  	err := cmd.SharedActor.CheckTarget(false, false)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	user, err := cmd.Config.CurrentUser()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	cmd.UI.DisplayTextWithFlavor("Getting users in org {{.Org}} / space {{.Space}} as {{.CurrentUser}}...", map[string]interface{}{
    30  		"Org":         cmd.RequiredArgs.Organization,
    31  		"Space":       cmd.RequiredArgs.Space,
    32  		"CurrentUser": user.Name,
    33  	})
    34  	cmd.UI.DisplayNewline()
    35  
    36  	org, warnings, err := cmd.Actor.GetOrganizationByName(cmd.RequiredArgs.Organization)
    37  	cmd.UI.DisplayWarnings(warnings)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	space, warnings, err := cmd.Actor.GetSpaceByNameAndOrganization(cmd.RequiredArgs.Space, org.GUID)
    42  	cmd.UI.DisplayWarnings(warnings)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	spaceUsersByRoleType, warnings, err := cmd.Actor.GetSpaceUsersByRoleType(space.GUID)
    48  	cmd.UI.DisplayWarnings(warnings)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	cmd.displaySpaceUsers(spaceUsersByRoleType)
    54  
    55  	return nil
    56  }
    57  
    58  func (cmd SpaceUsersCommand) displaySpaceUsers(orgUsersByRoleType map[constant.RoleType][]resources.User) {
    59  	cmd.displayRoleGroup(orgUsersByRoleType[constant.SpaceManagerRole], "SPACE MANAGER")
    60  	cmd.displayRoleGroup(orgUsersByRoleType[constant.SpaceDeveloperRole], "SPACE DEVELOPER")
    61  	cmd.displayRoleGroup(orgUsersByRoleType[constant.SpaceAuditorRole], "SPACE AUDITOR")
    62  }
    63  
    64  func (cmd SpaceUsersCommand) displayRoleGroup(usersWithRole []resources.User, roleLabel string) {
    65  	v7action.SortUsers(usersWithRole)
    66  
    67  	cmd.UI.DisplayHeader(roleLabel)
    68  	if len(usersWithRole) > 0 {
    69  		for _, userWithRole := range usersWithRole {
    70  			cmd.UI.DisplayText("  {{.PresentationName}} ({{.Origin}})", map[string]interface{}{
    71  				"PresentationName": userWithRole.PresentationName,
    72  				"Origin":           v7action.GetHumanReadableOrigin(userWithRole),
    73  			})
    74  		}
    75  	} else {
    76  		cmd.UI.DisplayText("  No {{.RoleLabel}} found", map[string]interface{}{
    77  			"RoleLabel": roleLabel,
    78  		})
    79  	}
    80  
    81  	cmd.UI.DisplayNewline()
    82  }