forked from micahyoung/buildpacks-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_builder.go
90 lines (76 loc) · 2.18 KB
/
inspect_builder.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
package pack
import (
"github.com/buildpack/lifecycle/image"
"github.com/buildpack/pack/builder"
"github.com/pkg/errors"
)
type BuilderInfo struct {
Stack string
RunImage string
RunImageMirrors []string
LocalRunImageMirrors []string
Buildpacks []BuildpackInfo
Groups [][]BuildpackInfo
}
type BuildpackInfo struct {
ID string
Version string
Latest bool
}
func (c *Client) InspectBuilder(name string, daemon bool) (*BuilderInfo, error) {
var (
img image.Image
err error
)
if daemon {
img, err = c.fetcher.FetchLocalImage(name)
} else {
img, err = c.fetcher.FetchRemoteImage(name)
}
if err != nil {
return nil, errors.Wrapf(err, "failed to get builder image '%s'", name)
}
if found, err := img.Found(); err != nil {
return nil, errors.Wrapf(err, "failed to find builder image '%s'", name)
} else if !found {
return nil, nil
}
bldr := builder.NewBuilder(img, c.config)
stackID, err := bldr.GetStack()
if err != nil {
return nil, errors.Wrapf(err, "failed to get stackID for builder image '%s'", name)
}
metadata, err := bldr.GetMetadata()
if err != nil {
return nil, errors.Wrapf(err, "failed to get metadata for builder image '%s'", name)
}
localMirrors, err := bldr.GetLocalRunImageMirrors()
if err != nil {
return nil, errors.Wrapf(err, "failed to get local run image mirrors for builder image '%s'", name)
}
var buildpacks []BuildpackInfo
for _, bp := range metadata.Buildpacks {
buildpacks = append(buildpacks, buildpackMetadataToInfo(bp))
}
groups := make([][]BuildpackInfo, len(metadata.Groups))
for i, group := range metadata.Groups {
for _, bp := range group.Buildpacks {
groups[i] = append(groups[i], buildpackMetadataToInfo(bp))
}
}
return &BuilderInfo{
Stack: stackID,
RunImage: metadata.Stack.RunImage.Image,
RunImageMirrors: metadata.Stack.RunImage.Mirrors,
LocalRunImageMirrors: localMirrors,
Buildpacks: buildpacks,
Groups: groups,
}, nil
}
func buildpackMetadataToInfo(bp builder.BuildpackMetadata) BuildpackInfo {
return BuildpackInfo{
ID: bp.ID,
Version: bp.Version,
Latest: bp.Latest,
}
}