github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/route/delete_route.go (about)

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