code.cloudfoundry.org/cli@v7.1.0+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"), T("stack")})
    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  			buildpack.Stack,
    84  		)
    85  		noBuildpacks = false
    86  		return true
    87  	})
    88  	err := table.Print()
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	if apiErr != nil {
    94  		return errors.New(T("Failed fetching buildpacks.\n{{.Error}}", map[string]interface{}{"Error": apiErr.Error()}))
    95  	}
    96  
    97  	if noBuildpacks {
    98  		cmd.ui.Say(T("No buildpacks found"))
    99  	}
   100  	return nil
   101  }