Skip to content

Commit

Permalink
Merge pull request #1103 from samj1912/package
Browse files Browse the repository at this point in the history
Allow pack buildpack package to create buildpackages without package.…
  • Loading branch information
jromero authored Mar 11, 2021
2 parents 6198c8b + 6213950 commit 8759cb6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/buildpacks/imgutil v0.0.0-20201211223552-8581300fe2b2 h1:tQWsNfyiBTEB6BaEs+sRxsbbo/XkNxCEqSWVwTq7I00=
github.com/buildpacks/imgutil v0.0.0-20201211223552-8581300fe2b2/go.mod h1:NC93OGDehA2ksqgTzugeQcPqmTpilMPYRO+XaFsDyts=
github.com/buildpacks/imgutil v0.0.0-20210209163614-30601e371ce3 h1:NuBOSLnHIE+gTjeRC/PAopad83eNT1YLRclBSIRYhKU=
github.com/buildpacks/imgutil v0.0.0-20210209163614-30601e371ce3/go.mod h1:JiWXkmkaiXrr5nq77JUtu7ys/5OAznS4VI7TwKthP7s=
Expand Down Expand Up @@ -232,7 +231,6 @@ github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
Expand All @@ -259,7 +257,6 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
16 changes: 13 additions & 3 deletions internal/commands/buildpack_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type BuildpackPackageFlags struct {
Publish bool
Policy string
BuildpackRegistry string
Path string
}

// BuildpackPackager packages buildpacks
Expand Down Expand Up @@ -60,8 +61,14 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, client Buildpack
if err != nil {
return errors.Wrap(err, "parsing pull policy")
}

bpPackageCfg := pubbldpkg.DefaultConfig()
var bpPath string
if flags.Path != "" {
if bpPath, err = filepath.Abs(flags.Path); err != nil {
return errors.Wrap(err, "resolving buildpack path")
}
bpPackageCfg.Buildpack.URI = bpPath
}
relativeBaseDir := ""
if flags.PackageTomlPath != "" {
bpPackageCfg, err = packageConfigReader.Read(flags.PackageTomlPath)
Expand All @@ -74,7 +81,6 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, client Buildpack
return errors.Wrap(err, "getting absolute path for config")
}
}

name := args[0]
if err := client.PackageBuildpack(cmd.Context(), pack.PackageBuildpackOptions{
RelativeBaseDir: relativeBaseDir,
Expand All @@ -98,10 +104,11 @@ func BuildpackPackage(logger logging.Logger, cfg config.Config, client Buildpack
}),
}

cmd.Flags().StringVarP(&flags.PackageTomlPath, "config", "c", "", "Path to package TOML config (required)")
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")
cmd.Flags().StringVarP(&flags.Path, "path", "p", "", "Path to the Buildpack that needs to be packaged")
cmd.Flags().StringVarP(&flags.BuildpackRegistry, "buildpack-registry", "r", "", "Buildpack Registry name")

AddHelpFlag(cmd, "package")
Expand All @@ -112,6 +119,9 @@ func validateBuildpackPackageFlags(p *BuildpackPackageFlags) error {
if p.Publish && p.Policy == pubcfg.PullNever.String() {
return errors.Errorf("--publish and --pull-policy never cannot be used together. The --publish flag requires the use of remote images.")
}
if p.PackageTomlPath != "" && p.Path != "" {
return errors.Errorf("--config and --path cannot be used together. Please specify the relative path to the Buildpack directory in the package config file.")
}

return nil
}
50 changes: 43 additions & 7 deletions internal/commands/buildpack_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands_test
import (
"bytes"
"fmt"
"path/filepath"
"testing"

"github.com/heroku/color"
Expand Down Expand Up @@ -87,6 +88,22 @@ func testPackageCommand(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, receivedOptions.Config, myConfig)
})

when("there is a path flag", func() {
it("returns an error saying that it cannot be used with the config flag", func() {
myConfig := pubbldpkg.Config{
Buildpack: dist.BuildpackURI{URI: "test"},
}

cmd := packageCommand(
withBuildpackPackager(fakeBuildpackPackager),
withPackageConfigReader(fakes.NewFakePackageConfigReader(whereReadReturns(myConfig, nil))),
withPath(".."),
)
err := cmd.Execute()
h.AssertError(t, err, "--config and --path cannot be used together")
})
})

when("pull-policy", func() {
var pullPolicyArgs = []string{
"some-image-name",
Expand Down Expand Up @@ -147,13 +164,25 @@ func testPackageCommand(t *testing.T, when spec.G, it spec.S) {
})

when("no config path is specified", func() {
it("creates a default config", func() {
cmd := packageCommand(withBuildpackPackager(fakeBuildpackPackager))
cmd.SetArgs([]string{"some-name"})
h.AssertNil(t, cmd.Execute())
when("no path is specified", func() {
it("creates a default config with the uri set to the current working directory", func() {
cmd := packageCommand(withBuildpackPackager(fakeBuildpackPackager))
cmd.SetArgs([]string{"some-name"})
h.AssertNil(t, cmd.Execute())

receivedOptions := fakeBuildpackPackager.CreateCalledWithOptions
h.AssertEq(t, receivedOptions.Config.Buildpack.URI, ".")
receivedOptions := fakeBuildpackPackager.CreateCalledWithOptions
h.AssertEq(t, receivedOptions.Config.Buildpack.URI, ".")
})
})
when("a path is specified", func() {
it("creates a default config with the appropriate path", func() {
cmd := packageCommand(withBuildpackPackager(fakeBuildpackPackager))
cmd.SetArgs([]string{"some-name", "-p", ".."})
h.AssertNil(t, cmd.Execute())
bpPath, _ := filepath.Abs("..")
receivedOptions := fakeBuildpackPackager.CreateCalledWithOptions
h.AssertEq(t, receivedOptions.Config.Buildpack.URI, bpPath)
})
})
})
})
Expand Down Expand Up @@ -223,6 +252,7 @@ type packageCommandConfig struct {
clientConfig config.Config
imageName string
configPath string
path string
}

type packageCommandOption func(config *packageCommandConfig)
Expand All @@ -242,7 +272,7 @@ func packageCommand(ops ...packageCommandOption) *cobra.Command {
}

cmd := commands.BuildpackPackage(config.logger, config.clientConfig, config.buildpackPackager, config.packageConfigReader)
cmd.SetArgs([]string{config.imageName, "--config", config.configPath})
cmd.SetArgs([]string{config.imageName, "--config", config.configPath, "-p", config.path})

return cmd
}
Expand Down Expand Up @@ -271,6 +301,12 @@ func withImageName(name string) packageCommandOption {
}
}

func withPath(name string) packageCommandOption {
return func(config *packageCommandConfig) {
config.path = name
}
}

func withPackageConfigPath(path string) packageCommandOption {
return func(config *packageCommandConfig) {
config.configPath = path
Expand Down

0 comments on commit 8759cb6

Please sign in to comment.