code.cloudfoundry.org/cli@v7.1.0+incompatible/cf/commands/stacks.go (about)

     1  package commands
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/stacks"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/terminal"
    11  )
    12  
    13  type ListStacks struct {
    14  	ui         terminal.UI
    15  	config     coreconfig.Reader
    16  	stacksRepo stacks.StackRepository
    17  }
    18  
    19  func init() {
    20  	commandregistry.Register(&ListStacks{})
    21  }
    22  
    23  func (cmd *ListStacks) MetaData() commandregistry.CommandMetadata {
    24  	return commandregistry.CommandMetadata{
    25  		Name:        "stacks",
    26  		Description: T("List all stacks (a stack is a pre-built file system, including an operating system, that can run apps)"),
    27  		Usage: []string{
    28  			T("CF_NAME stacks"),
    29  		},
    30  	}
    31  }
    32  
    33  func (cmd *ListStacks) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    34  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    35  		T("No argument required"),
    36  		func() bool {
    37  			return len(fc.Args()) != 0
    38  		},
    39  	)
    40  
    41  	reqs := []requirements.Requirement{
    42  		usageReq,
    43  		requirementsFactory.NewLoginRequirement(),
    44  	}
    45  
    46  	return reqs, nil
    47  }
    48  
    49  func (cmd *ListStacks) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    50  	cmd.ui = deps.UI
    51  	cmd.config = deps.Config
    52  	cmd.stacksRepo = deps.RepoLocator.GetStackRepository()
    53  	return cmd
    54  }
    55  
    56  func (cmd *ListStacks) Execute(c flags.FlagContext) error {
    57  	cmd.ui.Say(T("Getting stacks in org {{.OrganizationName}} / space {{.SpaceName}} as {{.Username}}...",
    58  		map[string]interface{}{"OrganizationName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    59  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    60  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    61  
    62  	stacks, err := cmd.stacksRepo.FindAll()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	cmd.ui.Ok()
    68  	cmd.ui.Say("")
    69  
    70  	table := cmd.ui.Table([]string{T("name"), T("description")})
    71  
    72  	for _, stack := range stacks {
    73  		table.Add(stack.Name, stack.Description)
    74  	}
    75  
    76  	err = table.Print()
    77  	if err != nil {
    78  		return err
    79  	}
    80  	return nil
    81  }