-
Notifications
You must be signed in to change notification settings - Fork 26
/
option.go
64 lines (54 loc) · 2.06 KB
/
option.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
package packit
import "io"
// OptionConfig is the set of configurable options for the Build and Detect
// functions.
type OptionConfig struct {
exitHandler ExitHandler
args []string
tomlWriter TOMLWriter
envWriter EnvironmentWriter
fileWriter FileWriter
}
// Option declares a function signature that can be used to define optional
// modifications to the behavior of the Detect and Build functions.
type Option func(config OptionConfig) OptionConfig
//go:generate faux --interface ExitHandler --output fakes/exit_handler.go
// ExitHandler serves as the interface for types that can handle an error
// during the Detect or Build functions. ExitHandlers are responsible for
// translating error values into exit codes according the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#detection and
// https://github.com/buildpacks/spec/blob/main/buildpack.md#build.
type ExitHandler interface {
Error(error)
}
// TOMLWriter serves as the interface for types that can handle the writing of
// TOML files. TOMLWriters take a path to a file location on disk and a
// datastructure to marshal.
type TOMLWriter interface {
Write(path string, value interface{}) error
}
// EnvironmentWriter serves as the interface for types that can write an
// Environment to a directory on disk according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
type EnvironmentWriter interface {
Write(dir string, env map[string]string) error
}
type FileWriter interface {
Write(path string, reader io.Reader) error
}
// WithExitHandler is an Option that overrides the ExitHandler for a given
// invocation of Build or Detect.
func WithExitHandler(exitHandler ExitHandler) Option {
return func(config OptionConfig) OptionConfig {
config.exitHandler = exitHandler
return config
}
}
// WithArgs is an Option that overrides the value of os.Args for a given
// invocation of Build or Detect.
func WithArgs(args []string) Option {
return func(config OptionConfig) OptionConfig {
config.args = args
return config
}
}