github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/application/stop.go (about)

     1  package application
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/flags"
     7  	. "code.cloudfoundry.org/cli/cf/i18n"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/applications"
    10  	"code.cloudfoundry.org/cli/cf/commandregistry"
    11  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  //go:generate counterfeiter . Stopper
    18  
    19  type Stopper interface {
    20  	commandregistry.Command
    21  	ApplicationStop(app models.Application, orgName string, spaceName string) (updatedApp models.Application, err error)
    22  }
    23  
    24  type Stop struct {
    25  	ui      terminal.UI
    26  	config  coreconfig.Reader
    27  	appRepo applications.Repository
    28  	appReq  requirements.ApplicationRequirement
    29  }
    30  
    31  func init() {
    32  	commandregistry.Register(&Stop{})
    33  }
    34  
    35  func (cmd *Stop) MetaData() commandregistry.CommandMetadata {
    36  	return commandregistry.CommandMetadata{
    37  		Name:        "stop",
    38  		ShortName:   "sp",
    39  		Description: T("Stop an app"),
    40  		Usage: []string{
    41  			T("CF_NAME stop APP_NAME"),
    42  		},
    43  	}
    44  }
    45  
    46  func (cmd *Stop) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    47  	if len(fc.Args()) != 1 {
    48  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("stop"))
    49  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    50  	}
    51  
    52  	cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0])
    53  
    54  	reqs := []requirements.Requirement{
    55  		requirementsFactory.NewLoginRequirement(),
    56  		requirementsFactory.NewTargetedSpaceRequirement(),
    57  		cmd.appReq,
    58  	}
    59  
    60  	return reqs, nil
    61  }
    62  
    63  func (cmd *Stop) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    64  	cmd.ui = deps.UI
    65  	cmd.config = deps.Config
    66  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    67  	return cmd
    68  }
    69  
    70  func (cmd *Stop) ApplicationStop(app models.Application, orgName, spaceName string) (models.Application, error) {
    71  	var updatedApp models.Application
    72  
    73  	if app.State == models.ApplicationStateStopped {
    74  		updatedApp = app
    75  		return updatedApp, nil
    76  	}
    77  
    78  	cmd.ui.Say(T("Stopping app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    79  		map[string]interface{}{
    80  			"AppName":     terminal.EntityNameColor(app.Name),
    81  			"OrgName":     terminal.EntityNameColor(orgName),
    82  			"SpaceName":   terminal.EntityNameColor(spaceName),
    83  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username())}))
    84  
    85  	state := "STOPPED"
    86  	updatedApp, err := cmd.appRepo.Update(app.GUID, models.AppParams{State: &state})
    87  	if err != nil {
    88  		return models.Application{}, err
    89  	}
    90  
    91  	cmd.ui.Ok()
    92  	return updatedApp, nil
    93  }
    94  
    95  func (cmd *Stop) Execute(c flags.FlagContext) error {
    96  	app := cmd.appReq.GetApplication()
    97  	if app.State == models.ApplicationStateStopped {
    98  		cmd.ui.Say(terminal.WarningColor(T("App ") + app.Name + T(" is already stopped")))
    99  	} else {
   100  		_, err := cmd.ApplicationStop(app, cmd.config.OrganizationFields().Name, cmd.config.SpaceFields().Name)
   101  		if err != nil {
   102  			return err
   103  		}
   104  	}
   105  	return nil
   106  }