Skip to content

Commit

Permalink
Fix cmd invocations
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Sep 25, 2023
1 parent 7253994 commit 5e785e3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
10 changes: 2 additions & 8 deletions cmd/lifecycle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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/lifecycle"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/priv"
Expand Down Expand Up @@ -100,7 +99,7 @@ func (a *analyzeCmd) Exec() error {
a.PlatformAPI,
&cmd.BuildpackAPIVerifier{},
NewCacheHandler(a.keychain),
lifecycle.NewConfigHandler(),
lifecycle.Config,
image.NewHandler(a.docker, a.keychain, a.LayoutDir, a.UseLayout, a.InsecureRegistries),
image.NewRegistryHandler(a.keychain, a.InsecureRegistries),
)
Expand All @@ -112,10 +111,5 @@ func (a *analyzeCmd) Exec() error {
if err != nil {
return cmd.FailErrCode(err, a.CodeFor(platform.AnalyzeError), "analyze")
}
cmd.DefaultLogger.Debugf("Run image info in analyzed metadata is: ")
cmd.DefaultLogger.Debugf(encoding.ToJSONMaybe(analyzedMD.RunImage))
if err = encoding.WriteTOML(a.AnalyzedPath, analyzedMD); err != nil {
return cmd.FailErr(err, "write analyzed")
}
return nil
return lifecycle.Config.WriteAnalyzed(a.AnalyzedPath, &analyzedMD, cmd.DefaultLogger)
}
6 changes: 4 additions & 2 deletions cmd/lifecycle/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func (c *createCmd) Exec() error {
if err != nil {
return err
}
if err := lifecycle.Config.WriteAnalyzed(c.AnalyzedPath, &analyzedMD, cmd.DefaultLogger); err != nil {
return err
}

// Detect
cmd.DefaultLogger.Phase("DETECTING")
Expand All @@ -164,8 +167,7 @@ func (c *createCmd) Exec() error {
Platform: c.Platform,
keychain: c.keychain,
}
err := restoreCmd.restore(analyzedMD.LayersMetadata, group, cacheStore)
if err != nil {
if err := restoreCmd.restore(analyzedMD.LayersMetadata, group, cacheStore); err != nil {
return err
}
}
Expand Down
37 changes: 10 additions & 27 deletions cmd/lifecycle/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"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/lifecycle"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/files"
Expand Down Expand Up @@ -78,7 +77,7 @@ func (d *detectCmd) Exec() error {
return err
}
}
group, plan, err := doDetect(detector, d.Platform)
group, _, err := doDetect(detector, d.Platform)
if err != nil {
return err // pass through error
}
Expand All @@ -103,16 +102,14 @@ func (d *detectCmd) Exec() error {
if err != nil {
return d.unwrapGenerateFail(err)
}

if err = d.writeGenerateData(result.AnalyzedMD); err != nil {
if err := lifecycle.Config.WriteAnalyzed(d.AnalyzedPath, &result.AnalyzedMD, cmd.DefaultLogger); err != nil {
return err
}
// was the build plan updated?
if result.UsePlan {
plan = result.Plan
if err := lifecycle.Config.WritePlan(d.PlanPath, &result.Plan); err != nil {
return err
}
}
return d.writeDetectData(group, plan)
return nil
}

func unwrapErrorFailWithMessage(err error, msg string) error {
Expand Down Expand Up @@ -152,25 +149,11 @@ func doDetect(detector *lifecycle.Detector, p *platform.Platform) (buildpack.Gro
return buildpack.Group{}, files.Plan{}, cmd.FailErrCode(err, p.CodeFor(platform.DetectError), "detect")
}
}
return group, plan, nil
}

