From a50d15c349a5c35b77bfe6fb31ad6e353d3b3609 Mon Sep 17 00:00:00 2001 From: Rashad Sirajudeen Date: Sat, 20 Apr 2024 00:04:23 +0530 Subject: [PATCH] todo: Remove top level dir and good to go --- pkg/client/create_builder.go | 97 +++++++++++++++--------------------- 1 file changed, 39 insertions(+), 58 deletions(-) diff --git a/pkg/client/create_builder.go b/pkg/client/create_builder.go index 1d28d64f9..330264cf7 100644 --- a/pkg/client/create_builder.go +++ b/pkg/client/create_builder.go @@ -222,7 +222,6 @@ func (c *Client) fetchLifecycle(ctx context.Context, config pubbldr.LifecycleCon return nil, err } var lifecycleImage imgutil.Image - // var blob blob.Blob imageName := buildpack.ParsePackageLocator(config.URI) c.logger.Debugf("Downloading lifecycle image: %s", style.Symbol(imageName)) @@ -238,71 +237,53 @@ func (c *Client) fetchLifecycle(ctx context.Context, config pubbldr.LifecycleCon return nil, err } - for _, layer := range layers { - diffID, err := layer.DiffID() - if err != nil { - return nil, err + // Assume the last layer has the lifecycle + diffID, err := layers[len(layers)-1].DiffID() + if err != nil { + return nil, err + } + + fmt.Printf("diffID: %s\n", diffID) + + layerContent, err := lifecycleImage.GetLayer(diffID.String()) + if err != nil { + return nil, err + } + + /////////////////// + tr := tar.NewReader(layerContent) + tr.Next() + defer layerContent.Close() // Close the ReadCloser when done + + // Iterate over the contents of the tar file + for { + header, err := tr.Next() + if err == io.EOF { + break // End of tar archive } - layerContent, err := lifecycleImage.GetLayer(diffID.String()) if err != nil { panic(err) } - defer layerContent.Close() - - // Extract the layer tarball - tarReader := tar.NewReader(layerContent) - fmt.Println("--------------------------") - fmt.Print(diffID) - fmt.Println("--------------------------") - - for { - header, err := tarReader.Next() - if err == io.EOF { - break // End of archive - } - if err != nil { - panic(err) - } - if header.Name == "cnb" && header.Typeflag == tar.TypeDir { - fmt.Println(header.Name) - } - } - fmt.Println("--------------------------") + // Print information about the current entry + fmt.Println(header.Name) + + } + ///////////////// + defer layerContent.Close() + + lifecycleLayerTar := filepath.Join(relativeBaseDir, "lifecycle-layer.tar") + lifecycleLayerWriter, err := os.OpenFile(lifecycleLayerTar, os.O_CREATE|os.O_RDWR, 0666) + if err != nil { + return nil, err + } + defer lifecycleLayerWriter.Close() + if _, err = io.Copy(lifecycleLayerWriter, layerContent); err != nil { + return nil, err } - // lifecycleImageTar := filepath.Join(relativeBaseDir, "lifecycle-image.tar") - // lifecycleImageReader, err := c.docker.ImageSave(context.Background(), []string{lifecycleImage.Name()}) - // if err != nil { - // return nil, err - // } - // defer lifecycleImageReader.Close() - // lifecycleImageWriter, err := os.Create(lifecycleImageTar) - // if err != nil { - // return nil, err - // } - // defer lifecycleImageWriter.Close() - // if _, err = io.Copy(lifecycleImageWriter, lifecycleImageReader); err != nil { - // return nil, err - // } - // lifecycleLayerName, err := LifecycleLayerName(lifecycleImageTar) - // if err != nil { - // return nil, err - // } - // if lifecycleLayerName == "" { - // return nil, errors.New("failed to find lifecycle layer") - // } - - // lifecycleLayerTar, err := LifecycleLayerTar(lifecycleImageTar, lifecycleLayerName) - // if err != nil { - // return nil, err - // } - - // uri, err = stripTopLevelDir(lifecycleLayerTar) - // if err != nil { - // return nil, err - // } + uri = lifecycleLayerTar case config.Version != "": v, err := semver.NewVersion(config.Version)