Skip to content

Commit

Permalink
Remove deprecated buildpack.yml (#282)
Browse files Browse the repository at this point in the history
* Remove buildpack.yml from README

* remove buildpack.yml parsing
  • Loading branch information
Frankie G-J authored Mar 2, 2022
1 parent 69031fa commit 1f851f2
Show file tree
Hide file tree
Showing 12 changed files with 7 additions and 590 deletions.
33 changes: 0 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ when compiling your program.
BP_GO_BUILD_LDFLAGS= -X main.variable=some-value
```

_Note: Specifying the `Go Build` configuration through `buildpack.yml` configuration
will be deprecated in Go Build Buildpack v1.0.0._

To migrate from using `buildpack.yml` please set the following environment
variables at build time either directly (ex. `pack build my-app --env
BP_ENVIRONMENT_VARIABLE=some-value`) or through a [`project.toml`
file](https://github.com/buildpacks/spec/blob/main/extensions/project-descriptor.md)

### `BP_GO_TARGETS`
The `BP_GO_TARGETS` variable allows you to specify multiple programs to be
compiled. The first target will be used as the start command for the image.
Expand All @@ -52,14 +44,6 @@ compiled. The first target will be used as the start command for the image.
BP_GO_TARGETS=./cmd/web-server:./cmd/debug-server
```

This will replace the following structure in `buildpack.yml`:
```yaml
go:
targets:
- ./cmd/web-server
- ./cmd/debug-server
```
### `BP_GO_BUILD_FLAGS`
The `BP_GO_BUILD_FLAGS` variable allows you to override the default build flags
when compiling your program.
Expand All @@ -68,16 +52,6 @@ when compiling your program.
BP_GO_BUILD_FLAGS= -buildmode=default -tags=paketo -ldflags="-X main.variable=some-value"
```

This will replace the following structure in `buildpack.yml`:
```yaml
go:
build:
flags:
- -buildmode=default
- -tags=paketo
- -ldflags="-X main.variable=some-value"
```

### `BP_GO_BUILD_IMPORT_PATH`
The `BP_GO_BUILD_IMPORT_PATH` allows you to specify an import path for your
application. This is necessary if you are building a $GOPATH application that
Expand All @@ -87,13 +61,6 @@ imports its own sub-packages.
BP_GO_BUILD_IMPORT_PATH= example.com/some-app
```

This will replace the following structure in `buildpack.yml`:
```yaml
go:
build:
import-path: example.com/some-app
```

### `BP_KEEP_FILES`
The `BP_KEEP_FILES` variable allows to you to specity a path list of files
(including file globs) that you would like to appear in the workspace of the
Expand Down
27 changes: 4 additions & 23 deletions build_configuration_parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gobuild

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -16,48 +15,30 @@ type TargetManager interface {
GenerateDefaults(workingDir string) ([]string, error)
}

//go:generate faux --interface BuildpackYMLParser --output fakes/buildpack_yml_parser.go
type BuildpackYMLParser interface {
Parse(buildpackVersion, workingDir string) (BuildConfiguration, error)
}

type BuildConfiguration struct {
Targets []string
Flags []string
ImportPath string
}

type BuildConfigurationParser struct {
targetManager TargetManager
buildpackYMLParser BuildpackYMLParser
targetManager TargetManager
}

func NewBuildConfigurationParser(targetManager TargetManager, buildpackYMLParser BuildpackYMLParser) BuildConfigurationParser {
func NewBuildConfigurationParser(targetManager TargetManager) BuildConfigurationParser {
return BuildConfigurationParser{
targetManager: targetManager,
buildpackYMLParser: buildpackYMLParser,
targetManager: targetManager,
}
}

func (p BuildConfigurationParser) Parse(buildpackVersion, workingDir string) (BuildConfiguration, error) {
var buildConfiguration BuildConfiguration

_, err := os.Stat(filepath.Join(workingDir, "buildpack.yml"))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return BuildConfiguration{}, err
}
} else {
buildConfiguration, err = p.buildpackYMLParser.Parse(buildpackVersion, workingDir)
if err != nil {
return BuildConfiguration{}, err
}
}

if val, ok := os.LookupEnv("BP_GO_TARGETS"); ok {
buildConfiguration.Targets = filepath.SplitList(val)
}

var err error
if len(buildConfiguration.Targets) > 0 {
buildConfiguration.Targets, err = p.targetManager.CleanAndValidate(buildConfiguration.Targets, workingDir)
if err != nil {
Expand Down
139 changes: 2 additions & 137 deletions build_configuration_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gobuild_test
import (
"errors"
"os"
"path/filepath"
"testing"

gobuild "github.com/paketo-buildpacks/go-build"
Expand All @@ -19,8 +18,7 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {

workingDir string

targetManager *fakes.TargetManager
buildpackYMLParser *fakes.BuildpackYMLParser
targetManager *fakes.TargetManager

parser gobuild.BuildConfigurationParser
)
Expand All @@ -33,16 +31,7 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {
targetManager = &fakes.TargetManager{}
targetManager.GenerateDefaultsCall.Returns.StringSlice = []string{"."}

buildpackYMLParser = &fakes.BuildpackYMLParser{}
buildpackYMLParser.ParseCall.Returns.BuildConfiguration = gobuild.BuildConfiguration{
Targets: []string{"./first", "./second"},
Flags: []string{
"-first=value",
},
ImportPath: "some-import-path",
}

parser = gobuild.NewBuildConfigurationParser(targetManager, buildpackYMLParser)
parser = gobuild.NewBuildConfigurationParser(targetManager)
})

it.After(func() {
Expand Down Expand Up @@ -205,131 +194,7 @@ func testBuildConfigurationParser(t *testing.T, context spec.G, it spec.S) {
})
})

context("when there is a buildpack.yml and environment variables are not set", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

targetManager.CleanAndValidateCall.Returns.StringSlice = []string{"./first", "./second"}
})

it("parses the targets and flags from a buildpack.yml", func() {
configuration, err := parser.Parse("1.2.3", workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(configuration).To(Equal(gobuild.BuildConfiguration{
Targets: []string{"./first", "./second"},
Flags: []string{
"-first=value",
},
ImportPath: "some-import-path",
}))

Expect(buildpackYMLParser.ParseCall.Receives.WorkingDir).To(Equal(workingDir))

Expect(targetManager.CleanAndValidateCall.Receives.Targets).To(Equal([]string{"./first", "./second"}))
Expect(targetManager.CleanAndValidateCall.Receives.WorkingDir).To(Equal(workingDir))
})
})

