From 641e4cd8464d51d37842b26ec829aa027aed2f89 Mon Sep 17 00:00:00 2001 From: gcemaj Date: Tue, 5 Apr 2022 12:43:25 -0400 Subject: [PATCH] Add semver checks Signed-off-by: gcemaj --- build.go | 27 ++++++++++++++++++++------- build_test.go | 2 +- go.mod | 3 ++- go.sum | 6 ++++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/build.go b/build.go index 61eed5c..3c0297c 100644 --- a/build.go +++ b/build.go @@ -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" @@ -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{ @@ -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 } @@ -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, @@ -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 @@ -338,7 +351,7 @@ 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.") @@ -346,7 +359,7 @@ func Build(builder Builder, options ...Option) { } } - 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) diff --git a/build_test.go b/build_test.go index 137c147..1c5a05b 100644 --- a/build_test.go +++ b/build_test.go @@ -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", )) }) }) diff --git a/go.mod b/go.mod index ea8c208..494a338 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 438a353..762acbb 100644 --- a/go.sum +++ b/go.sum @@ -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=