github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/cf/commands/route/unmap_route.go (about)

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