context("when there is a buildpack.yml and environment variables are set", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

os.Setenv("BP_GO_BUILD_IMPORT_PATH", "./some/import/path")
os.Setenv("BP_GO_TARGETS", "some/target1:./some/target2")
os.Setenv("BP_GO_BUILD_FLAGS", `-some-flag=some-value`)

targetManager.CleanAndValidateCall.Returns.StringSlice = []string{"./some/target1", "./some/target2"}
})

it.After(func() {
os.Unsetenv("BP_GO_BUILD_IMPORT_PATH")
os.Unsetenv("BP_GO_TARGETS")
os.Unsetenv("BP_GO_BUILD_FLAGS")
})

it("parses the targets and flags from a buildpack.yml but uses the values from the environment variables", func() {
configuration, err := parser.Parse("1.2.3", workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(configuration).To(Equal(gobuild.BuildConfiguration{
Targets: []string{"./some/target1", "./some/target2"},
Flags: []string{
"-some-flag=some-value",
},
ImportPath: "./some/import/path",
}))

Expect(buildpackYMLParser.ParseCall.Receives.WorkingDir).To(Equal(workingDir))
Expect(targetManager.CleanAndValidateCall.Receives.Targets).To(Equal([]string{"some/target1", "./some/target2"}))
Expect(targetManager.CleanAndValidateCall.Receives.WorkingDir).To(Equal(workingDir))
})
})

