forked from buildpacks/libcnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_detect_test.go
67 lines (61 loc) · 1.86 KB
/
example_detect_test.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
67
package libcnb_test
import (
"errors"
"os"
"path/filepath"
"github.com/buildpacks/libcnb"
"github.com/buildpacks/libcnb/log"
)
const (
Provides = "example"
BpExampleVersion = "BP_EXAMPLE_VERSION"
)
type Detector struct {
Logger log.Logger
}
func (Detector) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error) {
version := "1.0"
// Scan the application source folder to see if the example buildpack is
// required. If `version.toml` does not exist we return a failed DetectResult
// but no runtime error has occurred, so we return an empty error.
versionPath := filepath.Join(context.ApplicationPath, "version.toml")
if _, err := os.Open(versionPath); errors.Is(err, os.ErrNotExist) {
return libcnb.DetectResult{}, nil
}
// Read the version number from the buildpack definition
if exampleVersion, exists := context.Buildpack.Metadata["version"]; exists {
version = exampleVersion.(string)
}
// Accept version number from the environment if the user provides it
if exampleVersion, exists := context.Platform.Environment[BpExampleVersion]; exists {
version = exampleVersion
}
metadata := map[string]interface{}{
"version": version,
}
return libcnb.DetectResult{
Pass: true,
Plans: []libcnb.BuildPlan{
{
// Let the system know that if other buildpacks Require "example"
// then this buildpack Provides the implementation logic.
Provides: []libcnb.BuildPlanProvide{
{Name: Provides},
},
// It is common for a buildpack to Require itself if the build phase
// needs information from the detect phase. Here we pass the version number
// as metadata to the build phase.
Requires: []libcnb.BuildPlanRequire{
{
Name: Provides,
Metadata: metadata,
},
},
},
},
}, nil
}
func ExampleDetect() {
detector := Detector{log.New(os.Stdout)}
libcnb.Main(detector.Detect, nil)
}