From 578a465f0937767aa6bacfc18f7f9087c5bb2937 Mon Sep 17 00:00:00 2001 From: Daniel Mikusa Date: Mon, 28 Oct 2024 22:03:51 -0400 Subject: [PATCH] Remove commands that were moved to libpak-tools & move carton package into libpak-tools The rest of this is moving the internal packages that were all used by libpak before to now be external packages so that they're accessible by libpak-tools. This is a trade-off that we'll need to make in order to split these up, but I think it's OK. Some of these can be useful outside of libpak. Signed-off-by: Daniel Mikusa --- build.go | 8 +- buildmodule_test.go | 4 +- carton/build_image_dependency.go | 71 --- carton/build_image_dependency_test.go | 74 --- carton/buildmodule_dependency.go | 195 ------ carton/buildmodule_dependency_test.go | 425 ------------- carton/config.go | 66 -- carton/lifecycle_dependency.go | 71 --- carton/lifecycle_dependency_test.go | 80 --- carton/mocks/entry_writer.go | 38 -- carton/netrc.go | 117 ---- carton/netrc_test.go | 217 ------- carton/package.go | 276 -------- carton/package_dependency.go | 190 ------ carton/package_dependency_test.go | 224 ------- carton/package_test.go | 594 ------------------ carton/testdata/test-sha256-1.toml | 5 - carton/testdata/test-sha256-1/test-uri-1 | 0 carton/testdata/test-sha256-2.toml | 5 - carton/testdata/test-sha256-2/test-uri-2 | 0 carton/testdata/test-sha256-3.toml | 5 - carton/testdata/test-sha256-3/test-uri-3 | 0 cmd/create-package/main.go | 59 -- cmd/update-build-image-dependency/main.go | 49 -- cmd/update-buildmodule-dependency/main.go | 89 --- cmd/update-lifecycle-dependency/main.go | 49 -- cmd/update-package-dependency/main.go | 56 -- detect.go | 6 +- generate.go | 8 +- go.mod | 1 - go.sum | 2 - internal/toml_marshal.go | 13 - layer.go | 4 +- main.go | 14 +- sherpa/sherpa.go | 4 +- internal/match_toml.go => testing/matchers.go | 2 +- {internal => utils}/entry_writer.go | 4 +- {internal => utils}/entry_writer_test.go | 8 +- {internal => utils}/environment_writer.go | 4 +- .../environment_writer_test.go | 12 +- {internal => utils}/exit_handler.go | 4 +- {internal => utils}/exit_handler_test.go | 16 +- {internal => utils}/init_test.go | 8 +- carton/init_test.go => utils/toml_marshal.go | 22 +- {internal => utils}/toml_marshal_test.go | 21 +- {internal => utils}/toml_writer.go | 4 +- {internal => utils}/toml_writer_test.go | 13 +- 47 files changed, 88 insertions(+), 3049 deletions(-) delete mode 100644 carton/build_image_dependency.go delete mode 100644 carton/build_image_dependency_test.go delete mode 100644 carton/buildmodule_dependency.go delete mode 100644 carton/buildmodule_dependency_test.go delete mode 100644 carton/config.go delete mode 100644 carton/lifecycle_dependency.go delete mode 100644 carton/lifecycle_dependency_test.go delete mode 100644 carton/mocks/entry_writer.go delete mode 100644 carton/netrc.go delete mode 100644 carton/netrc_test.go delete mode 100644 carton/package.go delete mode 100644 carton/package_dependency.go delete mode 100644 carton/package_dependency_test.go delete mode 100644 carton/package_test.go delete mode 100644 carton/testdata/test-sha256-1.toml delete mode 100644 carton/testdata/test-sha256-1/test-uri-1 delete mode 100644 carton/testdata/test-sha256-2.toml delete mode 100644 carton/testdata/test-sha256-2/test-uri-2 delete mode 100644 carton/testdata/test-sha256-3.toml delete mode 100644 carton/testdata/test-sha256-3/test-uri-3 delete mode 100644 cmd/create-package/main.go delete mode 100644 cmd/update-build-image-dependency/main.go delete mode 100644 cmd/update-buildmodule-dependency/main.go delete mode 100644 cmd/update-lifecycle-dependency/main.go delete mode 100644 cmd/update-package-dependency/main.go delete mode 100644 internal/toml_marshal.go rename internal/match_toml.go => testing/matchers.go (99%) rename {internal => utils}/entry_writer.go (96%) rename {internal => utils}/entry_writer_test.go (94%) rename {internal => utils}/environment_writer.go (97%) rename {internal => utils}/environment_writer_test.go (89%) rename {internal => utils}/exit_handler.go (97%) rename {internal => utils}/exit_handler_test.go (82%) rename {internal => utils}/init_test.go (86%) rename carton/init_test.go => utils/toml_marshal.go (52%) rename {internal => utils}/toml_marshal_test.go (54%) rename {internal => utils}/toml_writer.go (98%) rename {internal => utils}/toml_writer_test.go (90%) diff --git a/build.go b/build.go index bde2f76..2da13ca 100644 --- a/build.go +++ b/build.go @@ -19,17 +19,17 @@ package libpak import ( "github.com/buildpacks/libcnb/v2" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" + "github.com/paketo-buildpacks/libpak/v2/utils" ) // Build is called by the main function of a buildpack, for build. func Build(builder libcnb.BuildFunc, options ...libcnb.Option) { libcnb.Build(buildDelegate{delegate: builder}.Build, libcnb.NewConfig(append([]libcnb.Option{ - libcnb.WithEnvironmentWriter(internal.NewEnvironmentWriter()), - libcnb.WithExitHandler(internal.NewExitHandler()), - libcnb.WithTOMLWriter(internal.NewTOMLWriter()), + libcnb.WithEnvironmentWriter(utils.NewEnvironmentWriter()), + libcnb.WithExitHandler(utils.NewExitHandler()), + libcnb.WithTOMLWriter(utils.NewTOMLWriter()), }, options...)...)) } diff --git a/buildmodule_test.go b/buildmodule_test.go index 02be140..6c8aa22 100644 --- a/buildmodule_test.go +++ b/buildmodule_test.go @@ -29,9 +29,9 @@ import ( "github.com/sclevine/spec" "github.com/paketo-buildpacks/libpak/v2" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" "github.com/paketo-buildpacks/libpak/v2/sbom" + "github.com/paketo-buildpacks/libpak/v2/utils" ) func testBuildpack(t *testing.T, context spec.G, it spec.S) { @@ -56,7 +56,7 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) { }, } - bytes, err := internal.Marshal(dependency) + bytes, err := utils.Marshal(dependency) Expect(err).NotTo(HaveOccurred()) var newDependency libpak.BuildModuleDependency diff --git a/carton/build_image_dependency.go b/carton/build_image_dependency.go deleted file mode 100644 index 2491a93..0000000 --- a/carton/build_image_dependency.go +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton - -import ( - "fmt" - "os" - "regexp" - - "github.com/paketo-buildpacks/libpak/v2/internal" - "github.com/paketo-buildpacks/libpak/v2/log" -) - -const ( - ImageDependencyPattern = `(?m)(.*build-image[\s]+=[\s]+"[^"]+:)[^"]+(".*)` - ImageDependencySubstitution = "${1}%s${2}" -) - -type BuildImageDependency struct { - BuilderPath string - Version string -} - -func (i BuildImageDependency) Update(options ...Option) { - config := Config{ - exitHandler: internal.NewExitHandler(), - } - - for _, option := range options { - config = option(config) - } - - logger := log.NewPaketoLogger(os.Stdout) - _, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity("Build Image", i.Version)) - - c, err := os.ReadFile(i.BuilderPath) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to read %s\n%w", i.BuilderPath, err)) - return - } - - r := regexp.MustCompile(ImageDependencyPattern) - - if !r.Match(c) { - config.exitHandler.Error(fmt.Errorf("unable to match '%s'", r.String())) - return - } - - s := fmt.Sprintf(ImageDependencySubstitution, i.Version) - c = r.ReplaceAll(c, []byte(s)) - - // #nosec G306 - permissions need to be 644 on the builder - if err := os.WriteFile(i.BuilderPath, c, 0644); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", i.BuilderPath, err)) - return - } -} diff --git a/carton/build_image_dependency_test.go b/carton/build_image_dependency_test.go deleted file mode 100644 index bfb9a4c..0000000 --- a/carton/build_image_dependency_test.go +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton_test - -import ( - "os" - "testing" - - "github.com/buildpacks/libcnb/v2/mocks" - . "github.com/onsi/gomega" - "github.com/sclevine/spec" - "github.com/stretchr/testify/mock" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func testBuildImageDependency(t *testing.T, _ spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - exitHandler *mocks.ExitHandler - path string - ) - - it.Before(func() { - var err error - - exitHandler = &mocks.ExitHandler{} - exitHandler.On("Error", mock.Anything) - - f, err := os.CreateTemp("", "carton-image-dependency") - Expect(err).NotTo(HaveOccurred()) - - _, err = f.WriteString(`test-prologue -build-image = "image-name:test-version-1" -test-epilogue -`) - Expect(err).To(Succeed()) - Expect(f.Close()).To(Succeed()) - path = f.Name() - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) - - it("updates dependency", func() { - d := carton.BuildImageDependency{ - BuilderPath: path, - Version: "test-version-2", - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(Equal([]byte(`test-prologue -build-image = "image-name:test-version-2" -test-epilogue -`))) - }) -} diff --git a/carton/buildmodule_dependency.go b/carton/buildmodule_dependency.go deleted file mode 100644 index 88ff780..0000000 --- a/carton/buildmodule_dependency.go +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton - -import ( - "bytes" - "fmt" - "os" - "regexp" - - "github.com/BurntSushi/toml" - - "github.com/paketo-buildpacks/libpak/v2/internal" - "github.com/paketo-buildpacks/libpak/v2/log" -) - -const ( - BuildModuleDependencyPattern = `(?m)([\s]*.*id[\s]+=[\s]+"%s"\n.*\n[\s]*version[\s]+=[\s]+")%s("\n[\s]*uri[\s]+=[\s]+").*("\n[\s]*sha256[\s]+=[\s]+").*(".*)` - BuildModuleDependencySubstitution = "${1}%s${2}%s${3}%s${4}" -) - -type BuildModuleDependency struct { - BuildModulePath string - ID string - SHA256 string - URI string - Version string - VersionPattern string - CPE string - CPEPattern string - PURL string - PURLPattern string -} - -func (b BuildModuleDependency) Update(options ...Option) { - config := Config{ - exitHandler: internal.NewExitHandler(), - } - - for _, option := range options { - config = option(config) - } - - logger := log.NewPaketoLogger(os.Stdout) - _, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity(b.ID, b.VersionPattern)) - logger.Headerf("Version: %s", b.Version) - logger.Headerf("PURL: %s", b.PURL) - logger.Headerf("CPEs: %s", b.CPE) - logger.Headerf("URI: %s", b.URI) - logger.Headerf("SHA256: %s", b.SHA256) - - versionExp, err := regexp.Compile(b.VersionPattern) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to compile version regex %s\n%w", b.VersionPattern, err)) - return - } - - cpeExp, err := regexp.Compile(b.CPEPattern) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to compile cpe regex %s\n%w", b.CPEPattern, err)) - return - } - - purlExp, err := regexp.Compile(b.PURLPattern) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to compile cpe regex %s\n%w", b.PURLPattern, err)) - return - } - - c, err := os.ReadFile(b.BuildModulePath) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to read %s\n%w", b.BuildModulePath, err)) - return - } - - // save any leading comments, this is to preserve license headers - // inline comments will be lost - comments := []byte{} - for i, line := range bytes.SplitAfter(c, []byte("\n")) { - if bytes.HasPrefix(line, []byte("#")) || (i > 0 && len(bytes.TrimSpace(line)) == 0) { - comments = append(comments, line...) - } else { - break // stop on first comment - } - } - - md := make(map[string]interface{}) - if err := toml.Unmarshal(c, &md); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to decode md%s\n%w", b.BuildModulePath, err)) - return - } - - metadataUnwrapped, found := md["metadata"] - if !found { - config.exitHandler.Error(fmt.Errorf("unable to find metadata block")) - return - } - - metadata, ok := metadataUnwrapped.(map[string]interface{}) - if !ok { - config.exitHandler.Error(fmt.Errorf("unable to cast metadata")) - return - } - - dependenciesUnwrapped, found := metadata["dependencies"] - if !found { - config.exitHandler.Error(fmt.Errorf("unable to find dependencies block")) - return - } - - dependencies, ok := dependenciesUnwrapped.([]map[string]interface{}) - if !ok { - config.exitHandler.Error(fmt.Errorf("unable to cast dependencies")) - return - } - - for _, dep := range dependencies { - depIDUnwrapped, found := dep["id"] - if !found { - continue - } - depID, ok := depIDUnwrapped.(string) - if !ok { - continue - } - - if depID == b.ID { - depVersionUnwrapped, found := dep["version"] - if !found { - continue - } - - depVersion, ok := depVersionUnwrapped.(string) - if !ok { - continue - } - if versionExp.MatchString(depVersion) { - dep["version"] = b.Version - dep["uri"] = b.URI - dep["sha256"] = b.SHA256 - - purlUnwrapped, found := dep["purl"] - if found { - purl, ok := purlUnwrapped.(string) - if ok { - dep["purl"] = purlExp.ReplaceAllString(purl, b.PURL) - } - } - - cpesUnwrapped, found := dep["cpes"] - if found { - cpes, ok := cpesUnwrapped.([]interface{}) - if ok { - for i := 0; i < len(cpes); i++ { - cpe, ok := cpes[i].(string) - if !ok { - continue - } - - cpes[i] = cpeExp.ReplaceAllString(cpe, b.CPE) - } - } - } - } - } - } - - c, err = internal.Marshal(md) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to encode md %s\n%w", b.BuildModulePath, err)) - return - } - - c = append(comments, c...) - - // #nosec G306 - permissions need to be 644 on the build module - if err := os.WriteFile(b.BuildModulePath, c, 0644); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", b.BuildModulePath, err)) - return - } -} diff --git a/carton/buildmodule_dependency_test.go b/carton/buildmodule_dependency_test.go deleted file mode 100644 index 5aab269..0000000 --- a/carton/buildmodule_dependency_test.go +++ /dev/null @@ -1,425 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton_test - -import ( - "os" - "testing" - - "github.com/buildpacks/libcnb/v2/mocks" - . "github.com/onsi/gomega" - "github.com/sclevine/spec" - "github.com/stretchr/testify/mock" - - "github.com/paketo-buildpacks/libpak/v2/carton" - "github.com/paketo-buildpacks/libpak/v2/internal" -) - -func testBuildpackDependency(t *testing.T, context spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - exitHandler *mocks.ExitHandler - path string - ) - - it.Before(func() { - var err error - - exitHandler = &mocks.ExitHandler{} - exitHandler.On("Error", mock.Anything) - - f, err := os.CreateTemp("", "carton-buildpack-dependency") - Expect(err).NotTo(HaveOccurred()) - Expect(f.Close()).To(Succeed()) - path = f.Name() - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) - - it("updates dependency", func() { - Expect(os.WriteFile(path, []byte(`api = "0.6" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-1" -uri = "test-uri-1" -sha256 = "test-sha256-1" -stacks = [ "test-stack" ] -`), 0600)).To(Succeed()) - - d := carton.BuildModuleDependency{ - BuildModulePath: path, - ID: "test-id", - SHA256: "test-sha256-2", - URI: "test-uri-2", - Version: "test-version-2", - VersionPattern: `test-version-[\d]`, - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.6" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-2" -uri = "test-uri-2" -sha256 = "test-sha256-2" -stacks = [ "test-stack" ] -`)) - }) - - it("updates dependency with purl & cpes", func() { - Expect(os.WriteFile(path, []byte(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-1" -uri = "test-uri-1" -sha256 = "test-sha256-1" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-1?arch=amd64" -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-1:patch1:*:*:*:*:*:*:*"] -`), 0600)).To(Succeed()) - - d := carton.BuildModuleDependency{ - BuildModulePath: path, - ID: "test-id", - SHA256: "test-sha256-2", - URI: "test-uri-2", - Version: "test-version-2", - VersionPattern: `test-version-[\d]`, - PURL: "different-version-2", - PURLPattern: `different-version-[\d]`, - CPE: "test-version-2:patch2", - CPEPattern: `test-version-[\d]:patch[\d]`, - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-2" -uri = "test-uri-2" -sha256 = "test-sha256-2" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-2?arch=amd64" -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*:*"] -`)) - }) - - it("updates multiple dependencies with different versions", func() { - Expect(os.WriteFile(path, []byte(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-1" -uri = "test-uri-1" -sha256 = "test-sha256-1" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-1?arch=amd64" -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-1:patch1:*:*:*:*:*:*:*"] - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-2" -uri = "test-uri-2" -sha256 = "test-sha256-2" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-2?arch=amd64" -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*:*"] -`), 0600)).To(Succeed()) - - d := carton.BuildModuleDependency{ - BuildModulePath: path, - ID: "test-id", - SHA256: "test-sha256-3", - URI: "test-uri-3", - Version: "test-version-3", - VersionPattern: `test-version-1`, - PURL: "different-version-3", - PURLPattern: `different-version-[\d]`, - CPE: "test-version-3:patch3", - CPEPattern: `test-version-[\d]:patch[\d]`, - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-3" -uri = "test-uri-3" -sha256 = "test-sha256-3" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-3?arch=amd64" -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-3:patch3:*:*:*:*:*:*:*"] - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-2" -uri = "test-uri-2" -sha256 = "test-sha256-2" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-2?arch=amd64" -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*:*"] -`)) - }) - - it("updates dependency with missing purl, still updates cpe", func() { - Expect(os.WriteFile(path, []byte(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-1" -uri = "test-uri-1" -sha256 = "test-sha256-1" -stacks = [ "test-stack" ] -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-1:patch1:*:*:*:*:*:*:*"] -`), 0600)).To(Succeed()) - - d := carton.BuildModuleDependency{ - BuildModulePath: path, - ID: "test-id", - SHA256: "test-sha256-2", - URI: "test-uri-2", - Version: "test-version-2", - VersionPattern: `test-version-[\d]`, - PURL: "different-version-2", - PURLPattern: `different-version-[\d]`, - CPE: "test-version-2:patch2", - CPEPattern: `test-version-[\d]:patch[\d]`, - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-2" -uri = "test-uri-2" -sha256 = "test-sha256-2" -stacks = [ "test-stack" ] -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*:*"] -`)) - }) - - it("updates dependency with invalid purl, still updates cpe", func() { - Expect(os.WriteFile(path, []byte(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-1" -uri = "test-uri-1" -sha256 = "test-sha256-1" -stacks = [ "test-stack" ] -purl = 1234 -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-1:patch1:*:*:*:*:*:*:*"] -`), 0600)).To(Succeed()) - - d := carton.BuildModuleDependency{ - BuildModulePath: path, - ID: "test-id", - SHA256: "test-sha256-2", - URI: "test-uri-2", - Version: "test-version-2", - VersionPattern: `test-version-[\d]`, - PURL: "different-version-2", - PURLPattern: `different-version-[\d]`, - CPE: "test-version-2:patch2", - CPEPattern: `test-version-[\d]:patch[\d]`, - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-2" -uri = "test-uri-2" -sha256 = "test-sha256-2" -stacks = [ "test-stack" ] -purl = 1234 -cpes = ["cpe:2.3:a:test-vendor:test-product:test-version-2:patch2:*:*:*:*:*:*:*"] -`)) - }) - - it("updates dependency with invalid cpe, still updates purl", func() { - Expect(os.WriteFile(path, []byte(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-1" -uri = "test-uri-1" -sha256 = "test-sha256-1" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-1?arch=amd64" -cpes = 1234 -`), 0600)).To(Succeed()) - - d := carton.BuildModuleDependency{ - BuildModulePath: path, - ID: "test-id", - SHA256: "test-sha256-2", - URI: "test-uri-2", - Version: "test-version-2", - VersionPattern: `test-version-[\d]`, - PURL: "different-version-2", - PURLPattern: `different-version-[\d]`, - CPE: "test-version-2:patch2", - CPEPattern: `test-version-[\d]:patch[\d]`, - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`api = "0.7" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] -id = "test-id" -name = "Test Name" -version = "test-version-2" -uri = "test-uri-2" -sha256 = "test-sha256-2" -stacks = [ "test-stack" ] -purl = "pkg:generic/test-jre@different-version-2?arch=amd64" -cpes = 1234 -`)) - }) - - it("updates indented dependency", func() { - Expect(os.WriteFile(path, []byte(`# it should preserve -# these comments -# exactly - -api = "0.6" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] - id = "test-id" - name = "Test Name" - version = "test-version-1" - uri = "test-uri-1" - sha256 = "test-sha256-1" - stacks = [ "test-stack" ] -`), 0600)).To(Succeed()) - - d := carton.BuildModuleDependency{ - BuildModulePath: path, - ID: "test-id", - SHA256: "test-sha256-2", - URI: "test-uri-2", - Version: "test-version-2", - VersionPattern: `test-version-[\d]`, - } - - d.Update(carton.WithExitHandler(exitHandler)) - - body, err := os.ReadFile(path) - Expect(err).NotTo(HaveOccurred()) - Expect(string(body)).To(HavePrefix(`# it should preserve -# these comments -# exactly - -api = "0.6"`)) - Expect(body).To(internal.MatchTOML(`api = "0.6" -[buildpack] -id = "some-buildpack" -name = "Some Buildpack" -version = "1.2.3" - -[[metadata.dependencies]] - id = "test-id" - name = "Test Name" - version = "test-version-2" - uri = "test-uri-2" - sha256 = "test-sha256-2" - stacks = [ "test-stack" ] -`)) - }) -} diff --git a/carton/config.go b/carton/config.go deleted file mode 100644 index e1e04ec..0000000 --- a/carton/config.go +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton - -import ( - "github.com/buildpacks/libcnb/v2" - - "github.com/paketo-buildpacks/libpak/v2/effect" -) - -//go:generate mockery --name EntryWriter --case=underscore - -// EntryWriter is the interface implemented by a type that wants to write an entry. -type EntryWriter interface { - - // Write reads a file from source and writes it to the relative path destination. - Write(source string, destination string) error -} - -// Config is an object that contains configurable properties for execution. -type Config struct { - entryWriter EntryWriter - executor effect.Executor - exitHandler libcnb.ExitHandler -} - -// Option is a function for configuring a Config instance. -type Option func(config Config) Config - -// WithEntryWriter creates an Option that sets an EntryWriter implementation. -func WithEntryWriter(entryWriter EntryWriter) Option { - return func(config Config) Config { - config.entryWriter = entryWriter - return config - } -} - -// WithExecutor creates an Option that sets an Executor implementation. -func WithExecutor(executor effect.Executor) Option { - return func(config Config) Config { - config.executor = executor - return config - } -} - -// WithExitHandler creates an Option that sets an ExitHandler implementation. -func WithExitHandler(exitHandler libcnb.ExitHandler) Option { - return func(config Config) Config { - config.exitHandler = exitHandler - return config - } -} diff --git a/carton/lifecycle_dependency.go b/carton/lifecycle_dependency.go deleted file mode 100644 index d7a3674..0000000 --- a/carton/lifecycle_dependency.go +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton - -import ( - "fmt" - "os" - "regexp" - - "github.com/paketo-buildpacks/libpak/v2/internal" - "github.com/paketo-buildpacks/libpak/v2/log" -) - -const ( - LifecycleDependencyPattern = `(?m)(.*\[lifecycle\]\nuri[\s]+=[\s]+")[^"]+(".*)` - LifecycleDependencySubstitution = "${1}https://github.com/buildpacks/lifecycle/releases/download/v%[1]s/lifecycle-v%[1]s+linux.x86-64.tgz${2}" -) - -type LifecycleDependency struct { - BuilderPath string - Version string -} - -func (l LifecycleDependency) Update(options ...Option) { - config := Config{ - exitHandler: internal.NewExitHandler(), - } - - for _, option := range options { - config = option(config) - } - - logger := log.NewPaketoLogger(os.Stdout) - _, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity("Lifecycle", l.Version)) - - c, err := os.ReadFile(l.BuilderPath) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to read %s\n%w", l.BuilderPath, err)) - return - } - - r := regexp.MustCompile(LifecycleDependencyPattern) - - if !r.Match(c) { - config.exitHandler.Error(fmt.Errorf("unable to match '%s'", LifecycleDependencyPattern)) - return - } - - s := fmt.Sprintf(LifecycleDependencySubstitution, l.Version) - c = r.ReplaceAll(c, []byte(s)) - - // #nosec G306 - permissions need to be 644 on the builder - if err := os.WriteFile(l.BuilderPath, c, 0644); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to write %s\n%w", l.BuilderPath, err)) - return - } -} diff --git a/carton/lifecycle_dependency_test.go b/carton/lifecycle_dependency_test.go deleted file mode 100644 index 4c029cc..0000000 --- a/carton/lifecycle_dependency_test.go +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton_test - -import ( - "os" - "testing" - - "github.com/buildpacks/libcnb/v2/mocks" - . "github.com/onsi/gomega" - "github.com/sclevine/spec" - "github.com/stretchr/testify/mock" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func testLifecycleDependency(t *testing.T, _ spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - exitHandler *mocks.ExitHandler - path string - ) - - it.Before(func() { - var err error - - exitHandler = &mocks.ExitHandler{} - exitHandler.On("Error", mock.Anything) - - f, err := os.CreateTemp("", "carton-builder-dependency") - Expect(err).NotTo(HaveOccurred()) - - _, err = f.WriteString(`test-prologue - -[lifecycle] -uri = "test-uri" - -test-epilogue -`) - Expect(err).To(Succeed()) - Expect(f.Close()).To(Succeed()) - path = f.Name() - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) - - it("updates dependency", func() { - d := carton.LifecycleDependency{ - BuilderPath: path, - Version: "test-version-3", - } - - d.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(Equal([]byte(`test-prologue - -[lifecycle] -uri = "https://github.com/buildpacks/lifecycle/releases/download/vtest-version-3/lifecycle-vtest-version-3+linux.x86-64.tgz" - -test-epilogue -`))) - }) -} diff --git a/carton/mocks/entry_writer.go b/carton/mocks/entry_writer.go deleted file mode 100644 index d4cd4cb..0000000 --- a/carton/mocks/entry_writer.go +++ /dev/null @@ -1,38 +0,0 @@ -// Code generated by mockery v2.30.1. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// EntryWriter is an autogenerated mock type for the EntryWriter type -type EntryWriter struct { - mock.Mock -} - -// Write provides a mock function with given fields: source, destination -func (_m *EntryWriter) Write(source string, destination string) error { - ret := _m.Called(source, destination) - - var r0 error - if rf, ok := ret.Get(0).(func(string, string) error); ok { - r0 = rf(source, destination) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// NewEntryWriter creates a new instance of EntryWriter. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func NewEntryWriter(t interface { - mock.TestingT - Cleanup(func()) -}) *EntryWriter { - mock := &EntryWriter{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/carton/netrc.go b/carton/netrc.go deleted file mode 100644 index 8a1e79e..0000000 --- a/carton/netrc.go +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton - -import ( - "fmt" - "net/http" - "os" - "os/user" - "path/filepath" - "strings" -) - -type Netrc []NetrcLine - -type NetrcLine struct { - Machine string - Login string - Password string -} - -func (n Netrc) BasicAuth(request *http.Request) (*http.Request, error) { - for _, l := range n { - if l.Machine != request.Host && l.Machine != "default" { - continue - } - - request.SetBasicAuth(l.Login, l.Password) - break - } - - return request, nil -} - -func ParseNetrc(path string) (Netrc, error) { - b, err := os.ReadFile(path) - if os.IsNotExist(err) { - return nil, nil - } else if err != nil { - return nil, fmt.Errorf("unable to open %s\n%w", path, err) - } - - var ( - n Netrc - l NetrcLine - m = false - ) - - for _, line := range strings.Split(string(b), "\n") { - if m { - if line == "" { - m = false - } - continue - } - - f := strings.Fields(line) - for i := 0; i < len(f); { - switch f[i] { - case "machine": - l = NetrcLine{Machine: f[i+1]} - i += 2 - case "default": - l = NetrcLine{Machine: "default"} - i++ - case "login": - l.Login = f[i+1] - i += 2 - case "password": - l.Password = f[i+1] - i += 2 - case "macdef": - m = true - i += 2 - } - - if l.Machine != "" && l.Login != "" && l.Password != "" { - n = append(n, l) - - if l.Machine == "default" { - return n, nil - } - - l = NetrcLine{} - } - } - } - - return n, nil -} - -func NetrcPath() (string, error) { - if s, ok := os.LookupEnv("NETRC"); ok { - return s, nil - } - - u, err := user.Current() - if err != nil { - return "", fmt.Errorf("unable to determine user home directory\n%w", err) - } - - return filepath.Join(u.HomeDir, ".netrc"), nil -} diff --git a/carton/netrc_test.go b/carton/netrc_test.go deleted file mode 100644 index ddfd1af..0000000 --- a/carton/netrc_test.go +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton_test - -import ( - "net/http" - "os" - "os/user" - "path/filepath" - "testing" - - . "github.com/onsi/gomega" - "github.com/sclevine/spec" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func testNetrc(t *testing.T, context spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - path string - ) - - it.Before(func() { - var err error - - f, err := os.CreateTemp("", "netrc") - Expect(err).NotTo(HaveOccurred()) - Expect(f.Close()).To(Succeed()) - path = f.Name() - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) - - context("path", func() { - context("$NETRC", func() { - it.Before(func() { - Expect(os.Setenv("NETRC", "test-value")).To(Succeed()) - }) - - it.After(func() { - Expect(os.Unsetenv("NETRC")).To(Succeed()) - }) - - it("returns value from env var", func() { - Expect(carton.NetrcPath()).To(Equal("test-value")) - }) - }) - - it("returns default", func() { - u, err := user.Current() - Expect(err).NotTo(HaveOccurred()) - - Expect(carton.NetrcPath()).To(Equal(filepath.Join(u.HomeDir, ".netrc"))) - }) - }) - - context("parse", func() { - it("parses one-liner", func() { - Expect(os.WriteFile(path, []byte(`machine test-machine login test-login password test-password`), 0600)).To(Succeed()) - - Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ - { - Machine: "test-machine", - Login: "test-login", - Password: "test-password", - }, - })) - }) - - it("parses multi-liner", func() { - Expect(os.WriteFile(path, []byte(` -machine test-machine -login test-login -password test-password -`), 0600)).To(Succeed()) - - Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ - { - Machine: "test-machine", - Login: "test-login", - Password: "test-password", - }, - })) - }) - - it("ignores macdef", func() { - Expect(os.WriteFile(path, []byte(` -macdef uploadtest - cd /pub/tests - bin - put filename.tar.gz - quit - -machine test-machine login test-login password test-password -`), 0600)).To(Succeed()) - - Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ - { - Machine: "test-machine", - Login: "test-login", - Password: "test-password", - }, - })) - }) - - it("ignores all after default", func() { - Expect(os.WriteFile(path, []byte(` -machine test-machine-1 login test-login-1 password test-password-1 - -default -login test-login-2 -password test-password-2 - -machine test-machine-3 login test-login-3 password test-password-3 -`), 0600)).To(Succeed()) - - Expect(carton.ParseNetrc(path)).To(Equal(carton.Netrc{ - { - Machine: "test-machine-1", - Login: "test-login-1", - Password: "test-password-1", - }, - { - Machine: "default", - Login: "test-login-2", - Password: "test-password-2", - }, - })) - }) - }) - - context("basic auth", func() { - it("does not apply auth if no candidates", func() { - n := carton.Netrc{ - { - Machine: "test-machine", - Login: "test-login", - Password: "test-password", - }, - } - - req, err := http.NewRequest("GET", "http://another-machine", nil) - Expect(err).NotTo(HaveOccurred()) - - req, err = n.BasicAuth(req) - Expect(err).NotTo(HaveOccurred()) - - _, _, ok := req.BasicAuth() - Expect(ok).To(BeFalse()) - }) - - it("applies basic auth for match", func() { - n := carton.Netrc{ - { - Machine: "test-machine", - Login: "test-login", - Password: "test-password", - }, - } - - req, err := http.NewRequest("GET", "http://test-machine", nil) - Expect(err).NotTo(HaveOccurred()) - - req, err = n.BasicAuth(req) - Expect(err).NotTo(HaveOccurred()) - - u, p, ok := req.BasicAuth() - Expect(ok).To(BeTrue()) - Expect(u).To(Equal("test-login")) - Expect(p).To(Equal("test-password")) - }) - - it("applies basic auth for default", func() { - n := carton.Netrc{ - { - Machine: "test-machine", - Login: "test-login", - Password: "test-password", - }, - { - Machine: "default", - Login: "default-login", - Password: "default-password", - }, - } - - req, err := http.NewRequest("GET", "http://another-machine", nil) - Expect(err).NotTo(HaveOccurred()) - - req, err = n.BasicAuth(req) - Expect(err).NotTo(HaveOccurred()) - - u, p, ok := req.BasicAuth() - Expect(ok).To(BeTrue()) - Expect(u).To(Equal("default-login")) - Expect(p).To(Equal("default-password")) - }) - }) -} diff --git a/carton/package.go b/carton/package.go deleted file mode 100644 index 56d7e4f..0000000 --- a/carton/package.go +++ /dev/null @@ -1,276 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton - -import ( - "fmt" - "os" - "path/filepath" - "regexp" - "sort" - "text/template" - - "github.com/BurntSushi/toml" - "github.com/buildpacks/libcnb/v2" - "github.com/heroku/color" - - "github.com/paketo-buildpacks/libpak/v2" - "github.com/paketo-buildpacks/libpak/v2/effect" - "github.com/paketo-buildpacks/libpak/v2/internal" - "github.com/paketo-buildpacks/libpak/v2/log" -) - -// Package is an object that contains the configuration for building a package. -type Package struct { - - // CacheLocation is the location to cache downloaded dependencies. - CacheLocation string - - // DependencyFilters indicates which filters should be applied to exclude dependencies - DependencyFilters []string - - // StrictDependencyFilters indicates that a filter must match both the ID and version, otherwise it must only match one of the two - StrictDependencyFilters bool - - // IncludeDependencies indicates whether to include dependencies in build package. - IncludeDependencies bool - - // Destination is the directory to create the build package in. - Destination string - - // Source is the source directory of the buildpack. - Source string - - // Version is a version to substitute into an existing buildpack.toml. - Version string -} - -// Create creates a package. -func (p Package) Create(options ...Option) { - config := Config{ - entryWriter: internal.EntryWriter{}, - executor: effect.NewExecutor(), - exitHandler: internal.NewExitHandler(), - } - - for _, option := range options { - config = option(config) - } - - var ( - err error - file string - ) - - logger := log.NewPaketoLogger(os.Stdout) - - // Is this a buildpack or an extension? - bpfile := filepath.Join(p.Source, "buildpack.toml") - extnfile := filepath.Join(p.Source, "extension.toml") - var metadataMap map[string]interface{} - var id string - var name string - var version string - var homepage string - extension := false - if _, err := os.Stat(bpfile); err == nil { - s, err := os.ReadFile(bpfile) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to read buildpack.toml %s\n%w", bpfile, err)) - return - } - var b libcnb.Buildpack - if err := toml.Unmarshal(s, &b); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to decode %s\n%w", bpfile, err)) - return - } - metadataMap = b.Metadata - id = b.Info.ID - name = b.Info.Name - version = b.Info.Version - homepage = b.Info.Homepage - logger.Debugf("Buildpack: %+v", b) - } else if _, err := os.Stat(extnfile); err == nil { - s, err := os.ReadFile(extnfile) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to read extension.toml %s\n%w", extnfile, err)) - return - } - var e libcnb.Extension - if err := toml.Unmarshal(s, &e); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to decode %s\n%w", extnfile, err)) - return - } - metadataMap = e.Metadata - id = e.Info.ID - name = e.Info.Name - version = e.Info.Version - homepage = e.Info.Homepage - extension = true - logger.Debugf("Extension: %+v", e) - } else { - config.exitHandler.Error(fmt.Errorf("unable to read buildpack/extension.toml at %s", p.Source)) - return - } - - metadata, err := libpak.NewBuildModuleMetadata(metadataMap) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to decode metadata %s\n%w", metadataMap, err)) - return - } - - entries := map[string]string{} - - for _, i := range metadata.IncludeFiles { - entries[i] = filepath.Join(p.Source, i) - } - logger.Debugf("Include files: %+v", entries) - - if p.Version != "" { - version = p.Version - - tomlName := "" - if extension { - tomlName = "extension" - } else { - tomlName = "buildpack" - } - - file = filepath.Join(p.Source, tomlName+".toml") - t, err := template.ParseFiles(file) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to parse template %s\n%w", file, err)) - return - } - - out, err := os.CreateTemp("", tomlName+"-*.toml") - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to open temporary "+tomlName+".toml file\n%w", err)) - } - defer out.Close() - - if err = t.Execute(out, map[string]interface{}{"version": p.Version}); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to execute template %s with version %s\n%w", file, p.Version, err)) - return - } - - entries[tomlName+".toml"] = out.Name() - } - - logger.Title(name, version, homepage) - logger.Headerf("Creating package in %s", p.Destination) - - if err = os.RemoveAll(p.Destination); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to remove destination path %s\n%w", p.Destination, err)) - return - } - - file = metadata.PrePackage - if file != "" { - logger.Headerf("Pre-package with %s", file) - execution := effect.Execution{ - Command: file, - Dir: p.Source, - Stdout: logger.BodyWriter(), - Stderr: logger.BodyWriter(), - } - - if err = config.executor.Execute(execution); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to execute pre-package script %s\n%w", file, err)) - } - } - - if p.IncludeDependencies { - cache := libpak.DependencyCache{ - Logger: logger, - UserAgent: fmt.Sprintf("%s/%s", id, version), - } - - if p.CacheLocation != "" { - cache.DownloadPath = p.CacheLocation - } else { - cache.DownloadPath = filepath.Join(p.Source, "dependencies") - } - - np, err := NetrcPath() - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to determine netrc path\n%w", err)) - return - } - - n, err := ParseNetrc(np) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to read %s as netrc\n%w", np, err)) - return - } - - for _, dep := range metadata.Dependencies { - if !p.matchDependency(dep) { - logger.Bodyf("Skipping [%s or %s] which matched a filter", dep.ID, dep.Version) - continue - } - - logger.Headerf("Caching %s", color.BlueString("%s %s", dep.Name, dep.Version)) - - f, err := cache.Artifact(dep, n.BasicAuth) - if err != nil { - config.exitHandler.Error(fmt.Errorf("unable to download %s\n%w", dep.URI, err)) - return - } - if err = f.Close(); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to close %s\n%w", f.Name(), err)) - return - } - - entries[fmt.Sprintf("dependencies/%s/%s", dep.SHA256, filepath.Base(f.Name()))] = f.Name() - entries[fmt.Sprintf("dependencies/%s.toml", dep.SHA256)] = fmt.Sprintf("%s.toml", filepath.Dir(f.Name())) - } - } - - var files []string - for d := range entries { - files = append(files, d) - } - sort.Strings(files) - for _, d := range files { - logger.Bodyf("Adding %s", d) - file = filepath.Join(p.Destination, d) - 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 - } - } -} - -// matchDependency checks all filters against dependency and returns true if there is a match (or no filters) and false if there is no match -// There is a match if a regular expression matches against the ID or Version -func (p Package) matchDependency(dep libpak.BuildModuleDependency) bool { - if len(p.DependencyFilters) == 0 { - return true - } - - for _, rawFilter := range p.DependencyFilters { - filter := regexp.MustCompile(rawFilter) - - if (p.StrictDependencyFilters && filter.MatchString(dep.ID) && filter.MatchString(dep.Version)) || - (!p.StrictDependencyFilters && (filter.MatchString(dep.ID) || filter.MatchString(dep.Version))) { - return true - } - } - - return false -} diff --git a/carton/package_dependency.go b/carton/package_dependency.go deleted file mode 100644 index e109457..0000000 --- a/carton/package_dependency.go +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton - -import ( - "bytes" - "fmt" - "os" - "strings" - - "github.com/BurntSushi/toml" - - "github.com/paketo-buildpacks/libpak/v2/internal" - "github.com/paketo-buildpacks/libpak/v2/log" -) - -type PackageDependency struct { - BuilderPath string - BuildpackPath string - ID string - Version string - PackagePath string -} - -func (p PackageDependency) Update(options ...Option) { - config := Config{ - exitHandler: internal.NewExitHandler(), - } - - for _, option := range options { - config = option(config) - } - - logger := log.NewPaketoLogger(os.Stdout) - _, _ = fmt.Fprintf(logger.TitleWriter(), "\n%s\n", log.FormatIdentity(p.ID, p.Version)) - - if p.BuilderPath != "" { - if err := updateFile(p.BuilderPath, updateByKey("buildpacks", p.ID, p.Version)); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to update %s\n%w", p.BuilderPath, err)) - } - } - - if p.PackagePath != "" { - if err := updateFile(p.PackagePath, updateByKey("dependencies", p.ID, p.Version)); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to update %s\n%w", p.PackagePath, err)) - } - } - - // Do we have a buildpack.toml with an order element? (composite buildpack) - if p.BuildpackPath != "" { - if err := updateFile(p.BuildpackPath, func(md map[string]interface{}) { - parts := strings.Split(p.ID, "/") - id := strings.Join(parts[len(parts)-2:], "/") - - groupsUnwrapped, found := md["order"] - if !found { - return - } - - groups, ok := groupsUnwrapped.([]map[string]interface{}) - if !ok { - return - } - - for _, group := range groups { - buildpacksUnwrapped, found := group["group"] - if !found { - continue - } - - buildpacks, ok := buildpacksUnwrapped.([]interface{}) - if !ok { - continue - } - - for _, bpw := range buildpacks { - bp, ok := bpw.(map[string]interface{}) - if !ok { - continue - } - - bpIDUnwrappd, found := bp["id"] - if !found { - continue - } - - bpID, ok := bpIDUnwrappd.(string) - if !ok { - continue - } - - if bpID == id { - bp["version"] = p.Version - } - } - } - }); err != nil { - config.exitHandler.Error(fmt.Errorf("unable to update %s\n%w", p.BuildpackPath, err)) - } - } -} - -func updateByKey(key, id, version string) func(md map[string]interface{}) { - return func(md map[string]interface{}) { - valuesUnwrapped, found := md[key] - if !found { - return - } - - values, ok := valuesUnwrapped.([]interface{}) - if !ok { - return - } - - for _, bpw := range values { - bp, ok := bpw.(map[string]interface{}) - if !ok { - continue - } - - uriUnwrapped, found := bp["uri"] - if !found { - continue - } - - uri, ok := uriUnwrapped.(string) - if !ok { - continue - } - - if strings.HasPrefix(uri, fmt.Sprintf("docker://%s", id)) { - parts := strings.Split(uri, ":") - bp["uri"] = fmt.Sprintf("%s:%s", strings.Join(parts[0:2], ":"), version) - } - } - } -} - -func updateFile(cfgPath string, f func(md map[string]interface{})) error { - c, err := os.ReadFile(cfgPath) - if err != nil { - return fmt.Errorf("unable to read %s\n%w", cfgPath, err) - } - - // save any leading comments, this is to preserve license headers - // inline comments will be lost - comments := []byte{} - for i, line := range bytes.SplitAfter(c, []byte("\n")) { - if bytes.HasPrefix(line, []byte("#")) || (i > 0 && len(bytes.TrimSpace(line)) == 0) { - comments = append(comments, line...) - } else { - break // stop on first comment - } - } - - md := make(map[string]interface{}) - if err := toml.Unmarshal(c, &md); err != nil { - return fmt.Errorf("unable to decode md %s\n%w", cfgPath, err) - } - - f(md) - - b, err := internal.Marshal(md) - if err != nil { - return fmt.Errorf("unable to encode md %s\n%w", cfgPath, err) - } - - b = append(comments, b...) - - // #nosec G306 - permissions need to be 644 on the package - if err := os.WriteFile(cfgPath, b, 0644); err != nil { - return fmt.Errorf("unable to write %s\n%w", cfgPath, err) - } - - return nil -} diff --git a/carton/package_dependency_test.go b/carton/package_dependency_test.go deleted file mode 100644 index b9627c8..0000000 --- a/carton/package_dependency_test.go +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton_test - -import ( - "os" - "testing" - - "github.com/buildpacks/libcnb/v2/mocks" - . "github.com/onsi/gomega" - "github.com/sclevine/spec" - "github.com/stretchr/testify/mock" - - "github.com/paketo-buildpacks/libpak/v2/carton" - "github.com/paketo-buildpacks/libpak/v2/internal" -) - -func testPackageDependency(t *testing.T, _ spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - exitHandler *mocks.ExitHandler - path string - ) - - it.Before(func() { - var err error - - exitHandler = &mocks.ExitHandler{} - exitHandler.On("Error", mock.Anything) - - f, err := os.CreateTemp("", "carton-package-dependency") - Expect(err).NotTo(HaveOccurred()) - Expect(f.Close()).To(Succeed()) - path = f.Name() - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) - - it("updates paketo-buildpacks dependency without losing other fields", func() { - Expect(os.WriteFile(path, []byte(`# it should preserve -# these comments -# exactly - -api = "0.6" -[buildpack] -id = "some-id" -name = "some-name" - -[[order]] -group = [ - { id = "paketo-buildpacks/test-1", version="test-version-1" }, - { id = "paketo-buildpacks/test-2", version="test-version-2" }, -] -[metadata] -include-files = [ - "LICENSE", - "README.md", - "buildpack.toml", -]`), 0600)).To(Succeed()) - - p := carton.PackageDependency{ - BuildpackPath: path, - ID: "gcr.io/paketo-buildpacks/test-1", - Version: "test-version-3", - } - - p.Update(carton.WithExitHandler(exitHandler)) - - body, err := os.ReadFile(path) - Expect(err).NotTo(HaveOccurred()) - Expect(string(body)).To(HavePrefix(`# it should preserve -# these comments -# exactly - -api = "0.6"`)) - - Expect(body).To(internal.MatchTOML(`api = "0.6" -[buildpack] -id = "some-id" -name = "some-name" - -[[order]] -[[order.group]] - id = "paketo-buildpacks/test-1" - version="test-version-3" -[[order.group]] - id = "paketo-buildpacks/test-2" - version="test-version-2" -[metadata] -include-files = ["LICENSE","README.md","buildpack.toml"] -`)) - }) - - it("updates paketo-buildpacks dependency id partial id", func() { - Expect(os.WriteFile(path, []byte(` -[[order]] -group = [ - { id = "paketo-buildpacks/test-1", version="test-version-1" }, - { id = "paketo-buildpacks/test-2", version="test-version-2" }, -]`), 0600)).To(Succeed()) - - p := carton.PackageDependency{ - BuildpackPath: path, - ID: "paketo-buildpacks/test-1", - Version: "test-version-3", - } - - p.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[order]] - [[order.group]] - id = "paketo-buildpacks/test-1" - version="test-version-3" - [[order.group]] - id = "paketo-buildpacks/test-2" - version="test-version-2"`)) - }) - - it("updates paketocommunity dependency", func() { - Expect(os.WriteFile(path, []byte(`[[order]] -group = [ - { id = "paketocommunity/test-1", version="test-version-1" }, - { id = "paketocommunity/test-2", version="test-version-2" }, -]`), 0600)).To(Succeed()) - - p := carton.PackageDependency{ - BuildpackPath: path, - ID: "docker.io/paketocommunity/test-1", - Version: "test-version-3", - } - - p.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[order]] - [[order.group]] - id = "paketocommunity/test-1" - version="test-version-3" - [[order.group]] - id = "paketocommunity/test-2" - version="test-version-2"`)) - }) - - it("updates builder dependency", func() { - Expect(os.WriteFile(path, []byte(`buildpacks = [ - { id = "paketo-buildpacks/test-1", uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-1" }, - { id = "paketo-buildpacks/test-2", uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2" }, -]`), 0600)).To(Succeed()) - - p := carton.PackageDependency{ - BuilderPath: path, - ID: "gcr.io/paketo-buildpacks/test-1", - Version: "test-version-3", - } - - p.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[buildpacks]] - id = "paketo-buildpacks/test-1" - uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-3" - - [[buildpacks]] - id = "paketo-buildpacks/test-2" - uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2"`)) - }) - - it("updates paketo-buildpacks package dependency", func() { - Expect(os.WriteFile(path, []byte(`dependencies = [ - { uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-1" }, - { uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2" }, -]`), 0600)).To(Succeed()) - - p := carton.PackageDependency{ - PackagePath: path, - ID: "gcr.io/paketo-buildpacks/test-1", - Version: "test-version-3", - } - - p.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[dependencies]] - uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-3" - - [[dependencies]] - uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2"`)) - }) - - it("updates paketocommunity package dependency", func() { - Expect(os.WriteFile(path, []byte(`dependencies = [ - { uri = "docker://docker.io/paketocommunity/test-1:test-version-1" }, - { uri = "docker://docker.io/paketocommunity/test-2:test-version-2" }, -]`), 0600)).To(Succeed()) - - p := carton.PackageDependency{ - PackagePath: path, - ID: "docker.io/paketocommunity/test-1", - Version: "test-version-3", - } - - p.Update(carton.WithExitHandler(exitHandler)) - - Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[dependencies]] - uri = "docker://docker.io/paketocommunity/test-1:test-version-3" - - [[dependencies]] - uri = "docker://docker.io/paketocommunity/test-2:test-version-2"`)) - }) -} diff --git a/carton/package_test.go b/carton/package_test.go deleted file mode 100644 index c4a6bff..0000000 --- a/carton/package_test.go +++ /dev/null @@ -1,594 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package carton_test - -import ( - "os" - "path/filepath" - "testing" - - "github.com/buildpacks/libcnb/v2/mocks" - . "github.com/onsi/gomega" - "github.com/sclevine/spec" - "github.com/stretchr/testify/mock" - - "github.com/paketo-buildpacks/libpak/v2/carton" - cMocks "github.com/paketo-buildpacks/libpak/v2/carton/mocks" - "github.com/paketo-buildpacks/libpak/v2/effect" - eMocks "github.com/paketo-buildpacks/libpak/v2/effect/mocks" -) - -func testPackage(t *testing.T, context spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - entryWriter *cMocks.EntryWriter - executor *eMocks.Executor - exitHandler *mocks.ExitHandler - path string - ) - - context("with buildpack.toml", func() { - it.Before(func() { - entryWriter = &cMocks.EntryWriter{} - entryWriter.On("Write", mock.Anything, mock.Anything).Return(nil) - - executor = &eMocks.Executor{} - executor.On("Execute", mock.Anything).Return(nil) - - exitHandler = &mocks.ExitHandler{} - exitHandler.On("Error", mock.Anything) - - path = t.TempDir() - - Expect(os.WriteFile(filepath.Join(path, "buildpack.toml"), []byte(` -api = "0.0.0" - -[buildpack] -name = "test-name" -version = "{{.version}}" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri" -sha256 = "test-sha256" -stacks = [ "test-stack" ] - - [[metadata.dependencies.licenses]] - type = "test-type" - uri = "test-uri" - -[metadata] -pre-package = "test-pre-package" -include-files = [ - "test-include-files", - "buildpack.toml", -] -`), 0600)).To(Succeed()) - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) - - it("executes pre_package script", func() { - carton.Package{ - Source: path, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - e, ok := executor.Calls[0].Arguments[0].(effect.Execution) - Expect(ok).To(BeTrue()) - Expect(e.Command).To(Equal("test-pre-package")) - Expect(e.Dir).To(Equal(path)) - }) - - it("includes include_files", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "buildpack.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("replaces .version in buildpack.toml", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - Version: "2.2.2", - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls[0].Arguments[0]).NotTo(Equal(filepath.Join(path, "buildpack.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - - Expect(os.ReadFile(entryWriter.Calls[0].Arguments[0].(string))).To(Equal([]byte(` -api = "0.0.0" - -[buildpack] -name = "test-name" -version = "2.2.2" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri" -sha256 = "test-sha256" -stacks = [ "test-stack" ] - - [[metadata.dependencies.licenses]] - type = "test-type" - uri = "test-uri" - -[metadata] -pre-package = "test-pre-package" -include-files = [ - "test-include-files", - "buildpack.toml", -] -`))) - }) - - context("includes dependencies", func() { - it.Before(func() { - Expect(os.WriteFile(filepath.Join(path, "buildpack.toml"), []byte(` -api = "0.0.0" - -[buildpack] -name = "test-name" -version = "{{.version}}" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri-1" -sha256 = "test-sha256-1" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "2.0.5" -uri = "test-uri-2" -sha256 = "test-sha256-2" - -[[metadata.dependencies]] -id = "another-test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri-3" -sha256 = "test-sha256-3" - -[metadata] -pre-package = "test-pre-package" -include-files = [ - "test-include-files", - "buildpack.toml", -] -`), 0600)).To(Succeed()) - }) - - it("includes all dependencies", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(8)) - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "buildpack.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) - - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-1.toml")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1.toml"))) - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal("testdata/test-sha256-1/test-uri-1")) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1/test-uri-1"))) - - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal("testdata/test-sha256-2.toml")) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2.toml"))) - Expect(entryWriter.Calls[4].Arguments[0]).To(Equal("testdata/test-sha256-2/test-uri-2")) - Expect(entryWriter.Calls[4].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2/test-uri-2"))) - - Expect(entryWriter.Calls[5].Arguments[0]).To(Equal("testdata/test-sha256-3.toml")) - Expect(entryWriter.Calls[5].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3.toml"))) - Expect(entryWriter.Calls[6].Arguments[0]).To(Equal("testdata/test-sha256-3/test-uri-3")) - Expect(entryWriter.Calls[6].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3/test-uri-3"))) - - Expect(entryWriter.Calls[7].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[7].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("includes filter by id", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - DependencyFilters: []string{`^another-test-id$`}, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(4)) - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "buildpack.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) - - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-3.toml")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3.toml"))) - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal("testdata/test-sha256-3/test-uri-3")) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3/test-uri-3"))) - - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("includes filter by version", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - DependencyFilters: []string{`^1.1.1$`}, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(6)) - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "buildpack.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) - - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-1.toml")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1.toml"))) - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal("testdata/test-sha256-1/test-uri-1")) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1/test-uri-1"))) - - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal("testdata/test-sha256-3.toml")) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3.toml"))) - Expect(entryWriter.Calls[4].Arguments[0]).To(Equal("testdata/test-sha256-3/test-uri-3")) - Expect(entryWriter.Calls[4].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3/test-uri-3"))) - - Expect(entryWriter.Calls[5].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[5].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("includes filter by version and id", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - DependencyFilters: []string{`^test-id$|^2\.0\.5$`}, - StrictDependencyFilters: true, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(4)) - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "buildpack.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) - - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-2.toml")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2.toml"))) - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal("testdata/test-sha256-2/test-uri-2")) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2/test-uri-2"))) - - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - }) - }) - - context("with extension.toml", func() { - it.Before(func() { - entryWriter = &cMocks.EntryWriter{} - entryWriter.On("Write", mock.Anything, mock.Anything).Return(nil) - - executor = &eMocks.Executor{} - executor.On("Execute", mock.Anything).Return(nil) - - exitHandler = &mocks.ExitHandler{} - exitHandler.On("Error", mock.Anything) - - path = t.TempDir() - - Expect(os.WriteFile(filepath.Join(path, "extension.toml"), []byte(` -api = "0.0.0" - -[extension] -name = "test-name" -version = "{{.version}}" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri" -sha256 = "test-sha256" -stacks = [ "test-stack" ] - - [[metadata.dependencies.licenses]] - type = "test-type" - uri = "test-uri" - -[metadata] -pre-package = "test-pre-package" -include-files = [ - "test-include-files", - "extension.toml", -] -`), 0600)).To(Succeed()) - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) - - it("executes pre_package script", func() { - carton.Package{ - Source: path, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - e, ok := executor.Calls[0].Arguments[0].(effect.Execution) - Expect(ok).To(BeTrue()) - Expect(e.Command).To(Equal("test-pre-package")) - Expect(e.Dir).To(Equal(path)) - }) - - it("includes include_files", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "extension.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "extension.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("replaces .version in extension.toml", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - Version: "2.2.2", - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls[0].Arguments[0]).NotTo(Equal(filepath.Join(path, "extension.toml"))) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "extension.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - - Expect(os.ReadFile(entryWriter.Calls[0].Arguments[0].(string))).To(Equal([]byte(` -api = "0.0.0" - -[extension] -name = "test-name" -version = "2.2.2" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri" -sha256 = "test-sha256" -stacks = [ "test-stack" ] - - [[metadata.dependencies.licenses]] - type = "test-type" - uri = "test-uri" - -[metadata] -pre-package = "test-pre-package" -include-files = [ - "test-include-files", - "extension.toml", -] -`))) - }) - - context("includes dependencies", func() { - it.Before(func() { - Expect(os.WriteFile(filepath.Join(path, "extension.toml"), []byte(` -api = "0.0.0" - -[extension] -name = "test-name" -version = "{{.version}}" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri-1" -sha256 = "test-sha256-1" - -[[metadata.dependencies]] -id = "test-id" -name = "test-name" -version = "2.0.5" -uri = "test-uri-2" -sha256 = "test-sha256-2" - -[[metadata.dependencies]] -id = "another-test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri-3" -sha256 = "test-sha256-3" - -[metadata] -pre-package = "test-pre-package" -include-files = [ - "test-include-files", - "extension.toml", -] -`), 0600)).To(Succeed()) - }) - - it("includes all dependencies", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(8)) - - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal("testdata/test-sha256-1.toml")) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-1/test-uri-1")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1/test-uri-1"))) - - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal("testdata/test-sha256-2.toml")) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2.toml"))) - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal("testdata/test-sha256-2/test-uri-2")) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2/test-uri-2"))) - - Expect(entryWriter.Calls[4].Arguments[0]).To(Equal("testdata/test-sha256-3.toml")) - Expect(entryWriter.Calls[4].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3.toml"))) - Expect(entryWriter.Calls[5].Arguments[0]).To(Equal("testdata/test-sha256-3/test-uri-3")) - Expect(entryWriter.Calls[5].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3/test-uri-3"))) - - Expect(entryWriter.Calls[6].Arguments[0]).To(Equal(filepath.Join(path, "extension.toml"))) - Expect(entryWriter.Calls[6].Arguments[1]).To(Equal(filepath.Join("test-destination", "extension.toml"))) - - Expect(entryWriter.Calls[7].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[7].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("includes filter by id", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - DependencyFilters: []string{`^another-test-id$`}, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(4)) - - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal("testdata/test-sha256-3.toml")) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-3/test-uri-3")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3/test-uri-3"))) - - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal(filepath.Join(path, "extension.toml"))) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "extension.toml"))) - - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("includes filter by version", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - DependencyFilters: []string{`^1.1.1$`}, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(6)) - - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal("testdata/test-sha256-1.toml")) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-1/test-uri-1")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-1/test-uri-1"))) - - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal("testdata/test-sha256-3.toml")) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3.toml"))) - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal("testdata/test-sha256-3/test-uri-3")) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-3/test-uri-3"))) - - Expect(entryWriter.Calls[4].Arguments[0]).To(Equal(filepath.Join(path, "extension.toml"))) - Expect(entryWriter.Calls[4].Arguments[1]).To(Equal(filepath.Join("test-destination", "extension.toml"))) - - Expect(entryWriter.Calls[5].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[5].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - - it("includes filter by version and id", func() { - carton.Package{ - Source: path, - Destination: "test-destination", - IncludeDependencies: true, - CacheLocation: "testdata", - DependencyFilters: []string{`^test-id$|^2\.0\.5$`}, - StrictDependencyFilters: true, - }.Create( - carton.WithEntryWriter(entryWriter), - carton.WithExecutor(executor), - carton.WithExitHandler(exitHandler)) - - Expect(entryWriter.Calls).To(HaveLen(4)) - - Expect(entryWriter.Calls[0].Arguments[0]).To(Equal("testdata/test-sha256-2.toml")) - Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2.toml"))) - Expect(entryWriter.Calls[1].Arguments[0]).To(Equal("testdata/test-sha256-2/test-uri-2")) - Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "dependencies/test-sha256-2/test-uri-2"))) - - Expect(entryWriter.Calls[2].Arguments[0]).To(Equal(filepath.Join(path, "extension.toml"))) - Expect(entryWriter.Calls[2].Arguments[1]).To(Equal(filepath.Join("test-destination", "extension.toml"))) - - Expect(entryWriter.Calls[3].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) - Expect(entryWriter.Calls[3].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) - }) - }) - }) -} diff --git a/carton/testdata/test-sha256-1.toml b/carton/testdata/test-sha256-1.toml deleted file mode 100644 index add68cc..0000000 --- a/carton/testdata/test-sha256-1.toml +++ /dev/null @@ -1,5 +0,0 @@ -id = "test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri-1" -sha256 = "test-sha256-1" \ No newline at end of file diff --git a/carton/testdata/test-sha256-1/test-uri-1 b/carton/testdata/test-sha256-1/test-uri-1 deleted file mode 100644 index e69de29..0000000 diff --git a/carton/testdata/test-sha256-2.toml b/carton/testdata/test-sha256-2.toml deleted file mode 100644 index 9c96f6c..0000000 --- a/carton/testdata/test-sha256-2.toml +++ /dev/null @@ -1,5 +0,0 @@ -id = "test-id" -name = "test-name" -version = "2.0.5" -uri = "test-uri-2" -sha256 = "test-sha256-2" \ No newline at end of file diff --git a/carton/testdata/test-sha256-2/test-uri-2 b/carton/testdata/test-sha256-2/test-uri-2 deleted file mode 100644 index e69de29..0000000 diff --git a/carton/testdata/test-sha256-3.toml b/carton/testdata/test-sha256-3.toml deleted file mode 100644 index 5d91b64..0000000 --- a/carton/testdata/test-sha256-3.toml +++ /dev/null @@ -1,5 +0,0 @@ -id = "another-test-id" -name = "test-name" -version = "1.1.1" -uri = "test-uri-3" -sha256 = "test-sha256-3" \ No newline at end of file diff --git a/carton/testdata/test-sha256-3/test-uri-3 b/carton/testdata/test-sha256-3/test-uri-3 deleted file mode 100644 index e69de29..0000000 diff --git a/cmd/create-package/main.go b/cmd/create-package/main.go deleted file mode 100644 index 3d75d55..0000000 --- a/cmd/create-package/main.go +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "fmt" - "log" - "os" - - "github.com/spf13/pflag" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func main() { - p := carton.Package{} - - flagSet := pflag.NewFlagSet("Create Package", pflag.ExitOnError) - flagSet.StringVar(&p.CacheLocation, "cache-location", "", "path to cache downloaded dependencies (default: $PWD/dependencies)") - flagSet.StringVar(&p.Destination, "destination", "", "path to the build package destination directory") - flagSet.BoolVar(&p.IncludeDependencies, "include-dependencies", false, "whether to include dependencies (default: false)") - flagSet.StringSliceVar(&p.DependencyFilters, "dependency-filter", []string{}, "one or more filters that are applied to exclude dependencies") - 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/extension.toml") - - if err := flagSet.Parse(os.Args[1:]); err != nil { - log.Fatal(fmt.Errorf("unable to parse flags\n%w", err)) - } - - if p.Destination == "" { - log.Fatal("destination must be set") - } - - p.Create() -} - -func defaultSource() string { - s, err := os.Getwd() - if err != nil { - log.Fatal(fmt.Errorf("unable to get working directory\n%w", err)) - } - - return s -} diff --git a/cmd/update-build-image-dependency/main.go b/cmd/update-build-image-dependency/main.go deleted file mode 100644 index 32c7317..0000000 --- a/cmd/update-build-image-dependency/main.go +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "fmt" - "log" - "os" - - "github.com/spf13/pflag" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func main() { - i := carton.BuildImageDependency{} - - flagSet := pflag.NewFlagSet("Update Image Dependency", pflag.ExitOnError) - flagSet.StringVar(&i.BuilderPath, "builder-toml", "", "path to builder.toml") - flagSet.StringVar(&i.Version, "version", "", "the new version of the dependency") - - if err := flagSet.Parse(os.Args[1:]); err != nil { - log.Fatal(fmt.Errorf("unable to parse flags\n%w", err)) - } - - if i.BuilderPath == "" { - log.Fatal("builder-toml must be set") - } - - if i.Version == "" { - log.Fatal("version must be set") - } - - i.Update() -} diff --git a/cmd/update-buildmodule-dependency/main.go b/cmd/update-buildmodule-dependency/main.go deleted file mode 100644 index ebe08f2..0000000 --- a/cmd/update-buildmodule-dependency/main.go +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "fmt" - "log" - "os" - - "github.com/spf13/pflag" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func main() { - b := carton.BuildModuleDependency{} - - flagSet := pflag.NewFlagSet("Update Build Module Dependency", pflag.ExitOnError) - flagSet.StringVar(&b.BuildModulePath, "buildmodule-toml", "", "path to buildpack.toml or extension.toml") - flagSet.StringVar(&b.ID, "id", "", "the id of the dependency") - flagSet.StringVar(&b.SHA256, "sha256", "", "the new sha256 of the dependency") - flagSet.StringVar(&b.URI, "uri", "", "the new uri of the dependency") - flagSet.StringVar(&b.Version, "version", "", "the new version of the dependency") - flagSet.StringVar(&b.VersionPattern, "version-pattern", "", "the version pattern of the dependency") - flagSet.StringVar(&b.PURL, "purl", "", "the new purl version of the dependency, if not set defaults to version") - flagSet.StringVar(&b.PURLPattern, "purl-pattern", "", "the purl version pattern of the dependency, if not set defaults to version-pattern") - flagSet.StringVar(&b.CPE, "cpe", "", "the new version use in all CPEs, if not set defaults to version") - flagSet.StringVar(&b.CPEPattern, "cpe-pattern", "", "the cpe version pattern of the dependency, if not set defaults to version-pattern") - - if err := flagSet.Parse(os.Args[1:]); err != nil { - log.Fatal(fmt.Errorf("unable to parse flags\n%w", err)) - } - - if b.BuildModulePath == "" { - log.Fatal("buildmodule toml path must be set") - } - - if b.ID == "" { - log.Fatal("id must be set") - } - - if b.SHA256 == "" { - log.Fatal("sha256 must be set") - } - - if b.URI == "" { - log.Fatal("uri must be set") - } - - if b.Version == "" { - log.Fatal("version must be set") - } - - if b.VersionPattern == "" { - log.Fatal("version-pattern must be set") - } - - if b.PURL == "" { - b.PURL = b.Version - } - - if b.PURLPattern == "" { - b.PURLPattern = b.VersionPattern - } - - if b.CPE == "" { - b.CPE = b.Version - } - - if b.CPEPattern == "" { - b.CPEPattern = b.VersionPattern - } - - b.Update() -} diff --git a/cmd/update-lifecycle-dependency/main.go b/cmd/update-lifecycle-dependency/main.go deleted file mode 100644 index b1db319..0000000 --- a/cmd/update-lifecycle-dependency/main.go +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "fmt" - "log" - "os" - - "github.com/spf13/pflag" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func main() { - l := carton.LifecycleDependency{} - - flagSet := pflag.NewFlagSet("Update Lifecycle Dependency", pflag.ExitOnError) - flagSet.StringVar(&l.BuilderPath, "builder-toml", "", "path to builder.toml") - flagSet.StringVar(&l.Version, "version", "", "the new version of the dependency") - - if err := flagSet.Parse(os.Args[1:]); err != nil { - log.Fatal(fmt.Errorf("unable to parse flags\n%w", err)) - } - - if l.BuilderPath == "" { - log.Fatal("builder-toml must be set") - } - - if l.Version == "" { - log.Fatal("version must be set") - } - - l.Update() -} diff --git a/cmd/update-package-dependency/main.go b/cmd/update-package-dependency/main.go deleted file mode 100644 index 6053e41..0000000 --- a/cmd/update-package-dependency/main.go +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2018-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "fmt" - "log" - "os" - - "github.com/spf13/pflag" - - "github.com/paketo-buildpacks/libpak/v2/carton" -) - -func main() { - p := carton.PackageDependency{} - - flagSet := pflag.NewFlagSet("Update Package Dependency", pflag.ExitOnError) - flagSet.StringVar(&p.BuilderPath, "builder-toml", "", "path to builder.toml") - flagSet.StringVar(&p.BuildpackPath, "buildpack-toml", "", "path to buildpack.toml") - flagSet.StringVar(&p.ID, "id", "", "the id of the dependency") - flagSet.StringVar(&p.PackagePath, "package-toml", "", "path to package.toml") - flagSet.StringVar(&p.Version, "version", "", "the new version of the dependency") - - if err := flagSet.Parse(os.Args[1:]); err != nil { - log.Fatal(fmt.Errorf("unable to parse flags\n%w", err)) - } - - if p.BuilderPath == "" && p.BuildpackPath == "" && p.PackagePath == "" { - log.Fatal("builder-toml, buildpack-toml, or package-toml must be set") - } - - if p.ID == "" { - log.Fatal("id must be set") - } - - if p.Version == "" { - log.Fatal("version must be set") - } - - p.Update() -} diff --git a/detect.go b/detect.go index 9d29fb6..757d036 100644 --- a/detect.go +++ b/detect.go @@ -19,16 +19,16 @@ package libpak import ( "github.com/buildpacks/libcnb/v2" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" + "github.com/paketo-buildpacks/libpak/v2/utils" ) // Detect is called by the main function of a buildpack, for detection. func Detect(detector libcnb.DetectFunc, options ...libcnb.Option) { libcnb.Detect(detectDelegate{delegate: detector}.Detect, libcnb.NewConfig(append([]libcnb.Option{ - libcnb.WithExitHandler(internal.NewExitHandler()), - libcnb.WithTOMLWriter(internal.NewTOMLWriter()), + libcnb.WithExitHandler(utils.NewExitHandler()), + libcnb.WithTOMLWriter(utils.NewTOMLWriter()), }, options...)...)) } diff --git a/generate.go b/generate.go index 1fce952..2ecd58a 100644 --- a/generate.go +++ b/generate.go @@ -19,17 +19,17 @@ package libpak import ( "github.com/buildpacks/libcnb/v2" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" + "github.com/paketo-buildpacks/libpak/v2/utils" ) // Generate is called by the main function of an extension, for generation. func Generate(generator libcnb.GenerateFunc, options ...libcnb.Option) { libcnb.Generate(generateDelegate{delegate: generator}.Generate, libcnb.NewConfig(append([]libcnb.Option{ - libcnb.WithEnvironmentWriter(internal.NewEnvironmentWriter()), - libcnb.WithExitHandler(internal.NewExitHandler()), - libcnb.WithTOMLWriter(internal.NewTOMLWriter()), + libcnb.WithEnvironmentWriter(utils.NewEnvironmentWriter()), + libcnb.WithExitHandler(utils.NewExitHandler()), + libcnb.WithTOMLWriter(utils.NewTOMLWriter()), }, options...)...)) } diff --git a/go.mod b/go.mod index d5b7f69..f7ed21f 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,6 @@ require ( github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/onsi/gomega v1.34.2 github.com/sclevine/spec v1.4.0 - github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 ) diff --git a/go.sum b/go.sum index 0fc95f8..df2e1e7 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,6 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8= github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/internal/toml_marshal.go b/internal/toml_marshal.go deleted file mode 100644 index 33e9342..0000000 --- a/internal/toml_marshal.go +++ /dev/null @@ -1,13 +0,0 @@ -package internal - -import ( - "bytes" - - "github.com/BurntSushi/toml" -) - -func Marshal(v interface{}) ([]byte, error) { - buf := new(bytes.Buffer) - err := toml.NewEncoder(buf).Encode(v) - return buf.Bytes(), err -} diff --git a/layer.go b/layer.go index ee666e1..8557848 100644 --- a/layer.go +++ b/layer.go @@ -28,10 +28,10 @@ import ( "github.com/buildpacks/libcnb/v2" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" "github.com/paketo-buildpacks/libpak/v2/sbom" "github.com/paketo-buildpacks/libpak/v2/sherpa" + "github.com/paketo-buildpacks/libpak/v2/utils" ) // ContributeLayersFunc takes a context and result pointer returning a list of Contributables, the list of Contributables will be turned into layers automatically @@ -144,7 +144,7 @@ func (l *LayerContributor) Contribute(layer *libcnb.Layer, f LayerFunc) error { } func (l *LayerContributor) checkIfMetadataMatches(layer libcnb.Layer) (map[string]interface{}, bool, error) { - raw, err := internal.Marshal(l.ExpectedMetadata) + raw, err := utils.Marshal(l.ExpectedMetadata) if err != nil { return map[string]interface{}{}, false, fmt.Errorf("unable to encode metadata\n%w", err) } diff --git a/main.go b/main.go index 3fe8f8a..6722f9d 100644 --- a/main.go +++ b/main.go @@ -19,16 +19,16 @@ package libpak import ( "github.com/buildpacks/libcnb/v2" - "github.com/paketo-buildpacks/libpak/v2/internal" + "github.com/paketo-buildpacks/libpak/v2/utils" ) // Main is called by the main function of a buildpack, encapsulating both detection and build in the same binary. func BuildpackMain(detect libcnb.DetectFunc, build libcnb.BuildFunc, options ...libcnb.Option) { libcnb.BuildpackMain(detectDelegate{delegate: detect}.Detect, buildDelegate{delegate: build}.Build, append([]libcnb.Option{ - libcnb.WithEnvironmentWriter(internal.NewEnvironmentWriter()), - libcnb.WithExitHandler(internal.NewExitHandler()), - libcnb.WithTOMLWriter(internal.NewTOMLWriter()), + libcnb.WithEnvironmentWriter(utils.NewEnvironmentWriter()), + libcnb.WithExitHandler(utils.NewExitHandler()), + libcnb.WithTOMLWriter(utils.NewTOMLWriter()), }, options...)..., ) } @@ -37,9 +37,9 @@ func BuildpackMain(detect libcnb.DetectFunc, build libcnb.BuildFunc, options ... func ExtensionMain(detect libcnb.DetectFunc, generate libcnb.GenerateFunc, options ...libcnb.Option) { libcnb.ExtensionMain(detectDelegate{delegate: detect}.Detect, generateDelegate{delegate: generate}.Generate, append([]libcnb.Option{ - libcnb.WithEnvironmentWriter(internal.NewEnvironmentWriter()), - libcnb.WithExitHandler(internal.NewExitHandler()), - libcnb.WithTOMLWriter(internal.NewTOMLWriter()), + libcnb.WithEnvironmentWriter(utils.NewEnvironmentWriter()), + libcnb.WithExitHandler(utils.NewExitHandler()), + libcnb.WithTOMLWriter(utils.NewTOMLWriter()), }, options...)..., ) } diff --git a/sherpa/sherpa.go b/sherpa/sherpa.go index 779c54e..b4a0f1d 100644 --- a/sherpa/sherpa.go +++ b/sherpa/sherpa.go @@ -22,7 +22,7 @@ import ( "path/filepath" "strconv" - "github.com/paketo-buildpacks/libpak/v2/internal" + "github.com/paketo-buildpacks/libpak/v2/utils" ) // ExecuteFunc is the callback function for buildpack helper application implementations. @@ -31,7 +31,7 @@ type ExecuteFunc func() error // Execute is called by the main function of a buildpack helper application, for execution. func Execute(f ExecuteFunc, options ...Option) { config := Config{ - exitHandler: internal.NewExitHandler(), + exitHandler: utils.NewExitHandler(), } for _, option := range options { diff --git a/internal/match_toml.go b/testing/matchers.go similarity index 99% rename from internal/match_toml.go rename to testing/matchers.go index 3994216..1035ef4 100644 --- a/internal/match_toml.go +++ b/testing/matchers.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal +package testing import ( "fmt" diff --git a/internal/entry_writer.go b/utils/entry_writer.go similarity index 96% rename from internal/entry_writer.go rename to utils/entry_writer.go index fa7237f..e8e5af4 100644 --- a/internal/entry_writer.go +++ b/utils/entry_writer.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal +package utils import ( "fmt" diff --git a/internal/entry_writer_test.go b/utils/entry_writer_test.go similarity index 94% rename from internal/entry_writer_test.go rename to utils/entry_writer_test.go index 1ca41ac..ac525f5 100644 --- a/internal/entry_writer_test.go +++ b/utils/entry_writer_test.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal_test +package utils_test import ( "os" @@ -23,7 +23,7 @@ import ( . "github.com/onsi/gomega" "github.com/sclevine/spec" - "github.com/paketo-buildpacks/libpak/v2/internal" + "github.com/paketo-buildpacks/libpak/v2/utils" ) func testEntryWriter(t *testing.T, context spec.G, it spec.S) { @@ -32,7 +32,7 @@ func testEntryWriter(t *testing.T, context spec.G, it spec.S) { destination string source string - writer internal.EntryWriter + writer utils.EntryWriter ) it.Before(func() { @@ -54,7 +54,7 @@ func testEntryWriter(t *testing.T, context spec.G, it spec.S) { Expect(os.RemoveAll(f.Name())).To(Succeed()) destination = f.Name() - writer = internal.EntryWriter{} + writer = utils.EntryWriter{} }) it.After(func() { diff --git a/internal/environment_writer.go b/utils/environment_writer.go similarity index 97% rename from internal/environment_writer.go rename to utils/environment_writer.go index 479c3d5..5d3d9f7 100644 --- a/internal/environment_writer.go +++ b/utils/environment_writer.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal +package utils import ( "fmt" diff --git a/internal/environment_writer_test.go b/utils/environment_writer_test.go similarity index 89% rename from internal/environment_writer_test.go rename to utils/environment_writer_test.go index a8e406c..ab7f55c 100644 --- a/internal/environment_writer_test.go +++ b/utils/environment_writer_test.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal_test +package utils_test import ( "bytes" @@ -25,8 +25,8 @@ import ( . "github.com/onsi/gomega" "github.com/sclevine/spec" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" + "github.com/paketo-buildpacks/libpak/v2/utils" ) func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) { @@ -34,14 +34,14 @@ func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) { Expect = NewWithT(t).Expect path string - writer internal.EnvironmentWriter + writer utils.EnvironmentWriter ) it.Before(func() { path = t.TempDir() Expect(os.RemoveAll(path)).To(Succeed()) - writer = internal.NewEnvironmentWriter() + writer = utils.NewEnvironmentWriter() }) it("writes the given environment to a directory", func() { @@ -85,7 +85,7 @@ func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) { it.Before(func() { b = bytes.NewBuffer(nil) - writer = internal.NewEnvironmentWriter(internal.WithEnvironmentWriterLogger(log.NewPaketoLogger(b))) + writer = utils.NewEnvironmentWriter(utils.WithEnvironmentWriterLogger(log.NewPaketoLogger(b))) }) it("logs environment", func() { diff --git a/internal/exit_handler.go b/utils/exit_handler.go similarity index 97% rename from internal/exit_handler.go rename to utils/exit_handler.go index 2024098..7e171e4 100644 --- a/internal/exit_handler.go +++ b/utils/exit_handler.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal +package utils import ( "fmt" diff --git a/internal/exit_handler_test.go b/utils/exit_handler_test.go similarity index 82% rename from internal/exit_handler_test.go rename to utils/exit_handler_test.go index bd425d9..db51776 100644 --- a/internal/exit_handler_test.go +++ b/utils/exit_handler_test.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal_test +package utils_test import ( "bytes" @@ -25,8 +25,8 @@ import ( . "github.com/onsi/gomega" "github.com/sclevine/spec" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" + "github.com/paketo-buildpacks/libpak/v2/utils" ) func testExitHandler(t *testing.T, _ spec.G, it spec.S) { @@ -35,16 +35,16 @@ func testExitHandler(t *testing.T, _ spec.G, it spec.S) { b *bytes.Buffer exitCode int - handler internal.ExitHandler + handler utils.ExitHandler ) it.Before(func() { b = bytes.NewBuffer([]byte{}) - handler = internal.NewExitHandler( - internal.WithExitHandlerExitFunc(func(c int) { exitCode = c }), - internal.WithExitHandlerLogger(log.NewPaketoLogger(b)), - internal.WithExitHandlerWriter(b), + handler = utils.NewExitHandler( + utils.WithExitHandlerExitFunc(func(c int) { exitCode = c }), + utils.WithExitHandlerLogger(log.NewPaketoLogger(b)), + utils.WithExitHandlerWriter(b), ) }) diff --git a/internal/init_test.go b/utils/init_test.go similarity index 86% rename from internal/init_test.go rename to utils/init_test.go index 436c025..db6d00a 100644 --- a/internal/init_test.go +++ b/utils/init_test.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal_test +package utils_test import ( "testing" @@ -24,11 +24,11 @@ import ( ) func TestUnit(t *testing.T) { - suite := spec.New("libpak/internal", spec.Report(report.Terminal{})) + suite := spec.New("utils", spec.Report(report.Terminal{})) suite("EntryWriter", testEntryWriter) suite("EnvironmentWriter", testEnvironmentWriter) suite("ExitHandler", testExitHandler) - suite("TOMLWriter", testTOMLWriter) suite("TOMLMarshal", testTOMLMarshal) + suite("TOMLWriter", testTOMLWriter) suite.Run(t) } diff --git a/carton/init_test.go b/utils/toml_marshal.go similarity index 52% rename from carton/init_test.go rename to utils/toml_marshal.go index 6c245ef..4ea1b16 100644 --- a/carton/init_test.go +++ b/utils/toml_marshal.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,22 +14,16 @@ * limitations under the License. */ -package carton_test +package utils import ( - "testing" + "bytes" - "github.com/sclevine/spec" - "github.com/sclevine/spec/report" + "github.com/BurntSushi/toml" ) -func TestUnit(t *testing.T) { - suite := spec.New("libpak/carton", spec.Report(report.Terminal{})) - suite("BuildpackDependency", testBuildpackDependency) - suite("BuildImageDependency", testBuildImageDependency) - suite("LifecycleDependency", testLifecycleDependency) - suite("Netrc", testNetrc) - suite("Package", testPackage) - suite("PackageDependency", testPackageDependency) - suite.Run(t) +func Marshal(v interface{}) ([]byte, error) { + buf := new(bytes.Buffer) + err := toml.NewEncoder(buf).Encode(v) + return buf.Bytes(), err } diff --git a/internal/toml_marshal_test.go b/utils/toml_marshal_test.go similarity index 54% rename from internal/toml_marshal_test.go rename to utils/toml_marshal_test.go index 1128a9f..e1481cc 100644 --- a/internal/toml_marshal_test.go +++ b/utils/toml_marshal_test.go @@ -1,4 +1,19 @@ -package internal_test +/* + * Copyright 2018-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package utils_test import ( "testing" @@ -7,7 +22,7 @@ import ( . "github.com/onsi/gomega" "github.com/sclevine/spec" - "github.com/paketo-buildpacks/libpak/v2/internal" + "github.com/paketo-buildpacks/libpak/v2/utils" ) type Stuff struct { @@ -40,7 +55,7 @@ func testTOMLMarshal(t *testing.T, _ spec.G, it spec.S) { &bString, } - dataBytes, err := internal.Marshal(someData) + dataBytes, err := utils.Marshal(someData) Expect(err).ToNot(HaveOccurred()) Expect(dataBytes).ToNot(BeEmpty()) diff --git a/internal/toml_writer.go b/utils/toml_writer.go similarity index 98% rename from internal/toml_writer.go rename to utils/toml_writer.go index 38269a4..2105950 100644 --- a/internal/toml_writer.go +++ b/utils/toml_writer.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal +package utils import ( "fmt" diff --git a/internal/toml_writer_test.go b/utils/toml_writer_test.go similarity index 90% rename from internal/toml_writer_test.go rename to utils/toml_writer_test.go index 0c744b6..8080dd7 100644 --- a/internal/toml_writer_test.go +++ b/utils/toml_writer_test.go @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 the original author or authors. + * Copyright 2018-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package internal_test +package utils_test import ( "bytes" @@ -28,8 +28,9 @@ import ( . "github.com/onsi/gomega" "github.com/sclevine/spec" - "github.com/paketo-buildpacks/libpak/v2/internal" "github.com/paketo-buildpacks/libpak/v2/log" + libpakTesting "github.com/paketo-buildpacks/libpak/v2/testing" + "github.com/paketo-buildpacks/libpak/v2/utils" ) func testTOMLWriter(t *testing.T, context spec.G, it spec.S) { @@ -38,7 +39,7 @@ func testTOMLWriter(t *testing.T, context spec.G, it spec.S) { parent string path string - tomlWriter internal.TOMLWriter + tomlWriter utils.TOMLWriter ) it.Before(func() { @@ -53,7 +54,7 @@ func testTOMLWriter(t *testing.T, context spec.G, it spec.S) { }) Expect(err).NotTo(HaveOccurred()) - Expect(os.ReadFile(path)).To(internal.MatchTOML(` + Expect(os.ReadFile(path)).To(libpakTesting.MatchTOML(` some-field = "some-value" other-field = "other-value"`)) }) @@ -65,7 +66,7 @@ other-field = "other-value"`)) it.Before(func() { b = bytes.NewBuffer(nil) - tomlWriter = internal.NewTOMLWriter(internal.WithTOMLWriterLogger(log.NewPaketoLogger(b))) + tomlWriter = utils.NewTOMLWriter(utils.WithTOMLWriterLogger(log.NewPaketoLogger(b))) }) it("does not log for uninteresting types", func() {