context("buildpack.yml specifies flags including -ldflags and BP_GO_BUILD_LDFLAGS is set", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

buildpackYMLParser.ParseCall.Returns.BuildConfiguration = gobuild.BuildConfiguration{
Targets: []string{"."},
Flags: []string{
`-ldflags="-buildpack -yml -flags"`,
`-otherflag`,
},
}

os.Setenv("BP_GO_BUILD_LDFLAGS", `-env -value`)
})

it.After(func() {
os.Unsetenv("BP_GO_BUILD_LDFLAGS")
})

it("uses build flags from the buildpack.yml EXCEPT -ldflags", func() {
configuration, err := parser.Parse("1.2.3", workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(configuration).To(Equal(gobuild.BuildConfiguration{
Flags: []string{
`-ldflags=-env -value`,
`-otherflag`,
},
}))

Expect(buildpackYMLParser.ParseCall.Receives.WorkingDir).To(Equal(workingDir))
})
})

context("failure cases", func() {
context("when the buildpack.yml cannot be stat'd", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})

it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})

it("returns an error", func() {
_, err := parser.Parse("1.2.3", workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("buildpack.yml parsing fails", func() {
it.Before(func() {
err := os.WriteFile(filepath.Join(workingDir, "buildpack.yml"), nil, 0644)
Expect(err).NotTo(HaveOccurred())

buildpackYMLParser.ParseCall.Returns.Error = errors.New("failed to parse buildpack.yml")
})

it("returns an error", func() {
_, err := parser.Parse("1.2.3", workingDir)
Expect(err).To(MatchError("failed to parse buildpack.yml"))
})
})

context("go targets fail to be cleaned an validated", func() {
it.Before(func() {
os.Setenv("BP_GO_TARGETS", "./some/target")
Expand Down
35 changes: 0 additions & 35 deletions fakes/buildpack_yml_parser.go

This file was deleted.

3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ go 1.16

require (
github.com/BurntSushi/toml v1.0.0
github.com/Masterminds/semver v1.5.0
github.com/buildkite/interpolate v0.0.0-20200526001904-07f35b4ae251
github.com/mattn/go-shellwords v1.0.11-0.20201201010856-2c8720de5e83
github.com/onsi/gomega v1.18.1
github.com/paketo-buildpacks/occam v0.6.0
github.com/paketo-buildpacks/packit/v2 v2.1.0
github.com/sclevine/spec v1.4.0
gopkg.in/yaml.v2 v2.4.0
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ github.com/CycloneDX/cyclonedx-go v0.4.0 h1:Wz4QZ9B4RXGWIWTypVLEOVJgOdFfy5mcS5PG
github.com/CycloneDX/cyclonedx-go v0.4.0/go.mod h1:rmRcf//gT7PIzovatusbWi377xqCg1FS4jyST0GH20E=
github.com/ForestEckhardt/freezer v0.0.10 h1:CQbYPeA/KxXMZhNeRd3Mk7IYV9uw7w9OkmW4alHqIFw=
github.com/ForestEckhardt/freezer v0.0.10/go.mod h1:GHguI9kaXxL6on564JrxYs25dWuMqbqTYQVtvnkH4os=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
Expand Down Expand Up @@ -150,8 +148,6 @@ github.com/buger/jsonparser v0.0.0-20180808090653-f4dd9f5a6b44/go.mod h1:bbYlZJ7
github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50=
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/buildkite/interpolate v0.0.0-20200526001904-07f35b4ae251 h1:k6UDF1uPYOs0iy1HPeotNa155qXRWrzKnqAaGXHLZCE=
github.com/buildkite/interpolate v0.0.0-20200526001904-07f35b4ae251/go.mod h1:gbPR1gPu9dB96mucYIR7T3B7p/78hRVSOuzIWLHK2Y4=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down
Loading

0 comments on commit 1f851f2

Please sign in to comment.