github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/route/routes.go (about)

     1  package route
     2  
     3  import (
     4  	"strings"
     5  
     6  	. "github.com/cloudfoundry/cli/cf/i18n"
     7  	"github.com/cloudfoundry/cli/flags"
     8  	"github.com/cloudfoundry/cli/flags/flag"
     9  
    10  	"github.com/cloudfoundry/cli/cf/api"
    11  	"github.com/cloudfoundry/cli/cf/command_registry"
    12  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    13  	"github.com/cloudfoundry/cli/cf/models"
    14  	"github.com/cloudfoundry/cli/cf/requirements"
    15  	"github.com/cloudfoundry/cli/cf/terminal"
    16  )
    17  
    18  type ListRoutes struct {
    19  	ui        terminal.UI
    20  	routeRepo api.RouteRepository
    21  	config    core_config.Reader
    22  }
    23  
    24  func init() {
    25  	command_registry.Register(&ListRoutes{})
    26  }
    27  
    28  func (cmd *ListRoutes) MetaData() command_registry.CommandMetadata {
    29  	fs := make(map[string]flags.FlagSet)
    30  	fs["orglevel"] = &cliFlags.BoolFlag{Name: "orglevel", Usage: T("List all the routes for all spaces of current organization")}
    31  
    32  	return command_registry.CommandMetadata{
    33  		Name:        "routes",
    34  		ShortName:   "r",
    35  		Description: T("List all routes in the current space or the current organization"),
    36  		Usage:       "CF_NAME routes",
    37  		Flags:       fs,
    38  	}
    39  }
    40  
    41  func (cmd *ListRoutes) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 0 {
    43  		cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("routes"))
    44  	}
    45  
    46  	return []requirements.Requirement{
    47  		requirementsFactory.NewLoginRequirement(),
    48  		requirementsFactory.NewTargetedSpaceRequirement(),
    49  	}, nil
    50  }
    51  
    52  func (cmd *ListRoutes) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    53  	cmd.ui = deps.Ui
    54  	cmd.config = deps.Config
    55  	cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
    56  	return cmd
    57  }
    58  
    59  func (cmd *ListRoutes) Execute(c flags.FlagContext) {
    60  	cmd.ui.Say(T("Getting routes as {{.Username}} ...\n",
    61  		map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())}))
    62  
    63  	table := cmd.ui.Table([]string{T("space"), T("host"), T("domain"), T("apps")})
    64  
    65  	noRoutes := true
    66  	var apiErr error
    67  	flag := c.Bool("orglevel")
    68  
    69  	if flag {
    70  		apiErr = cmd.routeRepo.ListAllRoutes(func(route models.Route) bool {
    71  			noRoutes = false
    72  			appNames := []string{}
    73  			for _, app := range route.Apps {
    74  				appNames = append(appNames, app.Name)
    75  			}
    76  
    77  			table.Add(route.Space.Name, route.Host, route.Domain.Name, strings.Join(appNames, ","))
    78  			return true
    79  		})
    80  
    81  	} else {
    82  
    83  		apiErr = cmd.routeRepo.ListRoutes(func(route models.Route) bool {
    84  			noRoutes = false
    85  			appNames := []string{}
    86  			for _, app := range route.Apps {
    87  				appNames = append(appNames, app.Name)
    88  			}
    89  
    90  			table.Add(route.Space.Name, route.Host, route.Domain.Name, strings.Join(appNames, ","))
    91  			return true
    92  		})
    93  	}
    94  
    95  	table.Print()
    96  
    97  	if apiErr != nil {
    98  		cmd.ui.Failed(T("Failed fetching routes.\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()}))
    99  		return
   100  	}
   101  
   102  	if noRoutes {
   103  		cmd.ui.Say(T("No routes found"))
   104  	}
   105  }