func (d *detectCmd) writeDetectData(group buildpack.Group, plan files.Plan) error {
if err := encoding.WriteTOML(d.GroupPath, group); err != nil {
return cmd.FailErr(err, "write buildpack group")
}
if err := encoding.WriteTOML(d.PlanPath, plan); err != nil {
return cmd.FailErr(err, "write detect plan")
if err := lifecycle.Config.WriteGroup(p.GroupPath, &group); err != nil {
return buildpack.Group{}, files.Plan{}, err
}
return nil
}

// writeGenerateData re-outputs the analyzedMD that we read previously, but now we've added the RunImage, if a custom runImage was configured
func (d *detectCmd) writeGenerateData(analyzedMD files.Analyzed) error {
cmd.DefaultLogger.Debugf("Run image info in analyzed metadata is: ")
cmd.DefaultLogger.Debugf(encoding.ToJSONMaybe(analyzedMD.RunImage))
if err := encoding.WriteTOML(d.AnalyzedPath, analyzedMD); err != nil {
return cmd.FailErr(err, "write analyzed metadata")
if err := lifecycle.Config.WritePlan(p.PlanPath, &plan); err != nil {
return buildpack.Group{}, files.Plan{}, err
}
return nil
return group, plan, nil
}
1 change: 0 additions & 1 deletion lifecycle/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func (f *HermeticFactory) NewDetector(inputs platform.LifecycleInputs, logger lo
}
var err error
if detector.AnalyzeMD, err = f.configHandler.ReadAnalyzed(inputs.AnalyzedPath, logger); err != nil {
// TODO: fix creator
return nil, err
}
if detector.Order, detector.HasExtensions, err = f.getOrder(inputs.OrderPath, logger); err != nil {
Expand Down
4 changes: 0 additions & 4 deletions lifecycle/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ func (f *HermeticFactory) NewGenerator(inputs platform.LifecycleInputs, stdout,
}
var err error
if generator.Extensions, err = f.getExtensions(inputs.GroupPath, logger); err != nil {
// TODO: fix detector
return nil, err
}
if generator.Plan, err = f.configHandler.ReadPlan(inputs.PlanPath); err != nil {
// TODO: fix creator
return nil, err
}
if generator.RunMetadata, err = f.configHandler.ReadRun(inputs.RunPath, logger); err != nil {
Expand All @@ -68,7 +66,6 @@ func (f *HermeticFactory) NewGenerator(inputs platform.LifecycleInputs, stdout,
type GenerateResult struct {
AnalyzedMD files.Analyzed
Plan files.Plan
UsePlan bool
}

func (g *Generator) Generate() (GenerateResult, error) {
Expand Down Expand Up @@ -139,7 +136,6 @@ func (g *Generator) Generate() (GenerateResult, error) {
return GenerateResult{
AnalyzedMD: finalAnalyzedMD,
Plan: filteredPlan,
UsePlan: true,
}, nil
}

Expand Down
32 changes: 30 additions & 2 deletions lifecycle/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/log"
"github.com/buildpacks/lifecycle/platform/files"
)
Expand Down Expand Up @@ -55,8 +56,19 @@ func NewConfigHandler() *DefaultConfigHandler {
return &DefaultConfigHandler{}
}

func (h *DefaultConfigHandler) ReadAnalyzed(path string, logr log.Logger) (files.Analyzed, error) {
return files.ReadAnalyzed(path, logr)
// ReadAnalyzed TODO
func (h *DefaultConfigHandler) ReadAnalyzed(path string, logger log.Logger) (files.Analyzed, error) {
return files.ReadAnalyzed(path, logger)
}

// WriteAnalyzed TODO
func (h *DefaultConfigHandler) WriteAnalyzed(path string, analyzedMD *files.Analyzed, logger log.Logger) error {
logger.Debugf("Run image info in analyzed metadata is: ")
logger.Debugf(encoding.ToJSONMaybe(analyzedMD.RunImage))
if err := encoding.WriteTOML(path, analyzedMD); err != nil {
return fmt.Errorf("failed to write analyzed: %w", err)
}
return nil
}

func (h *DefaultConfigHandler) ReadGroup(path string) (buildpackGroup []buildpack.GroupElement, extensionsGroup []buildpack.GroupElement, err error) {
Expand All @@ -78,6 +90,14 @@ func ReadGroup(path string) (buildpack.Group, error) {
return group, err
}

// WriteGroup TODO
func (h *DefaultConfigHandler) WriteGroup(path string, group *buildpack.Group) error {
if err := encoding.WriteTOML(path, group); err != nil {
return fmt.Errorf("failed to write group: %w", err)
}
return nil
}

// ReadPlan TODO
func (h *DefaultConfigHandler) ReadPlan(path string) (files.Plan, error) {
var plan files.Plan
Expand All @@ -87,6 +107,14 @@ func (h *DefaultConfigHandler) ReadPlan(path string) (files.Plan, error) {
return plan, nil
}

// WritePlan TODO
func (h *DefaultConfigHandler) WritePlan(path string, plan *files.Plan) error {
if err := encoding.WriteTOML(path, plan); err != nil {
return fmt.Errorf("failed to write plan: %w", err)
}
return nil
}

func (h *DefaultConfigHandler) ReadOrder(path string) (buildpack.Order, buildpack.Order, error) {
orderBp, orderExt, err := ReadOrder(path)
if err != nil {
Expand Down

0 comments on commit 5e785e3

Please sign in to comment.