forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
91 lines (75 loc) · 3.64 KB
/
layer.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
package packit
import (
"fmt"
"os"
)
// Layer provides a representation of a layer managed by a buildpack as
// described by the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#layers.
type Layer struct {
// Path is the absolute location of the layer on disk.
Path string `toml:"-"`
// Name is the descriptive name of the layer.
Name string `toml:"-"`
// Build indicates whether the layer is available to subsequent buildpacks
// during their build phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#build-layers.
Build bool `toml:"build"`
// Launch indicates whether the layer is exported into the application image
// and made available during the launch phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#launch-layers.
Launch bool `toml:"launch"`
// Cache indicates whether the layer is persisted and made available to
// subsequent builds of the same application according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#launch-layers
// and
// https://github.com/buildpacks/spec/blob/main/buildpack.md#build-layers.
Cache bool `toml:"cache"`
// SharedEnv is the set of environment variables attached to the layer and
// made available during both the build and launch phases according to the
// specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
SharedEnv Environment `toml:"-"`
// BuildEnv is the set of environment variables attached to the layer and
// made available during the build phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
BuildEnv Environment `toml:"-"`
// LaunchEnv is the set of environment variables attached to the layer and
// made available during the launch phase according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
LaunchEnv Environment `toml:"-"`
// ProcessLaunchEnv is a map of environment variables attached to the layer and
// made available to specified proccesses in the launch phase accoring to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks
ProcessLaunchEnv map[string]Environment `toml:"-"`
// Metadata is an unspecified field allowing buildpacks to communicate extra
// details about the layer. Examples of this type of metadata might include
// details about what versions of software are included in the layer such
// that subsequent builds can inspect that metadata and choose to reuse the
// layer if suitable. The Metadata field ultimately fills the metadata field
// of the Layer Content Metadata TOML file according to the specification:
// https://github.com/buildpacks/spec/blob/main/buildpack.md#layer-content-metadata-toml.
Metadata map[string]interface{} `toml:"metadata"`
}
// Reset clears the state of a layer such that the layer can be replaced with
// new content and metadata. It clears all environment variables, and removes
// the content of the layer directory on disk.
func (l Layer) Reset() (Layer, error) {
l.Build = false
l.Launch = false
l.Cache = false
l.SharedEnv = Environment{}
l.BuildEnv = Environment{}
l.LaunchEnv = Environment{}
l.ProcessLaunchEnv = make(map[string]Environment)
l.Metadata = nil
err := os.RemoveAll(l.Path)
if err != nil {
return Layer{}, fmt.Errorf("error could not remove file: %s", err)
}
err = os.MkdirAll(l.Path, os.ModePerm)
if err != nil {
return Layer{}, fmt.Errorf("error could not create directory: %s", err)
}
return l, nil
}