Skip to content

Commit

Permalink
Merge 20fceed into f1cde32
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdarshankumar authored Mar 7, 2023
2 parents f1cde32 + 20fceed commit 45eb473
Show file tree
Hide file tree
Showing 10 changed files with 587 additions and 28 deletions.
19 changes: 17 additions & 2 deletions buildpackage/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaultOS = "linux"
// Config encapsulates the possible configuration options for buildpackage creation.
type Config struct {
Buildpack dist.BuildpackURI `toml:"buildpack"`
Extension dist.BuildpackURI `toml:"extension"`
Dependencies []dist.ImageOrURI `toml:"dependencies"`
Platform dist.Platform `toml:"platform"`
}
Expand All @@ -32,6 +33,17 @@ func DefaultConfig() Config {
}
}

func DefaultExtensionConfig() Config {
return Config{
Extension: dist.BuildpackURI{
URI: ".",
},
Platform: dist.Platform{
OS: defaultOS,
},
}
}

// NewConfigReader returns an instance of ConfigReader. It does not take any parameters.
func NewConfigReader() *ConfigReader {
return &ConfigReader{}
Expand Down Expand Up @@ -61,8 +73,11 @@ func (r *ConfigReader) Read(path string) (Config, error) {
)
}

if packageConfig.Buildpack.URI == "" {
return packageConfig, errors.Errorf("missing %s configuration", style.Symbol("buildpack.uri"))
if packageConfig.Buildpack.URI == "" && packageConfig.Extension.URI == "" {
if packageConfig.Buildpack.URI == "" {
return packageConfig, errors.Errorf("missing %s configuration", style.Symbol("buildpack.uri"))
}
return packageConfig, errors.Errorf("missing %s configuration", style.Symbol("extension.uri"))
}

if packageConfig.Platform.OS == "" {
Expand Down
1 change: 1 addition & 0 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type PackClient interface {
CreateBuilder(context.Context, client.CreateBuilderOptions) error
NewBuildpack(context.Context, client.NewBuildpackOptions) error
PackageBuildpack(ctx context.Context, opts client.PackageBuildpackOptions) error
PackageExtension(ctx context.Context, opts client.PackageBuildpackOptions) error
Build(context.Context, client.BuildOptions) error
RegisterBuildpack(context.Context, client.RegisterBuildpackOptions) error
YankBuildpack(client.YankBuildpackOptions) error
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewExtensionCommand(logger logging.Logger, cfg config.Config, client PackCl

cmd.AddCommand(ExtensionInspect(logger, cfg, client))
// client and packageConfigReader to be passed later on
cmd.AddCommand(ExtensionPackage(logger, cfg))
cmd.AddCommand(ExtensionPackage(logger, cfg, client, packageConfigReader))
// client to be passed later on
cmd.AddCommand(ExtensionNew(logger))
cmd.AddCommand(ExtensionPull(logger, cfg, client))
Expand Down
93 changes: 83 additions & 10 deletions internal/commands/extension_package.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,111 @@
package commands

import (
"context"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"

pubbldpkg "github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
)

// ExtensionPackageFlags define flags provided to the ExtensionPackage command
type ExtensionPackageFlags struct {
PackageTomlPath string
Format string
Publish bool
Policy string
ExtensionRegistry string
Path string
PackageTomlPath string
Format string
Publish bool
Policy string
}

// Packager and PackageConfigReader to be added here and argument also to be added in the function
// ExtensionPackager packages extensions
type ExtensionPackager interface {
PackageExtension(ctx context.Context, options client.PackageBuildpackOptions) error
}

// ExtensionPackage packages (a) extension(s) into OCI format, based on a package config
func ExtensionPackage(logger logging.Logger, cfg config.Config) *cobra.Command {
func ExtensionPackage(logger logging.Logger, cfg config.Config, packager ExtensionPackager, packageConfigReader PackageConfigReader) *cobra.Command {
var flags ExtensionPackageFlags
cmd := &cobra.Command{
Use: "package <name> --config <config-path>",
Short: "Package an extension in OCI format",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
// logic will be added here
if err := validateExtensionPackageFlags(&flags); err != nil {
return err
}

stringPolicy := flags.Policy
if stringPolicy == "" {
stringPolicy = cfg.PullPolicy
}

pullPolicy, err := image.ParsePullPolicy(stringPolicy)
if err != nil {
return errors.Wrap(err, "parsing pull policy")
}

exPackageCfg := pubbldpkg.DefaultExtensionConfig()
relativeBaseDir := ""
if flags.PackageTomlPath != "" {
exPackageCfg, err = packageConfigReader.Read(flags.PackageTomlPath)
if err != nil {
return errors.Wrap(err, "reading config")
}

relativeBaseDir, err = filepath.Abs(filepath.Dir(flags.PackageTomlPath))
if err != nil {
return errors.Wrap(err, "getting absolute path for config")
}
}
name := args[0]
if flags.Format == client.FormatFile {
switch ext := filepath.Ext(name); ext {
case client.CNBExtension:
case "":
name += client.CNBExtension
default:
logger.Warnf("%s is not a valid extension for a packaged extension. Packaged extensions must have a %s extension", style.Symbol(ext), style.Symbol(client.CNBExtension))
}
}

if err := packager.PackageExtension(cmd.Context(), client.PackageBuildpackOptions{
RelativeBaseDir: relativeBaseDir,
Name: name,
Format: flags.Format,
Config: exPackageCfg,
Publish: flags.Publish,
PullPolicy: pullPolicy,
}); err != nil {
return err
}
action := "created"
if flags.Publish {
action = "published"
}

logger.Infof("Successfully %s package %s", action, style.Symbol(name))
return nil
}),
}

// flags will be added here

cmd.Flags().StringVarP(&flags.PackageTomlPath, "config", "c", "", "Path to package TOML config")
cmd.Flags().StringVarP(&flags.Format, "format", "f", "", `Format to save package as ("image" or "file")`)
cmd.Flags().BoolVar(&flags.Publish, "publish", false, `Publish to registry (applies to "--format=image" only)`)
cmd.Flags().StringVar(&flags.Policy, "pull-policy", "", "Pull policy to use. Accepted values are always, never, and if-not-present. The default is always")
AddHelpFlag(cmd, "package")
return cmd
}

func validateExtensionPackageFlags(p *ExtensionPackageFlags) error {
if p.Publish && p.Policy == image.PullNever.String() {
return errors.Errorf("--publish and --pull-policy never cannot be used together. The --publish flag requires the use of remote images.")
}
return nil
}
Loading

0 comments on commit 45eb473

Please sign in to comment.