github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/delete.go (about)

     1  package application
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api"
     5  	"code.cloudfoundry.org/cli/cf/api/applications"
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/errors"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  )
    14  
    15  type DeleteApp struct {
    16  	ui        terminal.UI
    17  	config    coreconfig.Reader
    18  	appRepo   applications.Repository
    19  	routeRepo api.RouteRepository
    20  	appReq    requirements.ApplicationRequirement
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&DeleteApp{})
    25  }
    26  
    27  func (cmd *DeleteApp) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    30  	fs["r"] = &flags.BoolFlag{ShortName: "r", Usage: T("Also delete any mapped routes")}
    31  
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "delete",
    34  		ShortName:   "d",
    35  		Description: T("Delete an app"),
    36  		Usage: []string{
    37  			T("CF_NAME delete APP_NAME [-f -r]"),
    38  		},
    39  		Flags: fs,
    40  	}
    41  }
    42  
    43  func (cmd *DeleteApp) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    44  	usageReq := requirementsFactory.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    45  		T("Requires app name as argument"),
    46  		func() bool {
    47  			return len(fc.Args()) != 1
    48  		},
    49  	)
    50  
    51  	reqs := []requirements.Requirement{
    52  		usageReq,
    53  		requirementsFactory.NewLoginRequirement(),
    54  		requirementsFactory.NewTargetedSpaceRequirement(),
    55  	}
    56  
    57  	return reqs, nil
    58  }
    59  
    60  func (cmd *DeleteApp) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    61  	cmd.ui = deps.UI
    62  	cmd.config = deps.Config
    63  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    64  	cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
    65  	return cmd
    66  }
    67  
    68  func (cmd *DeleteApp) Execute(c flags.FlagContext) error {
    69  	appName := c.Args()[0]
    70  
    71  	if !c.Bool("f") {
    72  		response := cmd.ui.ConfirmDelete(T("app"), appName)
    73  		if !response {
    74  			return nil
    75  		}
    76  	}
    77  
    78  	cmd.ui.Say(T("Deleting app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
    79  		map[string]interface{}{
    80  			"AppName":   terminal.EntityNameColor(appName),
    81  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    82  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    83  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    84  
    85  	app, err := cmd.appRepo.Read(appName)
    86  
    87  	switch err.(type) {
    88  	case nil: // no error
    89  	case *errors.ModelNotFoundError:
    90  		cmd.ui.Ok()
    91  		cmd.ui.Warn(T("App {{.AppName}} does not exist.", map[string]interface{}{"AppName": appName}))
    92  		return nil
    93  	default:
    94  		return err
    95  	}
    96  
    97  	if c.Bool("r") {
    98  		for _, route := range app.Routes {
    99  			err = cmd.routeRepo.Delete(route.GUID)
   100  			if err != nil {
   101  				return err
   102  			}
   103  		}
   104  	}
   105  
   106  	err = cmd.appRepo.Delete(app.GUID)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	cmd.ui.Ok()
   112  	return nil
   113  }