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

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/command/translatableerror"
     5  )
     6  
     7  type PasswdCommand struct {
     8  	BaseCommand
     9  
    10  	usage interface{} `usage:"CF_NAME passwd"`
    11  }
    12  
    13  func (cmd PasswdCommand) Execute(args []string) error {
    14  	err := cmd.SharedActor.CheckTarget(false, false)
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	currentUser, err := cmd.Config.CurrentUser()
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	currentPassword, err := cmd.UI.DisplayPasswordPrompt("Current password")
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	newPassword, err := cmd.UI.DisplayPasswordPrompt("New password")
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	verifyPassword, err := cmd.UI.DisplayPasswordPrompt("Verify password")
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	cmd.UI.DisplayNewline()
    40  
    41  	cmd.UI.DisplayTextWithFlavor("Changing password for user {{.Username}}...", map[string]interface{}{
    42  		"Username": currentUser.Name,
    43  	})
    44  
    45  	if newPassword != verifyPassword {
    46  		return translatableerror.PasswordVerificationFailedError{}
    47  	}
    48  
    49  	err = cmd.Actor.UpdateUserPassword(currentUser.GUID, currentPassword, newPassword)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	cmd.UI.DisplayOK()
    55  
    56  	cmd.Config.UnsetUserInformation()
    57  	cmd.UI.DisplayText("Please log in again.")
    58  
    59  	return nil
    60  }