forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutputdockerimage.go
77 lines (63 loc) · 1.74 KB
/
outputdockerimage.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
package baur
import (
"fmt"
"github.com/simplesurance/baur/v1/internal/digest"
"github.com/simplesurance/baur/v1/internal/fs"
)
type DockerInfoClient interface {
Size(imageID string) (int64, error)
Exists(imageID string) (bool, error)
}
// OutputDockerImage is a docker image artifact.
type OutputDockerImage struct {
name string
ImageID string
UploadDestination *UploadInfoDocker
dockerClient DockerInfoClient
digest *digest.Digest
}
func NewOutputDockerImageFromIIDFile(
dockerClient DockerInfoClient,
name,
iidfile string,
uploadDest *UploadInfoDocker,
) (*OutputDockerImage, error) {
id, err := fs.FileReadLine(iidfile)
if err != nil {
return nil, fmt.Errorf("reading %s failed: %w", iidfile, err)
}
digest, err := digest.FromString(id)
if err != nil {
return nil, fmt.Errorf("image id %q read from %q has an invalid format: %w", id, iidfile, err)
}
return &OutputDockerImage{
name: name,
dockerClient: dockerClient,
ImageID: id,
UploadDestination: uploadDest,
digest: digest,
}, nil
}
func (d *OutputDockerImage) String() string {
return fmt.Sprintf("docker image: %s", d.ImageID)
}
func (d *OutputDockerImage) Name() string {
return d.name
}
func (d *OutputDockerImage) Type() OutputType {
return DockerOutput
}
func (d *OutputDockerImage) Exists() (bool, error) {
return d.dockerClient.Exists(d.ImageID)
}
// Digest returns the imageID as a digest object. The method always returns a nil error.
func (d *OutputDockerImage) Digest() (*digest.Digest, error) {
return d.digest, nil
}
func (d *OutputDockerImage) Size() (uint64, error) {
size, err := d.dockerClient.Size(d.ImageID)
if err != nil {
return 0, nil
}
return uint64(size), nil
}