From 84dd448e6f0977624ca58c9fa80fadfe8f22e79c Mon Sep 17 00:00:00 2001 From: Daniel Mikusa Date: Tue, 16 Apr 2024 19:40:12 -0400 Subject: [PATCH] Adds `--target-arch` flag Adds a `--target-arch` flag which causes the tool to only create a build pack for the target architecture. By default, it builds all architectures. It still supports the old format of buildpack which does not include arch information in the include-files field. Signed-off-by: Daniel Mikusa --- carton/package.go | 24 ++++++++++++++++++++---- cmd/create-package/main.go | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/carton/package.go b/carton/package.go index 71e764e..3558b59 100644 --- a/carton/package.go +++ b/carton/package.go @@ -35,6 +35,8 @@ import ( "github.com/paketo-buildpacks/libpak/internal" ) +const DefaultTargetArch = "all" + // Package is an object that contains the configuration for building a package. type Package struct { @@ -58,6 +60,9 @@ type Package struct { // Version is a version to substitute into an existing buildpack.toml. Version string + + // TargetArch is the target architecture to package. Default is "all". + TargetArch string } // Create creates a package. @@ -113,7 +118,8 @@ func (p Package) Create(options ...Option) { } } - if len(supportedTargets) == 0 { + oldOutputFormat := len(supportedTargets) == 0 + if oldOutputFormat { logger.Info("No supported targets found, defaulting to old format") } @@ -122,7 +128,7 @@ func (p Package) Create(options ...Option) { entries := map[string]string{} for _, i := range metadata.IncludeFiles { - if len(supportedTargets) == 0 || strings.HasPrefix(i, "linux/") || i == "buildpack.toml" { + if oldOutputFormat || strings.HasPrefix(i, "linux/") || i == "buildpack.toml" { entries[i] = filepath.Join(p.Source, i) } else { for _, target := range supportedTargets { @@ -233,8 +239,18 @@ func (p Package) Create(options ...Option) { } sort.Strings(files) for _, d := range files { - logger.Bodyf("Adding %s", d) - file = filepath.Join(p.Destination, d) + if p.TargetArch != DefaultTargetArch && !oldOutputFormat && strings.HasPrefix(d, "linux/") && !strings.HasPrefix(d, fmt.Sprintf("linux/%s", p.TargetArch)) { + logger.Debugf("Skipping %s because target arch is %s", d, p.TargetArch) + continue + } + + targetLocation := d + if p.TargetArch != DefaultTargetArch { + targetLocation = strings.Replace(d, fmt.Sprintf("linux/%s/", p.TargetArch), "", 1) + } + + logger.Bodyf("Adding %s", targetLocation) + file = filepath.Join(p.Destination, targetLocation) if err = config.entryWriter.Write(entries[d], file); err != nil { config.exitHandler.Error(fmt.Errorf("unable to write file %s to %s\n%w", entries[d], file, err)) return diff --git a/cmd/create-package/main.go b/cmd/create-package/main.go index 2898043..24da66b 100644 --- a/cmd/create-package/main.go +++ b/cmd/create-package/main.go @@ -37,6 +37,7 @@ func main() { flagSet.BoolVar(&p.StrictDependencyFilters, "strict-filters", false, "require filter to match all data or just some data (default: false)") flagSet.StringVar(&p.Source, "source", defaultSource(), "path to build package source directory (default: $PWD)") flagSet.StringVar(&p.Version, "version", "", "version to substitute into buildpack.toml") + flagSet.StringVar(&p.TargetArch, "target-arch", carton.DefaultTargetArch, "target architecture for the package (default: all)") if err := flagSet.Parse(os.Args[1:]); err != nil { log.Fatal(fmt.Errorf("unable to parse flags\n%w", err))