github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/application/restart_app_instance.go (about)

     1  package application
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/appinstances"
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type RestartAppInstance struct {
    18  	ui               terminal.UI
    19  	config           coreconfig.Reader
    20  	appReq           requirements.ApplicationRequirement
    21  	appInstancesRepo appinstances.Repository
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&RestartAppInstance{})
    26  }
    27  
    28  func (cmd *RestartAppInstance) MetaData() commandregistry.CommandMetadata {
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "restart-app-instance",
    31  		Description: T("Terminate the running application Instance at the given index and instantiate a new instance of the application with the same index"),
    32  		Usage: []string{
    33  			T("CF_NAME restart-app-instance APP_NAME INDEX"),
    34  		},
    35  	}
    36  }
    37  
    38  func (cmd *RestartAppInstance) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    39  	if len(fc.Args()) != 2 {
    40  		usage := commandregistry.Commands.CommandUsage("restart-app-instance")
    41  		cmd.ui.Failed(T("Incorrect Usage. Requires arguments\n\n") + usage)
    42  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    43  	}
    44  
    45  	appName := fc.Args()[0]
    46  
    47  	cmd.appReq = requirementsFactory.NewApplicationRequirement(appName)
    48  
    49  	reqs := []requirements.Requirement{
    50  		requirementsFactory.NewLoginRequirement(),
    51  		requirementsFactory.NewTargetedSpaceRequirement(),
    52  		cmd.appReq,
    53  	}
    54  
    55  	return reqs, nil
    56  }
    57  
    58  func (cmd *RestartAppInstance) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    59  	cmd.ui = deps.UI
    60  	cmd.config = deps.Config
    61  	cmd.appInstancesRepo = deps.RepoLocator.GetAppInstancesRepository()
    62  	return cmd
    63  }
    64  
    65  func (cmd *RestartAppInstance) Execute(fc flags.FlagContext) error {
    66  	app := cmd.appReq.GetApplication()
    67  
    68  	instance, err := strconv.Atoi(fc.Args()[1])
    69  
    70  	if err != nil {
    71  		return errors.New(T("Instance must be a non-negative integer"))
    72  	}
    73  
    74  	cmd.ui.Say(T("Restarting instance {{.Instance}} of application {{.AppName}} as {{.Username}}",
    75  		map[string]interface{}{
    76  			"Instance": instance,
    77  			"AppName":  terminal.EntityNameColor(app.Name),
    78  			"Username": terminal.EntityNameColor(cmd.config.Username()),
    79  		}))
    80  
    81  	err = cmd.appInstancesRepo.DeleteInstance(app.GUID, instance)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	cmd.ui.Ok()
    87  	cmd.ui.Say("")
    88  	return nil
    89  }