Skip to content

Commit

Permalink
Consolidate methods that read and write platform spec'd TOML in platf…
Browse files Browse the repository at this point in the history
…orm/fies

With the exception of reading <layers>/config/metadata.toml in the launcher,
as we want to avoid importing the buildpack package,
and platform/files depends on it.

Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Oct 31, 2023
1 parent 7c591d1 commit 7371716
Show file tree
Hide file tree
Showing 21 changed files with 278 additions and 266 deletions.
5 changes: 3 additions & 2 deletions cmd/lifecycle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/buildpacks/lifecycle/image"
"github.com/buildpacks/lifecycle/phase"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/files"
"github.com/buildpacks/lifecycle/priv"
)

Expand Down Expand Up @@ -99,7 +100,7 @@ func (a *analyzeCmd) Exec() error {
a.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
NewCacheHandler(a.keychain),
phase.Config,
files.Handler,
image.NewHandler(a.docker, a.keychain, a.LayoutDir, a.UseLayout, a.InsecureRegistries),
image.NewRegistryHandler(a.keychain, a.InsecureRegistries),
)
Expand All @@ -111,5 +112,5 @@ func (a *analyzeCmd) Exec() error {
if err != nil {
return cmd.FailErrCode(err, a.CodeFor(platform.AnalyzeError), "analyze")
}
return phase.Config.WriteAnalyzed(a.AnalyzedPath, &analyzedMD, cmd.DefaultLogger)
return files.Handler.WriteAnalyzed(a.AnalyzedPath, &analyzedMD, cmd.DefaultLogger)
}
23 changes: 8 additions & 15 deletions cmd/lifecycle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package main
import (
"errors"

"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/cmd/lifecycle/cli"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/phase"
"github.com/buildpacks/lifecycle/platform"
Expand Down Expand Up @@ -66,7 +63,7 @@ func (b *buildCmd) Exec() error {
if err = verifyBuildpackApis(group); err != nil {
return err
}
amd, err := files.ReadAnalyzed(b.AnalyzedPath, cmd.DefaultLogger)
amd, err := files.Handler.ReadAnalyzed(b.AnalyzedPath, cmd.DefaultLogger)
if err != nil {
return unwrapErrorFailWithMessage(err, "reading analyzed.toml")
}
Expand All @@ -93,10 +90,7 @@ func (b *buildCmd) build(group buildpack.Group, plan files.Plan, analyzedMD file
if err != nil {
return b.unwrapBuildFail(err)
}
if err = encoding.WriteTOML(launch.GetMetadataFilePath(b.LayersDir), md); err != nil {
return cmd.FailErr(err, "write build metadata")
}
return nil
return files.Handler.WriteBuildMetadata(launch.GetMetadataFilePath(b.LayersDir), md)
}

func (b *buildCmd) unwrapBuildFail(err error) error {
Expand All @@ -109,14 +103,13 @@ func (b *buildCmd) unwrapBuildFail(err error) error {
}

func (b *buildCmd) readData() (buildpack.Group, files.Plan, error) {
group, err := phase.ReadGroup(b.GroupPath)
group, _, err := files.Handler.ReadGroup(b.GroupPath)
if err != nil {
return buildpack.Group{}, files.Plan{}, cmd.FailErr(err, "read buildpack group")
return buildpack.Group{}, files.Plan{}, err
}

var plan files.Plan
if _, err := toml.DecodeFile(b.PlanPath, &plan); err != nil {
return buildpack.Group{}, files.Plan{}, cmd.FailErr(err, "parse detect plan")
plan, err := files.Handler.ReadPlan(b.PlanPath)
if err != nil {
return buildpack.Group{}, files.Plan{}, err
}
return group, plan, nil
return buildpack.Group{Group: group}, plan, nil
}
6 changes: 3 additions & 3 deletions cmd/lifecycle/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (c *createCmd) Exec() error {
c.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
NewCacheHandler(c.keychain),
phase.NewConfigHandler(),
files.NewHandler(),
image.NewHandler(c.docker, c.keychain, c.LayoutDir, c.UseLayout, c.InsecureRegistries),
image.NewRegistryHandler(c.keychain, c.InsecureRegistries),
)
Expand All @@ -139,7 +139,7 @@ func (c *createCmd) Exec() error {
if err != nil {
return err
}
if err := phase.Config.WriteAnalyzed(c.AnalyzedPath, &analyzedMD, cmd.DefaultLogger); err != nil {
if err := files.Handler.WriteAnalyzed(c.AnalyzedPath, &analyzedMD, cmd.DefaultLogger); err != nil {
return err
}

Expand All @@ -148,7 +148,7 @@ func (c *createCmd) Exec() error {
detectorFactory := phase.NewHermeticFactory(
c.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
phase.NewConfigHandler(),
files.NewHandler(),
dirStore,
)
detector, err := detectorFactory.NewDetector(c.Inputs(), cmd.DefaultLogger)
Expand Down
12 changes: 6 additions & 6 deletions cmd/lifecycle/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d *detectCmd) Exec() error {
detectorFactory := phase.NewHermeticFactory(
d.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
phase.NewConfigHandler(),
files.NewHandler(),
dirStore,
)
detector, err := detectorFactory.NewDetector(
Expand All @@ -85,7 +85,7 @@ func (d *detectCmd) Exec() error {
generatorFactory := phase.NewHermeticFactory(
d.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
phase.Config,
files.Handler,
dirStore,
)
var generator *phase.Generator
Expand All @@ -102,10 +102,10 @@ func (d *detectCmd) Exec() error {
if err != nil {
return d.unwrapGenerateFail(err)
}
if err := phase.Config.WriteAnalyzed(d.AnalyzedPath, &result.AnalyzedMD, cmd.DefaultLogger); err != nil {
if err := files.Handler.WriteAnalyzed(d.AnalyzedPath, &result.AnalyzedMD, cmd.DefaultLogger); err != nil {
return err
}
if err := phase.Config.WritePlan(d.PlanPath, &result.Plan); err != nil {
if err := files.Handler.WritePlan(d.PlanPath, &result.Plan); err != nil {
return err
}
}
Expand Down Expand Up @@ -149,10 +149,10 @@ func doDetect(detector *phase.Detector, p *platform.Platform) (buildpack.Group,
return buildpack.Group{}, files.Plan{}, cmd.FailErrCode(err, p.CodeFor(platform.DetectError), "detect")
}
}
if err := phase.Config.WriteGroup(p.GroupPath, &group); err != nil {
if err := files.Handler.WriteGroup(p.GroupPath, &group); err != nil {
return buildpack.Group{}, files.Plan{}, err
}
if err := phase.Config.WritePlan(p.PlanPath, &plan); err != nil {
if err := files.Handler.WritePlan(p.PlanPath, &plan); err != nil {
return buildpack.Group{}, files.Plan{}, err
}
return group, plan, nil
Expand Down
21 changes: 7 additions & 14 deletions cmd/lifecycle/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"time"

"github.com/BurntSushi/toml"
"github.com/buildpacks/imgutil"
"github.com/buildpacks/imgutil/layout"
"github.com/buildpacks/imgutil/local"
Expand All @@ -26,7 +25,6 @@ import (
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/cmd/lifecycle/cli"
"github.com/buildpacks/lifecycle/image"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/layers"
"github.com/buildpacks/lifecycle/phase"
"github.com/buildpacks/lifecycle/platform"
Expand Down Expand Up @@ -96,7 +94,7 @@ func (e *exportCmd) Args(nargs int, args []string) error {
}
// read analyzed metadata for use in later stages
var err error
e.persistedData.analyzedMD, err = files.ReadAnalyzed(e.AnalyzedPath, cmd.DefaultLogger)
e.persistedData.analyzedMD, err = files.Handler.ReadAnalyzed(e.AnalyzedPath, cmd.DefaultLogger)
if err != nil {
return err
}
Expand Down Expand Up @@ -131,11 +129,11 @@ func (e *exportCmd) Privileges() error {
}

func (e *exportCmd) Exec() error {
group, err := phase.ReadGroup(e.GroupPath)
group, _, err := files.Handler.ReadGroup(e.GroupPath)
if err != nil {
return cmd.FailErr(err, "read buildpack group")
}
if err = verifyBuildpackApis(group); err != nil {
if err = verifyBuildpackApis(buildpack.Group{Group: group}); err != nil {
return err
}
cacheStore, err := initCache(e.CacheImageRef, e.CacheDir, e.keychain, e.PlatformAPI.LessThan("0.13"))
Expand All @@ -147,7 +145,7 @@ func (e *exportCmd) Exec() error {
return err
}
}
return e.export(group, cacheStore, e.persistedData.analyzedMD)
return e.export(buildpack.Group{Group: group}, cacheStore, e.persistedData.analyzedMD)
}

func (e *exportCmd) registryImages() []string {
Expand All @@ -162,19 +160,14 @@ func (e *exportCmd) registryImages() []string {

func (e *exportCmd) export(group buildpack.Group, cacheStore phase.Cache, analyzedMD files.Analyzed) error {
artifactsDir, err := os.MkdirTemp("", "lifecycle.exporter.layer")

if err != nil {
return cmd.FailErr(err, "create temp directory")
}
defer os.RemoveAll(artifactsDir)

var projectMD files.ProjectMetadata
_, err = toml.DecodeFile(e.ProjectMetadataPath, &projectMD)
projectMD, err := files.Handler.ReadProjectMetadata(e.ProjectMetadataPath, cmd.DefaultLogger)
if err != nil {
if !os.IsNotExist(err) {
return err
}
cmd.DefaultLogger.Debugf("no project metadata found at path '%s', project metadata will not be exported\n", e.ProjectMetadataPath)
return err
}

exporter := &phase.Exporter{
Expand Down Expand Up @@ -226,7 +219,7 @@ func (e *exportCmd) export(group buildpack.Group, cacheStore phase.Cache, analyz
if err != nil {
return cmd.FailErrCode(err, e.CodeFor(platform.ExportError), "export")
}
if err = encoding.WriteTOML(e.ReportPath, &report); err != nil {
if err = files.Handler.WriteReport(e.ReportPath, &report); err != nil {
return cmd.FailErrCode(err, e.CodeFor(platform.ExportError), "write export report")
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/lifecycle/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/buildpacks/lifecycle/internal/extend/kaniko"
"github.com/buildpacks/lifecycle/phase"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/files"
"github.com/buildpacks/lifecycle/priv"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func (e *extendCmd) Privileges() error {
}

func (e *extendCmd) Exec() error {
extenderFactory := phase.NewHermeticFactory(e.PlatformAPI, &cmd.BuildpackAPIVerifier{}, phase.NewConfigHandler(), platform.NewDirStore("", ""))
extenderFactory := phase.NewHermeticFactory(e.PlatformAPI, &cmd.BuildpackAPIVerifier{}, files.NewHandler(), platform.NewDirStore("", ""))
applier, err := kaniko.NewDockerfileApplier()
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions cmd/lifecycle/rebaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/cmd/lifecycle/cli"
"github.com/buildpacks/lifecycle/image"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/phase"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/files"
Expand Down Expand Up @@ -137,8 +136,7 @@ func (r *rebaseCmd) Exec() error {
if err != nil {
return cmd.FailErrCode(err, r.CodeFor(platform.RebaseError), "rebase")
}

if err := encoding.WriteTOML(r.ReportPath, &report); err != nil {
if err = files.Handler.WriteRebaseReport(r.ReportPath, &report); err != nil {
return cmd.FailErrCode(err, r.CodeFor(platform.RebaseError), "write rebase report")
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/lifecycle/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *restoreCmd) Privileges() error {
}

func (r *restoreCmd) Exec() error {
group, groupExt, err := phase.Config.ReadGroup(r.GroupPath)
group, groupExt, err := files.Handler.ReadGroup(r.GroupPath)
if err != nil {
return cmd.FailErr(err, "read buildpack group")
}
Expand All @@ -106,7 +106,7 @@ func (r *restoreCmd) Exec() error {
groupFile := buildpack.Group{Group: group, GroupExtensions: groupExt}

var analyzedMD files.Analyzed
if analyzedMD, err = files.ReadAnalyzed(r.AnalyzedPath, cmd.DefaultLogger); err == nil {
if analyzedMD, err = files.Handler.ReadAnalyzed(r.AnalyzedPath, cmd.DefaultLogger); err == nil {
if r.supportsBuildImageExtension() && r.BuildImageRef != "" {
cmd.DefaultLogger.Debugf("Pulling builder image metadata for %s...", r.BuildImageRef)
remoteBuildImage, err := r.pullSparse(r.BuildImageRef)
Expand Down Expand Up @@ -148,7 +148,7 @@ func (r *restoreCmd) Exec() error {
return cmd.FailErr(err, "update analyzed metadata")
}
}
if err = encoding.WriteTOML(r.AnalyzedPath, analyzedMD); err != nil {
if err = files.Handler.WriteAnalyzed(r.AnalyzedPath, &analyzedMD, cmd.DefaultLogger); err != nil {
return cmd.FailErr(err, "write analyzed metadata")
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions phase/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func (e *Exporter) Export(opts ExportOptions) (files.Report, error) {
// ensure we always copy the new RunImage into the old stack to preserve old behavior
meta.Stack = &files.Stack{RunImage: opts.RunImageForExport}

buildMD := &files.BuildMetadata{}
if err := files.DecodeBuildMetadata(launch.GetMetadataFilePath(opts.LayersDir), e.PlatformAPI, buildMD); err != nil {
buildMD, err := files.Handler.ReadBuildMetadata(launch.GetMetadataFilePath(opts.LayersDir), e.PlatformAPI)
if err != nil {
return files.Report{}, errors.Wrap(err, "read build metadata")
}

Expand Down
Loading

0 comments on commit 7371716

Please sign in to comment.