github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/route/unmap_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/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 UnmapRoute struct {
    16  	ui        terminal.UI
    17  	config    coreconfig.Reader
    18  	routeRepo api.RouteRepository
    19  	appReq    requirements.ApplicationRequirement
    20  	domainReq requirements.DomainRequirement
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&UnmapRoute{})
    25  }
    26  
    27  func (cmd *UnmapRoute) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["hostname"] = &flags.StringFlag{Name: "hostname", ShortName: "n", Usage: T("Hostname used to identify the HTTP route")}
    30  	fs["path"] = &flags.StringFlag{Name: "path", Usage: T("Path used to identify the HTTP route")}
    31  	fs["port"] = &flags.IntFlag{Name: "port", Usage: T("Port used to identify the TCP route")}
    32  
    33  	return commandregistry.CommandMetadata{
    34  		Name:        "unmap-route",
    35  		Description: T("Remove a url route from an app"),
    36  		Usage: []string{
    37  			fmt.Sprintf("%s:\n", T("Unmap an HTTP route")),
    38  			"      CF_NAME unmap-route ",
    39  			fmt.Sprintf("%s ", T("APP_NAME")),
    40  			fmt.Sprintf("%s ", T("DOMAIN")),
    41  			fmt.Sprintf("[--hostname %s] ", T("HOSTNAME")),
    42  			fmt.Sprintf("[--path %s]\n\n", T("PATH")),
    43  			fmt.Sprintf("   %s:\n", T("Unmap a TCP route")),
    44  			"      CF_NAME unmap-route ",
    45  			fmt.Sprintf("%s ", T("APP_NAME")),
    46  			fmt.Sprintf("%s ", T("DOMAIN")),
    47  			fmt.Sprintf("--port %s", T("PORT")),
    48  		},
    49  		Examples: []string{
    50  			"CF_NAME unmap-route my-app example.com                              # example.com",
    51  			"CF_NAME unmap-route my-app example.com --hostname myhost            # myhost.example.com",
    52  			"CF_NAME unmap-route my-app example.com --hostname myhost --path foo # myhost.example.com/foo",
    53  			"CF_NAME unmap-route my-app example.com --port 5000                  # example.com:5000",
    54  		},
    55  		Flags: fs,
    56  	}
    57  }
    58  
    59  func (cmd *UnmapRoute) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    60  	if len(fc.Args()) != 2 {
    61  		cmd.ui.Failed(T("Incorrect Usage. Requires app_name, domain_name as arguments\n\n") + commandregistry.Commands.CommandUsage("unmap-route"))
    62  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    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  	domainName := fc.Args()[1]
    71  
    72  	cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0])
    73  	cmd.domainReq = requirementsFactory.NewDomainRequirement(domainName)
    74  
    75  	var reqs []requirements.Requirement
    76  
    77  	reqs = append(reqs, []requirements.Requirement{
    78  		requirementsFactory.NewLoginRequirement(),
    79  		cmd.appReq,
    80  		cmd.domainReq,
    81  	}...)
    82  
    83  	return reqs, nil
    84  }
    85  
    86  func (cmd *UnmapRoute) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    87  	cmd.ui = deps.UI
    88  	cmd.config = deps.Config
    89  	cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
    90  	return cmd
    91  }
    92  
    93  func (cmd *UnmapRoute) Execute(c flags.FlagContext) error {
    94  	hostName := c.String("n")
    95  	path := c.String("path")
    96  	port := c.Int("port")
    97  	domain := cmd.domainReq.GetDomain()
    98  	app := cmd.appReq.GetApplication()
    99  
   100  	route, err := cmd.routeRepo.Find(hostName, domain, path, port)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	cmd.ui.Say(T("Removing route {{.URL}} from app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
   106  		map[string]interface{}{
   107  			"URL":       terminal.EntityNameColor(route.URL()),
   108  			"AppName":   terminal.EntityNameColor(app.Name),
   109  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
   110  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
   111  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
   112  
   113  	var routeFound bool
   114  	for _, routeApp := range route.Apps {
   115  		if routeApp.GUID == app.GUID {
   116  			routeFound = true
   117  			err = cmd.routeRepo.Unbind(route.GUID, app.GUID)
   118  			if err != nil {
   119  				return err
   120  			}
   121  			break
   122  		}
   123  	}
   124  
   125  	cmd.ui.Ok()
   126  
   127  	if !routeFound {
   128  		cmd.ui.Warn(T("\nRoute to be unmapped is not currently mapped to the application."))
   129  	}
   130  	return nil
   131  }