code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/routes_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/resources"
     9  	"code.cloudfoundry.org/cli/util/ui"
    10  )
    11  
    12  type RoutesCommand struct {
    13  	BaseCommand
    14  
    15  	usage           interface{} `usage:"CF_NAME routes [--org-level]"`
    16  	relatedCommands interface{} `related_commands:"check-route, create-route, domains, map-route, unmap-route"`
    17  	Orglevel        bool        `long:"org-level" description:"List all the routes for all spaces of current organization"`
    18  	Labels          string      `long:"labels" description:"Selector to filter routes by labels"`
    19  }
    20  
    21  func (cmd RoutesCommand) Execute(args []string) error {
    22  	var (
    23  		routes   []resources.Route
    24  		warnings v7action.Warnings
    25  		err      error
    26  	)
    27  
    28  	err = cmd.SharedActor.CheckTarget(true, true)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	currentUser, err := cmd.Config.CurrentUser()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	targetedOrg := cmd.Config.TargetedOrganization()
    39  	targetedSpace := cmd.Config.TargetedSpace()
    40  
    41  	if cmd.Orglevel {
    42  		cmd.UI.DisplayTextWithFlavor("Getting routes for org {{.CurrentOrg}} as {{.CurrentUser}}...\n", map[string]interface{}{
    43  			"CurrentOrg":  targetedOrg.Name,
    44  			"CurrentUser": currentUser.Name,
    45  		})
    46  		routes, warnings, err = cmd.Actor.GetRoutesByOrg(targetedOrg.GUID, cmd.Labels)
    47  	} else {
    48  		cmd.UI.DisplayTextWithFlavor("Getting routes for org {{.CurrentOrg}} / space {{.CurrentSpace}} as {{.CurrentUser}}...\n", map[string]interface{}{
    49  			"CurrentOrg":   targetedOrg.Name,
    50  			"CurrentSpace": targetedSpace.Name,
    51  			"CurrentUser":  currentUser.Name,
    52  		})
    53  		routes, warnings, err = cmd.Actor.GetRoutesBySpace(targetedSpace.GUID, cmd.Labels)
    54  	}
    55  
    56  	cmd.UI.DisplayWarnings(warnings)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	routeSummaries, warnings, err := cmd.Actor.GetRouteSummaries(routes)
    62  	cmd.UI.DisplayWarnings(warnings)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if len(routes) > 0 {
    68  		cmd.displayRoutesTable(routeSummaries)
    69  	} else {
    70  		cmd.UI.DisplayText("No routes found.")
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func (cmd RoutesCommand) displayRoutesTable(routeSummaries []v7action.RouteSummary) {
    77  	var routesTable = [][]string{
    78  		{
    79  			cmd.UI.TranslateText("space"),
    80  			cmd.UI.TranslateText("host"),
    81  			cmd.UI.TranslateText("domain"),
    82  			cmd.UI.TranslateText("port"),
    83  			cmd.UI.TranslateText("path"),
    84  			cmd.UI.TranslateText("protocol"),
    85  			cmd.UI.TranslateText("apps"),
    86  		},
    87  	}
    88  
    89  	for _, routeSummary := range routeSummaries {
    90  		port := ""
    91  		if routeSummary.Port != 0 {
    92  			port = strconv.Itoa(routeSummary.Port)
    93  		}
    94  		routesTable = append(routesTable, []string{
    95  			routeSummary.SpaceName,
    96  			routeSummary.Host,
    97  			routeSummary.DomainName,
    98  			port,
    99  			routeSummary.Path,
   100  			routeSummary.Protocol,
   101  			strings.Join(routeSummary.AppNames, ", "),
   102  		})
   103  	}
   104  
   105  	cmd.UI.DisplayTableWithHeader("", routesTable, ui.DefaultTableSpacePadding)
   106  }