Skip to content

Commit

Permalink
Merge pull request #284 from paketo-buildpacks/contribs
Browse files Browse the repository at this point in the history
libpak API evolution
  • Loading branch information
dmikusa authored Sep 6, 2023
2 parents 9588657 + fe3162f commit 64d6194
Show file tree
Hide file tree
Showing 32 changed files with 387 additions and 315 deletions.
4 changes: 2 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package libpak
import (
"github.com/buildpacks/libcnb/v2"

"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/internal"
"github.com/paketo-buildpacks/libpak/v2/log"
)

// Build is called by the main function of a buildpack, for build.
Expand All @@ -40,7 +40,7 @@ type buildDelegate struct {
func (b buildDelegate) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
result, err := b.delegate(context)
if err != nil {
err = bard.IdentifiableError{
err = log.IdentifiableError{
Name: context.Buildpack.Info.Name,
Description: context.Buildpack.Info.Version,
Err: err,
Expand Down
4 changes: 2 additions & 2 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/stretchr/testify/mock"

"github.com/paketo-buildpacks/libpak/v2"
"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/log"
)

func testBuild(t *testing.T, context spec.G, it spec.S) {
Expand Down Expand Up @@ -132,7 +132,7 @@ version = "test-version"`),
libcnb.WithExitHandler(exitHandler),
)

Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(bard.IdentifiableError{
Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(log.IdentifiableError{
Name: "test-name",
Description: "test-version",
Err: fmt.Errorf("test-error"),
Expand Down
43 changes: 20 additions & 23 deletions buildmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/heroku/color"

"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/log"
"github.com/paketo-buildpacks/libpak/v2/sbom"
)

Expand Down Expand Up @@ -284,7 +284,6 @@ func NewBuildModuleMetadata(metadata map[string]interface{}) (BuildModuleMetadat

// ConfigurationResolver provides functionality for resolving a configuration value.
type ConfigurationResolver struct {

// Configurations are the configurations to resolve against
Configurations []BuildModuleConfiguration
}
Expand Down Expand Up @@ -319,16 +318,20 @@ func (c configurationEntry) String(nameLength int, valueLength int) string {
return sb.String()
}

// NewConfigurationResolver creates a new instance from buildmodule metadata. Logs configuration options to the body
// level int the form 'Set $Name to configure $Description[. Default <i>$Default</i>.]'.
func NewConfigurationResolver(md BuildModuleMetadata, logger *bard.Logger) (ConfigurationResolver, error) {

// NewConfigurationResolver creates a new instance from buildmodule metadata.
func NewConfigurationResolver(md BuildModuleMetadata) (ConfigurationResolver, error) {
cr := ConfigurationResolver{Configurations: md.Configurations}

if logger == nil {
return cr, nil
}
sort.Slice(md.Configurations, func(i, j int) bool {
return md.Configurations[i].Name < md.Configurations[j].Name
})

return cr, nil
}

// LogConfiguration will write the configuration options to the body level in
// the form 'Set $Name to configure $Description[. Default <i>$Default</i>.]'.
func (c *ConfigurationResolver) LogConfiguration(logger log.Logger) {
var (
build []configurationEntry
launch []configurationEntry
Expand All @@ -338,16 +341,12 @@ func NewConfigurationResolver(md BuildModuleMetadata, logger *bard.Logger) (Conf
valueLength int
)

sort.Slice(md.Configurations, func(i, j int) bool {
return md.Configurations[i].Name < md.Configurations[j].Name
})

for _, c := range md.Configurations {
s, _ := cr.Resolve(c.Name)
for _, config := range c.Configurations {
s, _ := c.Resolve(config.Name)

e := configurationEntry{
Name: c.Name,
Description: c.Description,
Name: config.Name,
Description: config.Description,
Value: s,
}

Expand All @@ -359,15 +358,15 @@ func NewConfigurationResolver(md BuildModuleMetadata, logger *bard.Logger) (Conf
valueLength = l
}

if c.Build {
if config.Build {
build = append(build, e)
}

if c.Launch {
if config.Launch {
launch = append(launch, e)
}

if !c.Build && !c.Launch {
if !config.Build && !config.Launch {
unknown = append(unknown, e)
}
}
Expand All @@ -394,8 +393,6 @@ func NewConfigurationResolver(md BuildModuleMetadata, logger *bard.Logger) (Conf
logger.Body(e.String(nameLength, valueLength))
}
}

return cr, nil
}

// Resolve resolves the value for a configuration option, returning the default value and false if it was not set.
Expand Down Expand Up @@ -435,7 +432,7 @@ type DependencyResolver struct {
StackID string

// Logger is the logger used to write to the console.
Logger *bard.Logger
Logger log.Logger
}

// NewDependencyResolver creates a new instance from the build module metadata and stack id.
Expand Down
7 changes: 3 additions & 4 deletions buildmodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
"github.com/sclevine/spec"

"github.com/paketo-buildpacks/libpak/v2"
"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/internal"
"github.com/paketo-buildpacks/libpak/v2/log"
"github.com/paketo-buildpacks/libpak/v2/sbom"
)

Expand Down Expand Up @@ -284,7 +284,6 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
)

context("Resolve", func() {

it("filters by id", func() {
resolver.Dependencies = []libpak.BuildModuleDependency{
{
Expand Down Expand Up @@ -601,8 +600,8 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {

it("prints outdated dependencies", func() {
buff := bytes.NewBuffer(nil)
logger := bard.NewLogger(buff)
resolver.Logger = &logger
logger := log.NewPaketoLogger(buff)
resolver.Logger = logger
soonDeprecated := time.Now().UTC().Add(30 * 24 * time.Hour)
notSoSoonDeprecated := time.Now().UTC().Add(60 * 24 * time.Hour)
resolver.Dependencies = []libpak.BuildModuleDependency{
Expand Down
6 changes: 3 additions & 3 deletions carton/build_image_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"os"
"regexp"

"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/internal"
"github.com/paketo-buildpacks/libpak/v2/log"
)

const (
Expand All @@ -44,8 +44,8 @@ func (i BuildImageDependency) Update(options ...Option) {
config = option(config)
}

logger := bard.NewLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", bard.FormatIdentity("Build Image", i.Version))
logger := log.NewPaketoLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity("Build Image", i.Version))

c, err := os.ReadFile(i.BuilderPath)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions carton/buildmodule_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"regexp"

"github.com/BurntSushi/toml"
"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/internal"
"github.com/paketo-buildpacks/libpak/v2/log"
)

const (
Expand Down Expand Up @@ -54,8 +54,8 @@ func (b BuildModuleDependency) Update(options ...Option) {
config = option(config)
}

logger := bard.NewLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", bard.FormatIdentity(b.ID, b.VersionPattern))
logger := log.NewPaketoLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity(b.ID, b.VersionPattern))
logger.Headerf("Version: %s", b.Version)
logger.Headerf("PURL: %s", b.PURL)
logger.Headerf("CPEs: %s", b.CPE)
Expand Down
6 changes: 3 additions & 3 deletions carton/lifecycle_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"os"
"regexp"

"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/internal"
"github.com/paketo-buildpacks/libpak/v2/log"
)

const (
Expand All @@ -44,8 +44,8 @@ func (l LifecycleDependency) Update(options ...Option) {
config = option(config)
}

logger := bard.NewLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", bard.FormatIdentity("Lifecycle", l.Version))
logger := log.NewPaketoLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity("Lifecycle", l.Version))

c, err := os.ReadFile(l.BuilderPath)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions carton/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
"github.com/heroku/color"

"github.com/paketo-buildpacks/libpak/v2"
"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/effect"
"github.com/paketo-buildpacks/libpak/v2/internal"
"github.com/paketo-buildpacks/libpak/v2/log"
)

// Package is an object that contains the configuration for building a package.
Expand Down Expand Up @@ -76,7 +76,7 @@ func (p Package) Create(options ...Option) {
file string
)

logger := bard.NewLogger(os.Stdout)
logger := log.NewPaketoLogger(os.Stdout)

// Is this a buildpack or an extension?
bpfile := filepath.Join(p.Source, "buildpack.toml")
Expand All @@ -103,7 +103,7 @@ func (p Package) Create(options ...Option) {
name = b.Info.Name
version = b.Info.Version
homepage = b.Info.Homepage
logger.Debug("Buildpack: %+v", b)
logger.Debugf("Buildpack: %+v", b)
} else if _, err := os.Stat(extnfile); err == nil {
s, err := os.ReadFile(extnfile)
if err != nil {
Expand All @@ -121,7 +121,7 @@ func (p Package) Create(options ...Option) {
version = e.Info.Version
homepage = e.Info.Homepage
extension = true
logger.Debug("Extension: %+v", e)
logger.Debugf("Extension: %+v", e)
} else {
config.exitHandler.Error(fmt.Errorf("unable to read buildpack/extension.toml at %s", p.Source))
return
Expand All @@ -138,7 +138,7 @@ func (p Package) Create(options ...Option) {
for _, i := range metadata.IncludeFiles {
entries[i] = filepath.Join(p.Source, i)
}
logger.Debug("Include files: %+v", entries)
logger.Debugf("Include files: %+v", entries)

if p.Version != "" {
version = p.Version
Expand Down
6 changes: 3 additions & 3 deletions carton/package_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"strings"

"github.com/BurntSushi/toml"
"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/internal"
"github.com/paketo-buildpacks/libpak/v2/log"
)

type PackageDependency struct {
Expand All @@ -44,8 +44,8 @@ func (p PackageDependency) Update(options ...Option) {
config = option(config)
}

logger := bard.NewLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", bard.FormatIdentity(p.ID, p.Version))
logger := log.NewPaketoLogger(os.Stdout)
_, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity(p.ID, p.Version))

if p.BuilderPath != "" {
if err := updateFile(p.BuilderPath, updateByKey("buildpacks", p.ID, p.Version)); err != nil {
Expand Down
11 changes: 5 additions & 6 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/buildpacks/libcnb/v2"
"github.com/heroku/color"

"github.com/paketo-buildpacks/libpak/v2/bard"
"github.com/paketo-buildpacks/libpak/v2/log"
"github.com/paketo-buildpacks/libpak/v2/sherpa"
)

Expand All @@ -49,15 +49,14 @@ type HttpClientTimeouts struct {
// DependencyCache allows a user to get an artifact either from a buildmodule's cache, a previous download, or to download
// directly.
type DependencyCache struct {

// CachePath is the location where the buildmodule has cached its dependencies.
CachePath string

// DownloadPath is the location of all downloads during this execution of the build.
DownloadPath string

// Logger is the logger used to write to the console.
Logger bard.Logger
Logger log.Logger

// UserAgent is the User-Agent string to use with requests.
UserAgent string
Expand All @@ -71,12 +70,13 @@ type DependencyCache struct {

// NewDependencyCache creates a new instance setting the default cache path (<BUILDMODULE_PATH>/dependencies) and user
// agent (<BUILDMODULE_ID>/<BUILDMODULE_VERSION>).
func NewDependencyCache(buildModuleID string, buildModuleVersion string, buildModulePath string, platformBindings libcnb.Bindings) (DependencyCache, error) {
func NewDependencyCache(buildModuleID string, buildModuleVersion string, buildModulePath string, platformBindings libcnb.Bindings, logger log.Logger) (DependencyCache, error) {
cache := DependencyCache{
CachePath: filepath.Join(buildModulePath, "dependencies"),
DownloadPath: os.TempDir(),
UserAgent: fmt.Sprintf("%s/%s", buildModuleID, buildModuleVersion),
Logger: logger,
Mappings: map[string]string{},
UserAgent: fmt.Sprintf("%s/%s", buildModuleID, buildModuleVersion),
}
mappings, err := mappingsFromBindings(platformBindings)
if err != nil {
Expand Down Expand Up @@ -161,7 +161,6 @@ type RequestModifierFunc func(request *http.Request) (*http.Request, error)
// If the BuildpackDependency's SHA256 is not set, the download can never be verified to be up to date and will always
// download, skipping all the caches.
func (d *DependencyCache) Artifact(dependency BuildModuleDependency, mods ...RequestModifierFunc) (*os.File, error) {

var (
artifact string
file string
Expand Down
Loading

0 comments on commit 64d6194

Please sign in to comment.