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

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