forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_user.go
71 lines (59 loc) · 1.48 KB
/
delete_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package user
import (
"cf/api"
"cf/configuration"
"cf/requirements"
"cf/terminal"
"errors"
"github.com/codegangsta/cli"
)
type DeleteUser struct {
ui terminal.UI
config *configuration.Configuration
userRepo api.UserRepository
}
func NewDeleteUser(ui terminal.UI, config *configuration.Configuration, userRepo api.UserRepository) (cmd DeleteUser) {
cmd.ui = ui
cmd.config = config
cmd.userRepo = userRepo
return
}
func (cmd DeleteUser) GetRequirements(reqFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
err = errors.New("Invalid usage")
cmd.ui.FailWithUsage(c, "delete-user")
return
}
reqs = append(reqs, reqFactory.NewLoginRequirement())
return
}
func (cmd DeleteUser) Run(c *cli.Context) {
username := c.Args()[0]
force := c.Bool("f")
if !force && !cmd.ui.Confirm("Really delete user %s?%s",
terminal.EntityNameColor(username),
terminal.PromptColor(">"),
) {
return
}
cmd.ui.Say("Deleting user %s as %s...",
terminal.EntityNameColor(username),
terminal.EntityNameColor(cmd.config.Username()),
)
user, apiResponse := cmd.userRepo.FindByUsername(username)
if apiResponse.IsError() {
cmd.ui.Failed(apiResponse.Message)
return
}
if apiResponse.IsNotFound() {
cmd.ui.Ok()
cmd.ui.Warn("User %s does not exist.", username)
return
}
apiResponse = cmd.userRepo.Delete(user)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
cmd.ui.Ok()
}