-
Notifications
You must be signed in to change notification settings - Fork 26
/
layers.go
70 lines (59 loc) · 1.81 KB
/
layers.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
package packit
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
// Layers represents the set of layers managed by a buildpack.
type Layers struct {
// Path is the absolute location of the set of layers managed by a buildpack
// on disk.
Path string
}
// Get will either create a new layer with the given name and layer types. If a
// layer already exists on disk, then the layer metadata will be retrieved from
// disk and returned instead.
func (l Layers) Get(name string) (Layer, error) {
layer := Layer{
Path: filepath.Join(l.Path, name),
Name: name,
SharedEnv: Environment{},
BuildEnv: Environment{},
LaunchEnv: Environment{},
ProcessLaunchEnv: make(map[string]Environment),
}
_, err := toml.DecodeFile(filepath.Join(l.Path, fmt.Sprintf("%s.toml", name)), &layer)
if err != nil {
if !os.IsNotExist(err) {
return Layer{}, fmt.Errorf("failed to parse layer content metadata: %s", err)
}
}
layer.SharedEnv, err = newEnvironmentFromPath(filepath.Join(l.Path, name, "env"))
if err != nil {
return Layer{}, err
}
layer.BuildEnv, err = newEnvironmentFromPath(filepath.Join(l.Path, name, "env.build"))
if err != nil {
return Layer{}, err
}
layer.LaunchEnv, err = newEnvironmentFromPath(filepath.Join(l.Path, name, "env.launch"))
if err != nil {
return Layer{}, err
}
if _, err := os.Stat(filepath.Join(l.Path, name, "env.launch")); !os.IsNotExist(err) {
paths, err := os.ReadDir(filepath.Join(l.Path, name, "env.launch"))
if err != nil {
return Layer{}, err
}
for _, path := range paths {
if path.IsDir() {
layer.ProcessLaunchEnv[path.Name()], err = newEnvironmentFromPath(filepath.Join(l.Path, name, "env.launch", path.Name()))
if err != nil {
return Layer{}, err
}
}
}
}
return layer, nil
}