diff --git a/cmd/lifecycle/analyzer.go b/cmd/lifecycle/analyzer.go index d1138d1aa..12b1dbbe2 100644 --- a/cmd/lifecycle/analyzer.go +++ b/cmd/lifecycle/analyzer.go @@ -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" @@ -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), ) @@ -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) } diff --git a/cmd/lifecycle/creator.go b/cmd/lifecycle/creator.go index fadf67a88..6f05976c2 100644 --- a/cmd/lifecycle/creator.go +++ b/cmd/lifecycle/creator.go @@ -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") @@ -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 } } diff --git a/cmd/lifecycle/detector.go b/cmd/lifecycle/detector.go index d2aba450d..07ef35ab4 100644 --- a/cmd/lifecycle/detector.go +++ b/cmd/lifecycle/detector.go @@ -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" @@ -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 } @@ -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 { @@ -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 } diff --git a/lifecycle/detector.go b/lifecycle/detector.go index afcc35c5b..29ceea7fb 100644 --- a/lifecycle/detector.go +++ b/lifecycle/detector.go @@ -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 { diff --git a/lifecycle/generator.go b/lifecycle/generator.go index 349627a7d..12915424d 100644 --- a/lifecycle/generator.go +++ b/lifecycle/generator.go @@ -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 { @@ -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) { @@ -139,7 +136,6 @@ func (g *Generator) Generate() (GenerateResult, error) { return GenerateResult{ AnalyzedMD: finalAnalyzedMD, Plan: filteredPlan, - UsePlan: true, }, nil } diff --git a/lifecycle/handlers.go b/lifecycle/handlers.go index 651ececf3..a6a082df4 100644 --- a/lifecycle/handlers.go +++ b/lifecycle/handlers.go @@ -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" ) @@ -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) { @@ -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 @@ -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 {