github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/user/delete_user.go (about) 1 package user 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/api" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/errors" 10 "code.cloudfoundry.org/cli/cf/flags" 11 . "code.cloudfoundry.org/cli/cf/i18n" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 type DeleteUser struct { 17 ui terminal.UI 18 config coreconfig.Reader 19 userRepo api.UserRepository 20 } 21 22 func init() { 23 commandregistry.Register(&DeleteUser{}) 24 } 25 26 func (cmd *DeleteUser) MetaData() commandregistry.CommandMetadata { 27 fs := make(map[string]flags.FlagSet) 28 fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")} 29 30 return commandregistry.CommandMetadata{ 31 Name: "delete-user", 32 Description: T("Delete a user"), 33 Usage: []string{ 34 T("CF_NAME delete-user USERNAME [-f]"), 35 }, 36 Flags: fs, 37 } 38 } 39 40 func (cmd *DeleteUser) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 41 if len(fc.Args()) != 1 { 42 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-user")) 43 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 44 } 45 46 reqs := []requirements.Requirement{ 47 requirementsFactory.NewLoginRequirement(), 48 } 49 50 return reqs, nil 51 } 52 53 func (cmd *DeleteUser) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 54 cmd.ui = deps.UI 55 cmd.config = deps.Config 56 cmd.userRepo = deps.RepoLocator.GetUserRepository() 57 return cmd 58 } 59 60 func (cmd *DeleteUser) Execute(c flags.FlagContext) error { 61 username := c.Args()[0] 62 force := c.Bool("f") 63 64 if !force && !cmd.ui.ConfirmDelete(T("user"), username) { 65 return nil 66 } 67 68 cmd.ui.Say(T("Deleting user {{.TargetUser}} as {{.CurrentUser}}...", 69 map[string]interface{}{ 70 "TargetUser": terminal.EntityNameColor(username), 71 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 72 })) 73 74 users, err := cmd.userRepo.FindAllByUsername(username) 75 76 switch err.(type) { 77 case nil: 78 if len(users) > 1 { 79 return fmt.Errorf(T( 80 "Error deleting user {{.Username}} \nThe user exists in multiple origins.", 81 map[string]interface{}{ 82 "Username": username, 83 })) 84 } 85 case *errors.ModelNotFoundError: 86 cmd.ui.Ok() 87 cmd.ui.Warn(T("User {{.TargetUser}} does not exist.", map[string]interface{}{"TargetUser": username})) 88 return nil 89 default: 90 return err 91 } 92 93 err = cmd.userRepo.Delete(users[0].GUID) 94 if err != nil { 95 return err 96 } 97 98 cmd.ui.Ok() 99 return nil 100 }