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

     1  package route
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf"
     8  	"code.cloudfoundry.org/cli/cf/api"
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type CheckRoute struct {
    18  	ui         terminal.UI
    19  	config     coreconfig.Reader
    20  	routeRepo  api.RouteRepository
    21  	domainRepo api.DomainRepository
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&CheckRoute{})
    26  }
    27  
    28  func (cmd *CheckRoute) MetaData() commandregistry.CommandMetadata {
    29  	fs := make(map[string]flags.FlagSet)
    30  	fs["path"] = &flags.StringFlag{Name: "path", Usage: T("Path for the route")}
    31  
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "check-route",
    34  		Description: T("Perform a simple check to determine whether a route currently exists or not"),
    35  		Usage: []string{
    36  			T("CF_NAME check-route HOST DOMAIN [--path PATH]"),
    37  		},
    38  		Examples: []string{
    39  			"CF_NAME check-route myhost example.com            # example.com",
    40  			"CF_NAME check-route myhost example.com --path foo # myhost.example.com/foo",
    41  		},
    42  		Flags: fs,
    43  	}
    44  }
    45  
    46  func (cmd *CheckRoute) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    47  	if len(fc.Args()) != 2 {
    48  		cmd.ui.Failed(T("Incorrect Usage. Requires host and domain as arguments\n\n") + commandregistry.Commands.CommandUsage("check-route"))
    49  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    50  	}
    51  
    52  	var reqs []requirements.Requirement
    53  
    54  	if fc.String("path") != "" {
    55  		reqs = append(reqs, requirementsFactory.NewMinAPIVersionRequirement("Option '--path'", cf.RoutePathMinimumAPIVersion))
    56  	}
    57  
    58  	reqs = append(reqs, []requirements.Requirement{
    59  		requirementsFactory.NewTargetedOrgRequirement(),
    60  		requirementsFactory.NewLoginRequirement(),
    61  	}...)
    62  
    63  	return reqs, nil
    64  }
    65  
    66  func (cmd *CheckRoute) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    67  	cmd.ui = deps.UI
    68  	cmd.config = deps.Config
    69  	cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
    70  	cmd.domainRepo = deps.RepoLocator.GetDomainRepository()
    71  	return cmd
    72  }
    73  
    74  func (cmd *CheckRoute) Execute(c flags.FlagContext) error {
    75  	hostName := c.Args()[0]
    76  	domainName := c.Args()[1]
    77  	path := c.String("path")
    78  
    79  	cmd.ui.Say(T("Checking for route..."))
    80  
    81  	exists, err := cmd.CheckRoute(hostName, domainName, path)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	cmd.ui.Ok()
    87  
    88  	var existence string
    89  	if exists {
    90  		existence = "does exist"
    91  	} else {
    92  		existence = "does not exist"
    93  	}
    94  
    95  	if path != "" {
    96  		cmd.ui.Say(T("Route {{.HostName}}.{{.DomainName}}/{{.Path}} {{.Existence}}",
    97  			map[string]interface{}{
    98  				"HostName":   hostName,
    99  				"DomainName": domainName,
   100  				"Existence":  existence,
   101  				"Path":       strings.TrimPrefix(path, `/`),
   102  			},
   103  		))
   104  	} else {
   105  		cmd.ui.Say(T("Route {{.HostName}}.{{.DomainName}} {{.Existence}}",
   106  			map[string]interface{}{
   107  				"HostName":   hostName,
   108  				"DomainName": domainName,
   109  				"Existence":  existence,
   110  			},
   111  		))
   112  	}
   113  	return nil
   114  }
   115  
   116  func (cmd *CheckRoute) CheckRoute(hostName, domainName, path string) (bool, error) {
   117  	orgGUID := cmd.config.OrganizationFields().GUID
   118  	domain, err := cmd.domainRepo.FindByNameInOrg(domainName, orgGUID)
   119  	if err != nil {
   120  		return false, err
   121  	}
   122  
   123  	found, err := cmd.routeRepo.CheckIfExists(hostName, domain, path)
   124  	if err != nil {
   125  		return false, err
   126  	}
   127  
   128  	return found, nil
   129  }