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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/stacks"
     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/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  )
    14  
    15  type ListStack struct {
    16  	ui         terminal.UI
    17  	config     coreconfig.Reader
    18  	stacksRepo stacks.StackRepository
    19  }
    20  
    21  func init() {
    22  	commandregistry.Register(&ListStack{})
    23  }
    24  
    25  func (cmd *ListStack) MetaData() commandregistry.CommandMetadata {
    26  	fs := make(map[string]flags.FlagSet)
    27  	fs["guid"] = &flags.BoolFlag{Name: "guid", Usage: T("Retrieve and display the given stack's guid. All other output for the stack is suppressed.")}
    28  
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "stack",
    31  		Description: T("Show information for a stack (a stack is a pre-built file system, including an operating system, that can run apps)"),
    32  		Usage: []string{
    33  			T("CF_NAME stack STACK_NAME"),
    34  		},
    35  		Flags:     fs,
    36  		TotalArgs: 1,
    37  	}
    38  }
    39  
    40  func (cmd *ListStack) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    41  	if len(fc.Args()) != 1 {
    42  		cmd.ui.Failed(T("Incorrect Usage. Requires stack name as argument\n\n") + commandregistry.Commands.CommandUsage("stack"))
    43  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    44  	}
    45  
    46  	reqs := []requirements.Requirement{
    47  		requirementsFactory.NewLoginRequirement(),
    48  	}
    49  
    50  	return reqs, nil
    51  }
    52  
    53  func (cmd *ListStack) SetDependency(deps commandregistry.Dependency, _ bool) commandregistry.Command {
    54  	cmd.ui = deps.UI
    55  	cmd.config = deps.Config
    56  	cmd.stacksRepo = deps.RepoLocator.GetStackRepository()
    57  	return cmd
    58  }
    59  
    60  func (cmd *ListStack) Execute(c flags.FlagContext) error {
    61  	stackName := c.Args()[0]
    62  
    63  	stack, err := cmd.stacksRepo.FindByName(stackName)
    64  
    65  	if c.Bool("guid") {
    66  		cmd.ui.Say(stack.GUID)
    67  	} else {
    68  		if err != nil {
    69  			return err
    70  		}
    71  
    72  		cmd.ui.Say(T("Getting stack '{{.Stack}}' in org {{.OrganizationName}} / space {{.SpaceName}} as {{.Username}}...",
    73  			map[string]interface{}{"Stack": stackName,
    74  				"OrganizationName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    75  				"SpaceName":        terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    76  				"Username":         terminal.EntityNameColor(cmd.config.Username())}))
    77  
    78  		cmd.ui.Ok()
    79  		cmd.ui.Say("")
    80  		table := cmd.ui.Table([]string{T("name"), T("description")})
    81  		table.Add(stack.Name, stack.Description)
    82  		err = table.Print()
    83  		if err != nil {
    84  			return err
    85  		}
    86  	}
    87  	return nil
    88  }