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

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/command/flag"
     6  )
     7  
     8  type DeleteUserCommand struct {
     9  	BaseCommand
    10  
    11  	RequiredArgs    flag.Username `positional-args:"yes"`
    12  	Force           bool          `short:"f" description:"Prompt interactively for password"`
    13  	Origin          string        `long:"origin" description:"Origin for mapping a user account to a user in an external identity provider"`
    14  	usage           interface{}   `usage:"CF_NAME delete-user USERNAME [-f]\n   CF_NAME delete-user USERNAME [--origin ORIGIN]\n\nEXAMPLES:\n   cf delete-user jsmith                   # internal user\n   cf delete-user jsmith --origin ldap     # LDAP user"`
    15  	relatedCommands interface{}   `related_commands:"org-users"`
    16  }
    17  
    18  func (cmd *DeleteUserCommand) Execute(args []string) error {
    19  	err := cmd.SharedActor.CheckTarget(false, false)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	if !cmd.Force {
    25  		promptMessage := "Really delete the user {{.TargetUser}}?"
    26  		deleteUser, promptErr := cmd.UI.DisplayBoolPrompt(false, promptMessage, map[string]interface{}{"TargetUser": cmd.RequiredArgs.Username})
    27  		if promptErr != nil {
    28  			return nil
    29  		}
    30  
    31  		if !deleteUser {
    32  			cmd.UI.DisplayText("User '{{.TargetUser}}' has not been deleted.", map[string]interface{}{
    33  				"TargetUser": cmd.RequiredArgs.Username,
    34  			})
    35  			cmd.UI.DisplayOK()
    36  			return nil
    37  		}
    38  	}
    39  
    40  	currentUser, err := cmd.Config.CurrentUserName()
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	cmd.UI.DisplayTextWithFlavor("Deleting user {{.TargetUser}} as {{.CurrentUser}}...", map[string]interface{}{
    46  		"TargetUser":  cmd.RequiredArgs.Username,
    47  		"CurrentUser": currentUser,
    48  	})
    49  
    50  	user, err := cmd.Actor.GetUser(cmd.RequiredArgs.Username, cmd.Origin)
    51  	if err != nil {
    52  		// User never existed
    53  		if _, ok := err.(actionerror.UserNotFoundError); ok {
    54  			cmd.UI.DisplayTextWithFlavor(err.Error())
    55  			cmd.UI.DisplayOK()
    56  			return nil
    57  		}
    58  		return err
    59  	}
    60  
    61  	warnings, err := cmd.Actor.DeleteUser(user.GUID)
    62  
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	cmd.UI.DisplayWarnings(warnings)
    68  	cmd.UI.DisplayOK()
    69  
    70  	return nil
    71  }