forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.go
89 lines (73 loc) · 2.55 KB
/
stop.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package application
import (
"errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/api/applications"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type ApplicationStopper interface {
ApplicationStop(app models.Application, orgName string, spaceName string) (updatedApp models.Application, err error)
}
type Stop struct {
ui terminal.UI
config core_config.Reader
appRepo applications.ApplicationRepository
appReq requirements.ApplicationRequirement
}
func NewStop(ui terminal.UI, config core_config.Reader, appRepo applications.ApplicationRepository) (cmd *Stop) {
cmd = new(Stop)
cmd.ui = ui
cmd.config = config
cmd.appRepo = appRepo
return
}
func (cmd *Stop) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "stop",
ShortName: "sp",
Description: T("Stop an app"),
Usage: T("CF_NAME stop APP_NAME"),
}
}
func (cmd *Stop) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
cmd.ui.FailWithUsage(c)
}
cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
reqs = []requirements.Requirement{requirementsFactory.NewLoginRequirement(), cmd.appReq}
return
}
func (cmd *Stop) ApplicationStop(app models.Application, orgName, spaceName string) (updatedApp models.Application, err error) {
if app.State == "stopped" {
updatedApp = app
return
}
cmd.ui.Say(T("Stopping app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
map[string]interface{}{
"AppName": terminal.EntityNameColor(app.Name),
"OrgName": terminal.EntityNameColor(orgName),
"SpaceName": terminal.EntityNameColor(spaceName),
"CurrentUser": terminal.EntityNameColor(cmd.config.Username())}))
state := "STOPPED"
updatedApp, apiErr := cmd.appRepo.Update(app.Guid, models.AppParams{State: &state})
if apiErr != nil {
err = errors.New(apiErr.Error())
cmd.ui.Failed(apiErr.Error())
return
}
cmd.ui.Ok()
return
}
func (cmd *Stop) Run(c *cli.Context) {
app := cmd.appReq.GetApplication()
if app.State == "stopped" {
cmd.ui.Say(terminal.WarningColor(T("App ") + app.Name + T(" is already stopped")))
} else {
cmd.ApplicationStop(app, cmd.config.OrganizationFields().Name, cmd.config.SpaceFields().Name)
}
}