Skip to content

Commit

Permalink
Adds in functionality to set targets in ./cmd by default
Browse files Browse the repository at this point in the history
- If there is no targets set and there is no *.go file in the app root
then the buildpack will walk down ./cmd looking for top level *.go files
and adding directories that have those files into the targets list
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Nov 7, 2020
1 parent f9f97ca commit 4818f14
Show file tree
Hide file tree
Showing 16 changed files with 822 additions and 157 deletions.
20 changes: 16 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func Build(
clock chronos.Clock,
checksumCalculator ChecksumCalculator,
logs LogEmitter,
parser ConfigurationParser,
sourceRemover SourceRemover,
) packit.BuildFunc {

Expand All @@ -60,9 +59,22 @@ func Build(

previousSum, _ := targetsLayer.Metadata[WorkspaceSHAKey].(string)
if checksum != previousSum {
configuration, err := parser.Parse(filepath.Join(context.WorkingDir, "buildpack.yml"))
if err != nil {
return packit.BuildResult{}, err
configuration := BuildConfiguration{}

entry := context.Plan.Entries[0]

for _, target := range entry.Metadata["targets"].([]interface{}) {
configuration.Targets = append(configuration.Targets, target.(string))
}

if flags, ok := entry.Metadata["flags"]; ok {
for _, flag := range flags.([]interface{}) {
configuration.Flags = append(configuration.Flags, flag.(string))
}
}

if importPath, ok := entry.Metadata["import-path"]; ok {
configuration.ImportPath = importPath.(string)
}

goPath, path, err := pathManager.Setup(context.WorkingDir, configuration.ImportPath)
Expand Down
60 changes: 41 additions & 19 deletions build_configuration_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,48 @@ import (
"gopkg.in/yaml.v2"
)

//go:generate faux --interface TargetManager --output fakes/target_manager.go
type TargetManager interface {
CleanAndValidate(targets []string, workingDir string) ([]string, error)
GenerateDefaults(workingDir string) ([]string, error)
}

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

type BuildConfigurationParser struct{}
type BuildConfigurationParser struct {
targetManager TargetManager
}

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

func (p BuildConfigurationParser) Parse(path string) (BuildConfiguration, error) {
func (p BuildConfigurationParser) Parse(workingDir string) (BuildConfiguration, error) {
var targets []string
if val, ok := os.LookupEnv("BP_GO_TARGETS"); ok {
targets = filepath.SplitList(val)
}

file, err := os.Open(path)
file, err := os.Open(filepath.Join(workingDir, "buildpack.yml"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if len(targets) == 0 {
targets = []string{"."}
targets, err = p.targetManager.GenerateDefaults(workingDir)
if err != nil {
return BuildConfiguration{}, err
}
} else {
targets, err = p.targetManager.CleanAndValidate(targets, workingDir)
if err != nil {
return BuildConfiguration{}, err
}
}

return BuildConfiguration{Targets: targets}, nil
}

Expand All @@ -57,8 +74,24 @@ func (p BuildConfigurationParser) Parse(path string) (BuildConfiguration, error)
return BuildConfiguration{}, fmt.Errorf("failed to decode buildpack.yml: %w", err)
}

// This will use targets that it got from the env var over the targests set in buildpack.yml
if len(targets) > 0 {
config.Go.Targets = targets
config.Go.Targets, err = p.targetManager.CleanAndValidate(targets, workingDir)
if err != nil {
return BuildConfiguration{}, err
}
} else {
config.Go.Targets, err = p.targetManager.CleanAndValidate(config.Go.Targets, workingDir)
if err != nil {
return BuildConfiguration{}, err
}
}

if len(config.Go.Targets) == 0 {
config.Go.Targets, err = p.targetManager.GenerateDefaults(workingDir)
if err != nil {
return BuildConfiguration{}, err
}
}

env := interpolate.NewSliceEnv(os.Environ())
Expand All @@ -75,17 +108,6 @@ func (p BuildConfigurationParser) Parse(path string) (BuildConfiguration, error)
}
config.Go.Build.Flags = buildFlags

for index, target := range config.Go.Targets {
if strings.HasPrefix(target, string(filepath.Separator)) {
return BuildConfiguration{}, fmt.Errorf("failed to determine build targets: %q is an absolute path, targets must be relative to the source directory", target)
}
config.Go.Targets[index] = fmt.Sprintf("./%s", filepath.Clean(target))
}

if len(config.Go.Targets) == 0 {
config.Go.Targets = []string{"."}
}

return BuildConfiguration{
Targets: config.Go.Targets,
Flags: config.Go.Build.Flags,
Expand Down
Loading

0 comments on commit 4818f14

Please sign in to comment.