forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
143 lines (123 loc) · 3.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package packit
import (
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/paketo-buildpacks/packit/v2/internal"
)
// DetectFunc is the definition of a callback that can be invoked when the
// Detect function is executed. Buildpack authors should implement a DetectFunc
// that performs the specific detect phase operations for a buildpack.
type DetectFunc func(DetectContext) (DetectResult, error)
// DetectContext provides the contextual details that are made available by the
// buildpack lifecycle during the detect phase. This context is populated by
// the Detect function and passed to the DetectFunc during execution.
type DetectContext struct {
// WorkingDir is the location of the application source code as provided by
// the lifecycle.
WorkingDir string
// CNBPath is the absolute path location of the buildpack contents.
// This path is useful for finding the buildpack.toml or any other
// files included in the buildpack.
CNBPath string
// Platform includes the platform context according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#detection
Platform Platform
// BuildpackInfo
// Deprecated: Use Info instead
BuildpackInfo Info
// Info includes the details of the buildpack (or extension) parsed
// from the buildpack.toml (or extension.tom) included in the buildpack
// (or extension) contents.
Info Info
// Stack is the value of the chosen stack. This value is populated from the
// $CNB_STACK_ID environment variable.
Stack string
}
// DetectResult allows buildpack authors to indicate the result of the detect
// phase for a given buildpack. This result, returned in a DetectFunc callback,
// will be parsed and persisted by the Detect function and returned to the
// lifecycle at the end of the detect phase execution.
type DetectResult struct {
// Plan is the set of Build Plan provisions and requirements that are
// detected during the detect phase of the lifecycle.
Plan BuildPlan
}
// Detect is an implementation of the detect phase according to the Cloud
// Native Buildpacks specification. Calling this function with a DetectFunc
// will perform the detect phase process.
func Detect(f DetectFunc, options ...Option) {
config := OptionConfig{
exitHandler: internal.NewExitHandler(),
args: os.Args,
}
for _, option := range options {
config = option(config)
}
dir, err := os.Getwd()
if err != nil {
config.exitHandler.Error(err)
return
}
cnbPath, ok := os.LookupEnv("CNB_BUILDPACK_DIR")
isExtension := false
if !ok {
cnbPath, ok = os.LookupEnv("CNB_EXTENSION_DIR")
isExtension = ok
}
if !ok {
cnbPath = filepath.Clean(strings.TrimSuffix(config.args[0], filepath.Join("bin", "detect")))
}
info := Info{}
if isExtension {
_, err = toml.DecodeFile(filepath.Join(cnbPath, "extension.toml"), &struct {
Extension *Info `toml:"extension"`
}{
Extension: &info,
})
} else {
_, err = toml.DecodeFile(filepath.Join(cnbPath, "buildpack.toml"), &struct {
Buildpack *Info `toml:"buildpack"`
}{
Buildpack: &info,
})
}
if err != nil {
config.exitHandler.Error(err)
return
}
platformPath, ok := os.LookupEnv("CNB_PLATFORM_DIR")
if !ok {
platformPath = config.args[1]
}
result, err := f(DetectContext{
WorkingDir: dir,
Platform: Platform{
Path: platformPath,
},
CNBPath: cnbPath,
BuildpackInfo: info,
Info: info,
Stack: os.Getenv("CNB_STACK_ID"),
})
if err != nil {
config.exitHandler.Error(err)
return
}
planPath, ok := os.LookupEnv("CNB_BUILD_PLAN_PATH")
if !ok {
planPath = config.args[2]
}
file, err := os.OpenFile(planPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
config.exitHandler.Error(err)
return
}
defer file.Close()
err = toml.NewEncoder(file).Encode(result.Plan)
if err != nil {
config.exitHandler.Error(err)
return
}
}