Skip to content

Commit

Permalink
Add semver checks
Browse files Browse the repository at this point in the history
Signed-off-by: gcemaj <[email protected]>
  • Loading branch information
gcemaj committed Apr 6, 2022
1 parent 71c0efa commit 641e4cd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
27 changes: 20 additions & 7 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/BurntSushi/toml"
"github.com/Masterminds/semver/v3"

"github.com/buildpacks/libcnb/internal"
"github.com/buildpacks/libcnb/poet"
Expand Down Expand Up @@ -89,6 +90,12 @@ type BOM struct {
Entries []BOMEntry
}

// Constants to track minimum and maximum supported Buildpack API versions
const (
MinSupportedBPVersion = "0.5"
MaxSupportedBPVersion = "0.8"
)

// NewBuildResult creates a new BuildResult instance, initializing empty fields.
func NewBuildResult() BuildResult {
return BuildResult{
Expand Down Expand Up @@ -170,9 +177,15 @@ func Build(builder Builder, options ...Option) {
}
logger.Debugf("Buildpack: %+v", ctx.Buildpack)

API := strings.TrimSpace(ctx.Buildpack.API)
if API != "0.5" && API != "0.6" && API != "0.7" && API != "0.8" {
config.exitHandler.Error(errors.New("this version of libcnb is only compatible with buildpack APIs 0.5, 0.6, 0.7 and 0.8"))
API, err := semver.NewVersion(ctx.Buildpack.API)
if err != nil {
config.exitHandler.Error(errors.New("version cannot be parsed"))
return
}

compatVersionCheck, _ := semver.NewConstraint(fmt.Sprintf(">= %s, <= %s", MinSupportedBPVersion, MaxSupportedBPVersion))
if !compatVersionCheck.Check(API) {
config.exitHandler.Error(fmt.Errorf("this version of libcnb is only compatible with buildpack APIs >= %s, <= %s", MinSupportedBPVersion, MaxSupportedBPVersion))
return
}

Expand Down Expand Up @@ -279,7 +292,7 @@ func Build(builder Builder, options ...Option) {
file = filepath.Join(ctx.Layers.Path, fmt.Sprintf("%s.toml", layer.Name))
logger.Debugf("Writing layer metadata: %s <= %+v", file, layer)
var toWrite interface{} = layer
if API == "0.5" {
if API.Equal(semver.MustParse("0.5")) {
toWrite = internal.LayerAPI5{
Build: layer.LayerTypes.Build,
Cache: layer.LayerTypes.Cache,
Expand Down Expand Up @@ -307,7 +320,7 @@ func Build(builder Builder, options ...Option) {
}
}

if API != "0.5" && API != "0.6" {
if API.GreaterThan(semver.MustParse("0.7")) || API.Equal(semver.MustParse("0.7")) {
if err := validateSBOMFormats(ctx.Layers.Path, ctx.Buildpack.Info.SBOMFormats); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to validate SBOM\n%w", err))
return
Expand Down Expand Up @@ -338,15 +351,15 @@ func Build(builder Builder, options ...Option) {
file = filepath.Join(ctx.Layers.Path, "launch.toml")
logger.Debugf("Writing application metadata: %s <= %+v", file, launch)

if API == "0.5" {
if API.LessThan(semver.MustParse("0.6")) {
for _, process := range launch.Processes {
if process.Default {
logger.Info("WARNING: Launch layer is setting default=true, but that is not supported until API version 0.6. This setting will be ignored.")
}
}
}

if API != "0.8" {
if API.LessThan(semver.MustParse("0.8")) {
for i, process := range launch.Processes {
if process.WorkingDirectory != "" {
logger.Infof("WARNING: Launch layer is setting working-directory=%s, but that is not supported until API version 0.8. This setting will be ignored.", process.WorkingDirectory)
Expand Down
2 changes: 1 addition & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ version = "1.1.1"
)

Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(
"this version of libcnb is only compatible with buildpack APIs 0.5, 0.6, 0.7 and 0.8",
"this version of libcnb is only compatible with buildpack APIs >= 0.5, <= 0.8",
))
})
})
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/buildpacks/libcnb
go 1.17

require (
github.com/BurntSushi/toml v1.1.0
github.com/BurntSushi/toml v1.0.0
github.com/Masterminds/semver/v3 v3.1.1
github.com/onsi/gomega v1.19.0
github.com/sclevine/spec v1.4.0
github.com/stretchr/testify v1.7.1
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down

0 comments on commit 641e4cd

Please sign in to comment.