Skip to content

Commit

Permalink
Add target structs
Browse files Browse the repository at this point in the history
Co-authored-by: Ralf Pannemans <[email protected]>
Signed-off-by: Nicolas Bender <[email protected]>
  • Loading branch information
nicolasbender and c0d1ngm0nk3y committed May 15, 2024
1 parent d30376d commit 1de7510
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const (
MinSupportedBPVersion = "0.8"

// MaxSupportedBPVersion indicates the maximum supported version of the Buildpacks API
MaxSupportedBPVersion = "0.9"
MaxSupportedBPVersion = "0.10"
)

// NewBuildResult creates a new BuildResult instance, initializing empty fields.
Expand Down
55 changes: 55 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,61 @@ version = "1.1.1"
})
})

context("has a build environment specifying target metadata", func() {
var ctx libcnb.BuildContext

it.Before(func() {
Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"),
[]byte(`
api = "0.10"
[buildpack]
id = "test-id"
name = "test-name"
version = "1.1.1"
[[targets]]
os = "linux"
arch = "amd64"
[[targets.distros]]
name = "ubuntu"
version = "18.04"
[[targets.distros]]
name = "debian"
[[targets]]
os = "linux"
arch = "arm"
variant = "v6"
`), 0600),
).To(Succeed())

buildFunc = func(context libcnb.BuildContext) (libcnb.BuildResult, error) {
ctx = context
return libcnb.NewBuildResult(), nil
}
})

it("provides target information", func() {
libcnb.Build(buildFunc,
libcnb.NewConfig(
libcnb.WithArguments([]string{commandPath})),
)

Expect(ctx.Buildpack.Targets).To(HaveLen(2))
Expect(ctx.Buildpack.Targets[0].OS).To(Equal("linux"))
Expect(ctx.Buildpack.Targets[0].Arch).To(Equal("amd64"))
Expect(ctx.Buildpack.Targets[0].Distros).To(HaveLen(2))
Expect(ctx.Buildpack.Targets[0].Distros[0].Name).To(Equal("ubuntu"))
Expect(ctx.Buildpack.Targets[0].Distros[0].Version).To(Equal("18.04"))
Expect(ctx.Buildpack.Targets[0].Distros[1].Name).To(Equal("debian"))

Expect(ctx.Buildpack.Targets[1].Variant).To(Equal("v6"))
})
})

it("fails if CNB_BUILDPACK_DIR is not set", func() {
Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed())

Expand Down
27 changes: 27 additions & 0 deletions buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ type BuildpackStack struct {
Mixins []string `toml:"mixins"`
}

// BuildpackTargetDistro is the supported target distro
type BuildpackTargetDistro struct {
// Name is the name of the supported distro.
Name string `toml:"name"`

// Version is the version of the supported distro.
Version string `toml:"version"`
}

// BuildpackTarget is a target supported by the buildpack.
type BuildpackTarget struct {
// OS is the supported os.
OS string `toml:"os"`

// Arch is the supported architecture.
Arch string `toml:"arch"`

// Variant is the supported variant of the architecture.
Variant string `toml:"variant"`

// Distros is the collection of distros associated with the target.
Distros []BuildpackTargetDistro `toml:"distros"`
}

// Buildpack is the contents of the buildpack.toml file.
type Buildpack struct {
// API is the api version expected by the buildpack.
Expand All @@ -99,6 +123,9 @@ type Buildpack struct {
// Deprecated: Stacks is the collection of stacks supported by the buildpack.
Stacks []BuildpackStack `toml:"stacks"`

// Targets is the collection of targets supported by the buildpack.
Targets []BuildpackTarget `toml:"targets"`

// Metadata is arbitrary metadata attached to the buildpack.
Metadata map[string]interface{} `toml:"metadata"`
}

0 comments on commit 1de7510

Please sign in to comment.