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

     1  package buildpack
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  
    10  	"code.cloudfoundry.org/cli/cf/api"
    11  	"code.cloudfoundry.org/cli/cf/commandregistry"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type ListBuildpacks struct {
    18  	ui            terminal.UI
    19  	buildpackRepo api.BuildpackRepository
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&ListBuildpacks{})
    24  }
    25  
    26  func (cmd *ListBuildpacks) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "buildpacks",
    29  		Description: T("List all buildpacks"),
    30  		Usage: []string{
    31  			T("CF_NAME buildpacks"),
    32  		},
    33  	}
    34  }
    35  
    36  func (cmd *ListBuildpacks) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    37  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    38  		T("No argument required"),
    39  		func() bool {
    40  			return len(fc.Args()) != 0
    41  		},
    42  	)
    43  
    44  	reqs := []requirements.Requirement{
    45  		usageReq,
    46  		requirementsFactory.NewLoginRequirement(),
    47  	}
    48  
    49  	return reqs, nil
    50  }
    51  
    52  func (cmd *ListBuildpacks) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    53  	cmd.ui = deps.UI
    54  	cmd.buildpackRepo = deps.RepoLocator.GetBuildpackRepository()
    55  	return cmd
    56  }
    57  
    58  func (cmd *ListBuildpacks) Execute(c flags.FlagContext) error {
    59  	cmd.ui.Say(T("Getting buildpacks...\n"))
    60  
    61  	table := cmd.ui.Table([]string{"buildpack", T("position"), T("enabled"), T("locked"), T("filename")})
    62  	noBuildpacks := true
    63  
    64  	apiErr := cmd.buildpackRepo.ListBuildpacks(func(buildpack models.Buildpack) bool {
    65  		position := ""
    66  		if buildpack.Position != nil {
    67  			position = strconv.Itoa(*buildpack.Position)
    68  		}
    69  		enabled := ""
    70  		if buildpack.Enabled != nil {
    71  			enabled = strconv.FormatBool(*buildpack.Enabled)
    72  		}
    73  		locked := ""
    74  		if buildpack.Locked != nil {
    75  			locked = strconv.FormatBool(*buildpack.Locked)
    76  		}
    77  		table.Add(
    78  			buildpack.Name,
    79  			position,
    80  			enabled,
    81  			locked,
    82  			buildpack.Filename,
    83  		)
    84  		noBuildpacks = false
    85  		return true
    86  	})
    87  	err := table.Print()
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	if apiErr != nil {
    93  		return errors.New(T("Failed fetching buildpacks.\n{{.Error}}", map[string]interface{}{"Error": apiErr.Error()}))
    94  	}
    95  
    96  	if noBuildpacks {
    97  		cmd.ui.Say(T("No buildpacks found"))
    98  	}
    99  	return nil
   100  }