forked from paketo-buildpacks/occam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
89 lines (78 loc) · 2.15 KB
/
image.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
package occam
import (
"encoding/json"
"fmt"
)
type Image struct {
ID string
Buildpacks []ImageBuildpackMetadata
Labels map[string]string
}
type ImageBuildpackMetadata struct {
Key string
Layers map[string]ImageBuildpackMetadataLayer
}
type ImageBuildpackMetadataLayer struct {
SHA string
Build bool
Launch bool
Cache bool
Metadata map[string]interface{}
}
func NewImageFromInspectOutput(output []byte) (Image, error) {
var inspect []struct {
ID string `json:"Id"`
Config struct {
Labels map[string]string `json:"Labels"`
} `json:"Config"`
}
err := json.Unmarshal(output, &inspect)
if err != nil {
return Image{}, fmt.Errorf("failed to inspect docker image: %w", err)
}
var metadata struct {
Buildpacks []struct {
Key string `json:"key"`
Layers map[string]struct {
SHA string `json:"sha"`
Build bool `json:"build"`
Launch bool `json:"launch"`
Cache bool `json:"cache"`
Data map[string]interface{} `json:"data"`
} `json:"layers"`
} `json:"buildpacks"`
}
err = json.Unmarshal([]byte(inspect[0].Config.Labels["io.buildpacks.lifecycle.metadata"]), &metadata)
if err != nil {
return Image{}, fmt.Errorf("failed to inspect docker image: %w", err)
}
var buildpacks []ImageBuildpackMetadata
for _, buildpack := range metadata.Buildpacks {
layers := map[string]ImageBuildpackMetadataLayer{}
for name, layer := range buildpack.Layers {
layers[name] = ImageBuildpackMetadataLayer{
SHA: layer.SHA,
Build: layer.Build,
Launch: layer.Launch,
Cache: layer.Cache,
Metadata: layer.Data,
}
}
buildpacks = append(buildpacks, ImageBuildpackMetadata{
Key: buildpack.Key,
Layers: layers})
}
return Image{
ID: inspect[0].ID,
Buildpacks: buildpacks,
Labels: inspect[0].Config.Labels,
}, nil
}
func (i Image) BuildpackForKey(key string) (ImageBuildpackMetadata, error) {
for _, buildpack := range i.Buildpacks {
if buildpack.Key == key {
return buildpack, nil
}
}
return ImageBuildpackMetadata{}, fmt.Errorf("no buildpack found for key: %s", key)
}