github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/route_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
     5  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
     6  
     7  	"strconv"
     8  )
     9  
    10  type RouteCommand struct {
    11  	BaseCommand
    12  
    13  	RequiredArgs    flag.Domain      `positional-args:"yes"`
    14  	Hostname        string           `long:"hostname" short:"n" description:"Hostname used to identify the HTTP route"`
    15  	Path            flag.V7RoutePath `long:"path" description:"Path used to identify the HTTP route"`
    16  	Port            int              `long:"port" description:"Port used to identify the TCP route"`
    17  	relatedCommands interface{}      `related_commands:"create-route, delete-route, routes"`
    18  }
    19  
    20  func (cmd RouteCommand) Usage() string {
    21  	return `
    22  Display an HTTP route:
    23     CF_NAME route DOMAIN [--hostname HOSTNAME] [--path PATH]
    24  
    25  Display a TCP route:
    26     CF_NAME route DOMAIN --port PORT`
    27  }
    28  
    29  func (cmd RouteCommand) Examples() string {
    30  	return `
    31  CF_NAME route example.com                      # example.com
    32  CF_NAME route example.com -n myhost --path foo # myhost.example.com/foo
    33  CF_NAME route example.com --path foo           # example.com/foo
    34  CF_NAME route example.com --port 5000          # example.com:5000`
    35  }
    36  
    37  func (cmd RouteCommand) Execute(args []string) error {
    38  	err := cmd.SharedActor.CheckTarget(true, false)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	user, err := cmd.Actor.GetCurrentUser()
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	domain, warnings, err := cmd.Actor.GetDomainByName(cmd.RequiredArgs.Domain)
    49  
    50  	cmd.UI.DisplayWarnings(warnings)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	hostName := ""
    56  	if cmd.Hostname != "" {
    57  		hostName = cmd.Hostname + "."
    58  	}
    59  
    60  	displayPort := ""
    61  	if cmd.Port != 0 {
    62  		displayPort = ":" + strconv.Itoa(cmd.Port)
    63  
    64  	}
    65  
    66  	cmd.UI.DisplayTextWithFlavor(" Showing route {{.HostName}}{{.DomainName}}{{.Port}}{{.PathName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    67  		"HostName":   hostName,
    68  		"DomainName": cmd.RequiredArgs.Domain,
    69  		"PathName":   cmd.Path.Path,
    70  		"Port":       displayPort,
    71  		"OrgName":    cmd.Config.TargetedOrganization().Name,
    72  		"SpaceName":  cmd.Config.TargetedSpace().Name,
    73  		"Username":   user.Name,
    74  	})
    75  	cmd.UI.DisplayNewline()
    76  
    77  	route, warnings, err := cmd.Actor.GetRouteByAttributes(domain, cmd.Hostname, cmd.Path.Path, cmd.Port)
    78  	cmd.UI.DisplayWarnings(warnings)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	port := ""
    84  	if route.Port != 0 {
    85  		port = strconv.Itoa(route.Port)
    86  	}
    87  
    88  	appMap, warnings, err := cmd.Actor.GetApplicationMapForRoute(route)
    89  	cmd.UI.DisplayWarnings(warnings)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	table := [][]string{
    95  		{cmd.UI.TranslateText("domain:"), domain.Name},
    96  		{cmd.UI.TranslateText("host:"), route.Host},
    97  		{cmd.UI.TranslateText("port:"), port},
    98  		{cmd.UI.TranslateText("path:"), route.Path},
    99  		{cmd.UI.TranslateText("protocol:"), route.Protocol},
   100  	}
   101  
   102  	cmd.UI.DisplayKeyValueTable("", table, 3)
   103  	cmd.UI.DisplayNewline()
   104  
   105  	cmd.UI.DisplayText("Destinations:")
   106  	cmd.displayDestinations(route, appMap)
   107  
   108  	return nil
   109  }
   110  
   111  func (cmd RouteCommand) displayDestinations(route resources.Route, appMap map[string]resources.Application) {
   112  	destinations := route.Destinations
   113  	if len(destinations) > 0 {
   114  		var keyValueTable = [][]string{
   115  			{
   116  				cmd.UI.TranslateText("app"),
   117  				cmd.UI.TranslateText("process"),
   118  				cmd.UI.TranslateText("port"),
   119  				cmd.UI.TranslateText("app-protocol"),
   120  			},
   121  		}
   122  
   123  		for _, destination := range destinations {
   124  			port := ""
   125  			if destination.Port != 0 {
   126  				port = strconv.Itoa(destination.Port)
   127  			}
   128  			keyValueTable = append(keyValueTable, []string{
   129  				appMap[destination.App.GUID].Name,
   130  				destination.App.Process.Type,
   131  				port,
   132  				destination.Protocol,
   133  			})
   134  		}
   135  
   136  		cmd.UI.DisplayKeyValueTable("\t", keyValueTable, 3)
   137  	}
   138  }