github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/routergroups/router_groups.go (about)

     1  package routergroups
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type RouterGroups struct {
    17  	ui             terminal.UI
    18  	routingAPIRepo api.RoutingAPIRepository
    19  	config         coreconfig.Reader
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&RouterGroups{})
    24  }
    25  
    26  func (cmd *RouterGroups) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "router-groups",
    29  		Description: T("List router groups"),
    30  		Usage: []string{
    31  			"CF_NAME router-groups",
    32  		},
    33  	}
    34  }
    35  
    36  func (cmd *RouterGroups) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    37  	return []requirements.Requirement{
    38  		requirementsFactory.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    39  			T("No argument required"),
    40  			func() bool {
    41  				return len(fc.Args()) != 0
    42  			},
    43  		),
    44  		requirementsFactory.NewLoginRequirement(),
    45  		requirementsFactory.NewRoutingAPIRequirement(),
    46  	}, nil
    47  }
    48  
    49  func (cmd *RouterGroups) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    50  	cmd.ui = deps.UI
    51  	cmd.config = deps.Config
    52  	cmd.routingAPIRepo = deps.RepoLocator.GetRoutingAPIRepository()
    53  	return cmd
    54  }
    55  
    56  func (cmd *RouterGroups) Execute(c flags.FlagContext) error {
    57  	cmd.ui.Say(T("Getting router groups as {{.Username}} ...\n",
    58  		map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())}))
    59  
    60  	table := cmd.ui.Table([]string{T("name"), T("type")})
    61  
    62  	noRouterGroups := true
    63  	cb := func(group models.RouterGroup) bool {
    64  		noRouterGroups = false
    65  		table.Add(group.Name, group.Type)
    66  		return true
    67  	}
    68  
    69  	apiErr := cmd.routingAPIRepo.ListRouterGroups(cb)
    70  	if apiErr != nil {
    71  		return errors.New(T("Failed fetching router groups.\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()}))
    72  	}
    73  
    74  	if noRouterGroups {
    75  		cmd.ui.Say(T("No router groups found"))
    76  	}
    77  
    78  	err := table.Print()
    79  	if err != nil {
    80  		return err
    81  	}
    82  	return nil
    83  }