forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.go
94 lines (82 loc) · 2.41 KB
/
save.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
92
93
94
package lifecycle
import (
"fmt"
"github.com/buildpacks/imgutil"
"github.com/buildpacks/imgutil/local"
"github.com/buildpacks/imgutil/remote"
"github.com/pkg/errors"
"github.com/buildpacks/lifecycle/platform"
)
func saveImage(image imgutil.Image, additionalNames []string, logger Logger) (platform.ImageReport, error) {
var saveErr error
imageReport := platform.ImageReport{}
logger.Infof("Saving %s...\n", image.Name())
if err := image.Save(additionalNames...); err != nil {
var ok bool
if saveErr, ok = err.(imgutil.SaveError); !ok {
return platform.ImageReport{}, errors.Wrap(err, "saving image")
}
}
id, idErr := image.Identifier()
if idErr != nil {
if saveErr != nil {
return platform.ImageReport{}, &MultiError{Errors: []error{idErr, saveErr}}
}
return platform.ImageReport{}, idErr
}
logger.Infof("*** Images (%s):\n", shortID(id))
for _, n := range append([]string{image.Name()}, additionalNames...) {
if ok, message := getSaveStatus(saveErr, n); !ok {
logger.Infof(" %s - %s\n", n, message)
} else {
logger.Infof(" %s\n", n)
imageReport.Tags = append(imageReport.Tags, n)
}
}
switch v := id.(type) {
case local.IDIdentifier:
imageReport.ImageID = v.String()
logger.Debugf("\n*** Image ID: %s\n", v.String())
case remote.DigestIdentifier:
imageReport.Digest = v.Digest.DigestStr()
logger.Debugf("\n*** Digest: %s\n", v.Digest.DigestStr())
default:
}
manifestSize, sizeErr := image.ManifestSize()
if sizeErr != nil {
// ignore the manifest size if it's unavailable
logger.Infof("*** Manifest size is unavailable: %s\n", sizeErr.Error())
} else if manifestSize != 0 {
imageReport.ManifestSize = manifestSize
logger.Debugf("\n*** Manifest Size: %d\n", manifestSize)
}
return imageReport, saveErr
}
type MultiError struct {
Errors []error
}
func (me *MultiError) Error() string {
return fmt.Sprintf("failed with multiple errors %+v", me.Errors)
}
func shortID(identifier imgutil.Identifier) string {
switch v := identifier.(type) {
case local.IDIdentifier:
return TruncateSha(v.String())
case remote.DigestIdentifier:
return v.Digest.DigestStr()
default:
return v.String()
}
}
func getSaveStatus(err error, imageName string) (bool, string) {
if err != nil {
if saveErr, ok := err.(imgutil.SaveError); ok {
for _, d := range saveErr.Errors {
if d.ImageName == imageName {
return false, d.Cause.Error()
}
}
}
}
return true, ""
}