Skip to content

Commit

Permalink
Merge pull request #331 from paketo-buildpacks/gh-330
Browse files Browse the repository at this point in the history
Adds `--target-arch` flag
  • Loading branch information
dmikusa authored Apr 17, 2024
2 parents 99fe914 + 84dd448 commit 72711c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 20 additions & 4 deletions carton/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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.
Expand Down Expand Up @@ -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")
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/create-package/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 72711c3

Please sign in to comment.