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

     1  package application
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/flags"
     9  	. "code.cloudfoundry.org/cli/cf/i18n"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  )
    14  
    15  //go:generate counterfeiter . Restarter
    16  
    17  type Restarter interface {
    18  	commandregistry.Command
    19  	ApplicationRestart(app models.Application, orgName string, spaceName string) error
    20  }
    21  
    22  type Restart struct {
    23  	ui      terminal.UI
    24  	config  coreconfig.Reader
    25  	starter Starter
    26  	stopper Stopper
    27  	appReq  requirements.ApplicationRequirement
    28  }
    29  
    30  func init() {
    31  	commandregistry.Register(&Restart{})
    32  }
    33  
    34  func (cmd *Restart) MetaData() commandregistry.CommandMetadata {
    35  	return commandregistry.CommandMetadata{
    36  		Name:        "restart",
    37  		ShortName:   "rs",
    38  		Description: T("Restart an app"),
    39  		Usage: []string{
    40  			T("CF_NAME restart APP_NAME"),
    41  		},
    42  	}
    43  }
    44  
    45  func (cmd *Restart) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    46  	if len(fc.Args()) != 1 {
    47  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("restart"))
    48  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    49  	}
    50  
    51  	cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0])
    52  
    53  	reqs := []requirements.Requirement{
    54  		requirementsFactory.NewLoginRequirement(),
    55  		requirementsFactory.NewTargetedSpaceRequirement(),
    56  		cmd.appReq,
    57  	}
    58  
    59  	return reqs, nil
    60  }
    61  
    62  func (cmd *Restart) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    63  	cmd.ui = deps.UI
    64  	cmd.config = deps.Config
    65  
    66  	//get start for dependency
    67  	starter := commandregistry.Commands.FindCommand("start")
    68  	starter = starter.SetDependency(deps, false)
    69  	cmd.starter = starter.(Starter)
    70  
    71  	//get stop for dependency
    72  	stopper := commandregistry.Commands.FindCommand("stop")
    73  	stopper = stopper.SetDependency(deps, false)
    74  	cmd.stopper = stopper.(Stopper)
    75  
    76  	return cmd
    77  }
    78  
    79  func (cmd *Restart) Execute(c flags.FlagContext) error {
    80  	app := cmd.appReq.GetApplication()
    81  	return cmd.ApplicationRestart(app, cmd.config.OrganizationFields().Name, cmd.config.SpaceFields().Name)
    82  }
    83  
    84  func (cmd *Restart) ApplicationRestart(app models.Application, orgName, spaceName string) error {
    85  	stoppedApp, err := cmd.stopper.ApplicationStop(app, orgName, spaceName)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	cmd.ui.Say("")
    91  
    92  	_, err = cmd.starter.ApplicationStart(stoppedApp, orgName, spaceName)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	return nil
    97  }