This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
forked from paketo-buildpacks/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
66 lines (56 loc) · 1.97 KB
/
detect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cpython
import (
"fmt"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
//go:generate faux --interface VersionParser --output fakes/version_parser.go
// VersionParser defines the interface for determining the version of Cpython.
type VersionParser interface {
ParseVersion(path string) (version string, err error)
}
// BuildPlanMetadata is the buildpack specific data included in build plan
// requirements.
type BuildPlanMetadata struct {
// Version denotes the version constraint to be requested in the requirements.
Version string `toml:"version"`
// VersionSource denotes the source of the version information. This may be
// used by the consumer of the metadata to determine the priority of this
// version request.
VersionSource string `toml:"version-source"`
}
// Detect will return a packit.DetectFunc that will be invoked during the
// detect phase of the buildpack lifecycle.
//
// Detect always passes, and will contribute a Build Plan that provides cpython.
func Detect() packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
bpYML, err := fs.Exists(filepath.Join(context.WorkingDir, "buildpack.yml"))
if err != nil {
return packit.DetectResult{}, fmt.Errorf("failed to check for buildpack.yml: %w", err)
}
if bpYML {
return packit.DetectResult{}, fmt.Errorf("working directory contains deprecated 'buildpack.yml'; use environment variables for configuration")
}
var requirements []packit.BuildPlanRequirement
if version, ok := os.LookupEnv("BP_CPYTHON_VERSION"); ok {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: Cpython,
Metadata: BuildPlanMetadata{
Version: version,
VersionSource: "BP_CPYTHON_VERSION",
},
})
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: Cpython},
},
Requires: requirements,
},
}, nil
}
}