github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/package_buildpack.go (about)

     1  package commands
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/spf13/cobra"
     8  
     9  	pubbldpkg "github.com/buildpacks/pack/buildpackage"
    10  	"github.com/buildpacks/pack/internal/config"
    11  	"github.com/buildpacks/pack/internal/style"
    12  	"github.com/buildpacks/pack/pkg/client"
    13  	"github.com/buildpacks/pack/pkg/image"
    14  	"github.com/buildpacks/pack/pkg/logging"
    15  )
    16  
    17  // Deprecated: use BuildpackPackage instead
    18  // PackageBuildpack packages (a) buildpack(s) into OCI format, based on a package config
    19  func PackageBuildpack(logger logging.Logger, cfg config.Config, packager BuildpackPackager, packageConfigReader PackageConfigReader) *cobra.Command {
    20  	var flags BuildpackPackageFlags
    21  
    22  	cmd := &cobra.Command{
    23  		Use:     `package-buildpack <name> --config <package-config-path>`,
    24  		Hidden:  true,
    25  		Args:    cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
    26  		Short:   "Package buildpack in OCI format.",
    27  		Example: "pack package-buildpack my-buildpack --config ./package.toml",
    28  		Long: "package-buildpack allows users to package (a) buildpack(s) into OCI format, which can then to be hosted in " +
    29  			"image repositories. You can also package a number of buildpacks together, to enable easier distribution of " +
    30  			"a set of buildpacks. Packaged buildpacks can be used as inputs to `pack build` (using the `--buildpack` flag), " +
    31  			"and they can be included in the configs used in `pack builder create` and `pack buildpack package`. For more " +
    32  			"on how to package a buildpack, see: https://buildpacks.io/docs/buildpack-author-guide/package-a-buildpack/.",
    33  		RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
    34  			deprecationWarning(logger, "package-buildpack", "buildpack package")
    35  
    36  			if err := validateBuildpackPackageFlags(cfg, &flags); err != nil {
    37  				return err
    38  			}
    39  
    40  			stringPolicy := flags.Policy
    41  			if stringPolicy == "" {
    42  				stringPolicy = cfg.PullPolicy
    43  			}
    44  			pullPolicy, err := image.ParsePullPolicy(stringPolicy)
    45  			if err != nil {
    46  				return errors.Wrap(err, "parsing pull policy")
    47  			}
    48  
    49  			cfg := pubbldpkg.DefaultConfig()
    50  			relativeBaseDir := ""
    51  			if flags.PackageTomlPath != "" {
    52  				cfg, err = packageConfigReader.Read(flags.PackageTomlPath)
    53  				if err != nil {
    54  					return errors.Wrap(err, "reading config")
    55  				}
    56  
    57  				relativeBaseDir, err = filepath.Abs(filepath.Dir(flags.PackageTomlPath))
    58  				if err != nil {
    59  					return errors.Wrap(err, "getting absolute path for config")
    60  				}
    61  			}
    62  
    63  			name := args[0]
    64  			if err := packager.PackageBuildpack(cmd.Context(), client.PackageBuildpackOptions{
    65  				RelativeBaseDir: relativeBaseDir,
    66  				Name:            name,
    67  				Format:          flags.Format,
    68  				Config:          cfg,
    69  				Publish:         flags.Publish,
    70  				PullPolicy:      pullPolicy,
    71  				Registry:        flags.BuildpackRegistry,
    72  			}); err != nil {
    73  				return err
    74  			}
    75  
    76  			action := "created"
    77  			if flags.Publish {
    78  				action = "published"
    79  			}
    80  
    81  			logger.Infof("Successfully %s package %s", action, style.Symbol(name))
    82  			return nil
    83  		}),
    84  	}
    85  	cmd.Flags().StringVarP(&flags.PackageTomlPath, "config", "c", "", "Path to package TOML config (required)")
    86  
    87  	cmd.Flags().StringVarP(&flags.Format, "format", "f", "", `Format to save package as ("image" or "file")`)
    88  	cmd.Flags().BoolVar(&flags.Publish, "publish", false, `Publish the buildpack directly to the container registry specified in <name>, instead of the daemon (applies to "--format=image" only).`)
    89  	cmd.Flags().StringVar(&flags.Policy, "pull-policy", "", "Pull policy to use. Accepted values are always, never, and if-not-present. The default is always")
    90  	cmd.Flags().StringVarP(&flags.BuildpackRegistry, "buildpack-registry", "r", "", "Buildpack Registry name")
    91  
    92  	AddHelpFlag(cmd, "package-buildpack")
    93  	return cmd
    94  }