github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v7/create_route_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  	"code.cloudfoundry.org/cli/command/v7/shared"
    10  )
    11  
    12  //go:generate counterfeiter . CreateRouteActor
    13  
    14  type CreateRouteActor interface {
    15  	CreateRoute(orgName, spaceName, domainName, hostname, path string) (v7action.Warnings, error)
    16  }
    17  
    18  type CreateRouteCommand struct {
    19  	RequiredArgs    flag.Domain `positional-args:"yes"`
    20  	usage           interface{} `usage:"CF_NAME create-route DOMAIN [--hostname HOSTNAME] [--path PATH]\n\nEXAMPLES:\n   CF_NAME create-route example.com                             # example.com\n   CF_NAME create-route example.com --hostname myapp            # myapp.example.com\n   CF_NAME create-route example.com --hostname myapp --path foo # myapp.example.com/foo"`
    21  	Hostname        string      `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"`
    22  	Path            string      `long:"path" description:"Path for the HTTP route"`
    23  	relatedCommands interface{} `related_commands:"check-route, domains, map-route, routes, unmap-route"`
    24  
    25  	UI          command.UI
    26  	Config      command.Config
    27  	Actor       CreateRouteActor
    28  	SharedActor command.SharedActor
    29  }
    30  
    31  func (cmd *CreateRouteCommand) Setup(config command.Config, ui command.UI) error {
    32  	cmd.UI = ui
    33  	cmd.Config = config
    34  	sharedActor := sharedaction.NewActor(config)
    35  	cmd.SharedActor = sharedActor
    36  
    37  	ccClient, uaaClient, err := shared.NewClients(config, ui, true, "")
    38  	if err != nil {
    39  		return err
    40  	}
    41  	cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient)
    42  	return nil
    43  }
    44  
    45  func (cmd CreateRouteCommand) Execute(args []string) error {
    46  	err := cmd.SharedActor.CheckTarget(true, true)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	user, err := cmd.Config.CurrentUser()
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	domain := cmd.RequiredArgs.Domain
    57  	hostname := cmd.Hostname
    58  	pathName := cmd.Path
    59  	spaceName := cmd.Config.TargetedSpace().Name
    60  	orgName := cmd.Config.TargetedOrganization().Name
    61  	fqdn := desiredFQDN(domain, hostname, pathName)
    62  
    63  	cmd.UI.DisplayTextWithFlavor("Creating route {{.FQDN}} for org {{.Organization}} / space {{.Space}} as {{.User}}...",
    64  		map[string]interface{}{
    65  			"FQDN":         fqdn,
    66  			"User":         user.Name,
    67  			"Space":        spaceName,
    68  			"Organization": orgName,
    69  		})
    70  
    71  	warnings, err := cmd.Actor.CreateRoute(orgName, spaceName, domain, hostname, pathName)
    72  
    73  	cmd.UI.DisplayWarnings(warnings)
    74  	if err != nil {
    75  		if _, ok := err.(actionerror.RouteAlreadyExistsError); ok {
    76  			cmd.UI.DisplayText(err.Error())
    77  			cmd.UI.DisplayOK()
    78  			return nil
    79  		}
    80  		return err
    81  	}
    82  
    83  	cmd.UI.DisplayText("Route {{.FQDN}} has been created.",
    84  		map[string]interface{}{
    85  			"FQDN": fqdn,
    86  		})
    87  
    88  	cmd.UI.DisplayOK()
    89  	return nil
    90  }
    91  
    92  func desiredFQDN(domain, hostname, path string) string {
    93  	fqdn := ""
    94  
    95  	if hostname != "" {
    96  		fqdn += hostname + "."
    97  	}
    98  	fqdn += domain
    99  
   100  	if path != "" {
   101  		if string(path[0]) == "/" {
   102  			fqdn += path
   103  		} else {
   104  			fqdn += "/" + path
   105  		}
   106  	}
   107  
   108  	return fqdn
   109  }