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

     1  package application
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/applications"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type Restage struct {
    18  	ui                terminal.UI
    19  	config            coreconfig.Reader
    20  	appRepo           applications.Repository
    21  	appStagingWatcher StagingWatcher
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&Restage{})
    26  }
    27  
    28  func (cmd *Restage) MetaData() commandregistry.CommandMetadata {
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "restage",
    31  		ShortName:   "rg",
    32  		Description: T("Restage an app"),
    33  		Usage: []string{
    34  			T("CF_NAME restage APP_NAME"),
    35  		},
    36  	}
    37  }
    38  
    39  func (cmd *Restage) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    40  	if len(fc.Args()) != 1 {
    41  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("restage"))
    42  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    43  	}
    44  
    45  	reqs := []requirements.Requirement{
    46  		requirementsFactory.NewLoginRequirement(),
    47  		requirementsFactory.NewTargetedSpaceRequirement(),
    48  	}
    49  
    50  	return reqs, nil
    51  }
    52  
    53  func (cmd *Restage) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    54  	cmd.ui = deps.UI
    55  	cmd.config = deps.Config
    56  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    57  
    58  	//get command from registry for dependency
    59  	commandDep := commandregistry.Commands.FindCommand("start")
    60  	commandDep = commandDep.SetDependency(deps, false)
    61  	cmd.appStagingWatcher = commandDep.(StagingWatcher)
    62  
    63  	return cmd
    64  }
    65  
    66  func (cmd *Restage) Execute(c flags.FlagContext) error {
    67  	app, err := cmd.appRepo.Read(c.Args()[0])
    68  	if notFound, ok := err.(*errors.ModelNotFoundError); ok {
    69  		return notFound
    70  	}
    71  
    72  	cmd.ui.Say(T("Restaging app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    73  		map[string]interface{}{
    74  			"AppName":     terminal.EntityNameColor(app.Name),
    75  			"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    76  			"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    77  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    78  		}))
    79  
    80  	app.PackageState = ""
    81  
    82  	_, err = cmd.appStagingWatcher.WatchStaging(app, cmd.config.OrganizationFields().Name, cmd.config.SpaceFields().Name, func(app models.Application) (models.Application, error) {
    83  		return app, cmd.appRepo.CreateRestageRequest(app.GUID)
    84  	})
    85  	if err != nil {
    86  		cmd.ui.Say(T("Failed to watch staging of app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    87  			map[string]interface{}{
    88  				"AppName":     terminal.EntityNameColor(app.Name),
    89  				"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    90  				"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    91  				"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    92  			}))
    93  	}
    94  	return nil
    95  }