github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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  	}, nil
    46  }
    47  
    48  func (cmd *RouterGroups) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    49  	cmd.ui = deps.UI
    50  	cmd.config = deps.Config
    51  	cmd.routingAPIRepo = deps.RepoLocator.GetRoutingAPIRepository()
    52  	return cmd
    53  }
    54  
    55  func (cmd *RouterGroups) Execute(c flags.FlagContext) error {
    56  	cmd.ui.Say(T("Getting router groups as {{.Username}} ...\n",
    57  		map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())}))
    58  
    59  	table := cmd.ui.Table([]string{T("name"), T("type")})
    60  
    61  	noRouterGroups := true
    62  	cb := func(group models.RouterGroup) bool {
    63  		noRouterGroups = false
    64  		table.Add(group.Name, group.Type)
    65  		return true
    66  	}
    67  
    68  	apiErr := cmd.routingAPIRepo.ListRouterGroups(cb)
    69  	if apiErr != nil {
    70  		return errors.New(T("Failed fetching router groups.\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()}))
    71  	}
    72  
    73  	if noRouterGroups {
    74  		cmd.ui.Say(T("No router groups found"))
    75  	}
    76  
    77  	err := table.Print()
    78  	if err != nil {
    79  		return err
    80  	}
    81  	return nil
    82  }