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

     1  package route
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf"
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/models"
    14  	"code.cloudfoundry.org/cli/cf/requirements"
    15  	"code.cloudfoundry.org/cli/cf/terminal"
    16  )
    17  
    18  type DeleteRoute struct {
    19  	ui        terminal.UI
    20  	config    coreconfig.Reader
    21  	routeRepo api.RouteRepository
    22  	domainReq requirements.DomainRequirement
    23  }
    24  
    25  func init() {
    26  	commandregistry.Register(&DeleteRoute{})
    27  }
    28  
    29  func (cmd *DeleteRoute) MetaData() commandregistry.CommandMetadata {
    30  	fs := make(map[string]flags.FlagSet)
    31  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    32  	fs["hostname"] = &flags.StringFlag{Name: "hostname", ShortName: "n", Usage: T("Hostname used to identify the HTTP route")}
    33  	fs["path"] = &flags.StringFlag{Name: "path", Usage: T("Path used to identify the HTTP route")}
    34  	fs["port"] = &flags.IntFlag{Name: "port", Usage: T("Port used to identify the TCP route")}
    35  
    36  	return commandregistry.CommandMetadata{
    37  		Name:        "delete-route",
    38  		Description: T("Delete a route"),
    39  		Usage: []string{
    40  			fmt.Sprintf("%s:\n", T("Delete an HTTP route")),
    41  			"      CF_NAME delete-route ",
    42  			fmt.Sprintf("%s ", T("DOMAIN")),
    43  			fmt.Sprintf("[--hostname %s] ", T("HOSTNAME")),
    44  			fmt.Sprintf("[--path %s] [-f]\n\n", T("PATH")),
    45  			fmt.Sprintf("   %s:\n", T("Delete a TCP route")),
    46  			"      CF_NAME delete-route ",
    47  			fmt.Sprintf("%s ", T("DOMAIN")),
    48  			fmt.Sprintf("--port %s [-f]", T("PORT")),
    49  		},
    50  		Examples: []string{
    51  			"CF_NAME delete-route example.com                              # example.com",
    52  			"CF_NAME delete-route example.com --hostname myhost            # myhost.example.com",
    53  			"CF_NAME delete-route example.com --hostname myhost --path foo # myhost.example.com/foo",
    54  			"CF_NAME delete-route example.com --port 50000                 # example.com:50000",
    55  		},
    56  		Flags: fs,
    57  	}
    58  }
    59  
    60  func (cmd *DeleteRoute) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    61  	if len(fc.Args()) != 1 {
    62  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-route"))
    63  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    64  	}
    65  
    66  	if fc.IsSet("port") && (fc.IsSet("hostname") || fc.IsSet("path")) {
    67  		cmd.ui.Failed(T("Cannot specify port together with hostname and/or path."))
    68  		return nil, fmt.Errorf("Cannot specify port together with hostname and/or path.")
    69  	}
    70  
    71  	cmd.domainReq = requirementsFactory.NewDomainRequirement(fc.Args()[0])
    72  
    73  	var reqs []requirements.Requirement
    74  
    75  	if fc.String("path") != "" {
    76  		reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '--path'", cf.RoutePathMinimumAPIVersion))
    77  	}
    78  
    79  	if fc.IsSet("port") {
    80  		reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '--port'", cf.TCPRoutingMinimumAPIVersion))
    81  	}
    82  
    83  	reqs = append(reqs, []requirements.Requirement{
    84  		requirementsFactory.NewLoginRequirement(),
    85  		cmd.domainReq,
    86  	}...)
    87  
    88  	return reqs, nil
    89  }
    90  
    91  func (cmd *DeleteRoute) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    92  	cmd.ui = deps.UI
    93  	cmd.config = deps.Config
    94  	cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
    95  	return cmd
    96  }
    97  
    98  func (cmd *DeleteRoute) Execute(c flags.FlagContext) error {
    99  	host := c.String("n")
   100  	path := c.String("path")
   101  	domainName := c.Args()[0]
   102  	port := c.Int("port")
   103  
   104  	url := (&models.RoutePresenter{
   105  		Host:   host,
   106  		Domain: domainName,
   107  		Path:   path,
   108  		Port:   port,
   109  	}).URL()
   110  
   111  	if !c.Bool("f") {
   112  		if !cmd.ui.ConfirmDelete("route", url) {
   113  			return nil
   114  		}
   115  	}
   116  
   117  	cmd.ui.Say(T("Deleting route {{.URL}}...", map[string]interface{}{"URL": terminal.EntityNameColor(url)}))
   118  
   119  	domain := cmd.domainReq.GetDomain()
   120  	route, err := cmd.routeRepo.Find(host, domain, path, port)
   121  	if err != nil {
   122  		if _, ok := err.(*errors.ModelNotFoundError); ok {
   123  			cmd.ui.Warn(T("Unable to delete, route '{{.URL}}' does not exist.",
   124  				map[string]interface{}{"URL": url}))
   125  			return nil
   126  		}
   127  		return err
   128  	}
   129  
   130  	err = cmd.routeRepo.Delete(route.GUID)
   131  	if err != nil {
   132  		return err
   133  	}
   134  
   135  	cmd.ui.Ok()
   136  	return nil
   137  }