forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.go
115 lines (92 loc) · 2.57 KB
/
output.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package baur
import (
"fmt"
"path/filepath"
"github.com/simplesurance/baur/v1/internal/digest"
)
type OutputType int
const (
DockerOutput OutputType = iota
FileOutput
)
func (o OutputType) String() string {
switch o {
case DockerOutput:
return "docker"
case FileOutput:
return "file"
default:
return "invalid OutputType"
}
}
type Output interface {
Name() string
String() string
Exists() (bool, error)
Size() (uint64, error)
Digest() (*digest.Digest, error)
Type() OutputType
}
func dockerOutputs(dockerClient DockerInfoClient, task *Task) ([]Output, error) {
result := make([]Output, 0, len(task.Outputs.DockerImage))
for _, dockerOutput := range task.Outputs.DockerImage {
d, err := NewOutputDockerImageFromIIDFile(
dockerClient,
dockerOutput.IDFile,
filepath.Join(task.Directory, dockerOutput.IDFile),
&UploadInfoDocker{
Registry: dockerOutput.RegistryUpload.Registry,
Repository: dockerOutput.RegistryUpload.Repository,
Tag: dockerOutput.RegistryUpload.Tag,
},
)
if err != nil {
return nil, err
}
result = append(result, d)
}
return result, nil
}
func fileOutputs(task *Task) ([]Output, error) {
result := make([]Output, 0, len(task.Outputs.File))
for _, fileOutput := range task.Outputs.File {
var s3Upload *UploadInfoS3
var fileCopyUpload *UploadInfoFileCopy
if fileOutput.S3Upload.IsEmpty() && fileOutput.FileCopy.IsEmpty() {
return nil, fmt.Errorf("no upload method for output %q is specified", fileOutput.Path)
}
// TODO: use pointers in the outputfile struct for filecopy and S3 instead of having to provide and use IsEmpty)
if !fileOutput.S3Upload.IsEmpty() {
s3Upload = &UploadInfoS3{
Bucket: fileOutput.S3Upload.Bucket,
Key: fileOutput.S3Upload.Key,
}
}
if !fileOutput.FileCopy.IsEmpty() {
fileCopyUpload = &UploadInfoFileCopy{DestinationPath: fileOutput.FileCopy.Path}
}
result = append(result, NewOutputFile(
fileOutput.Path,
filepath.Join(task.Directory, fileOutput.Path),
s3Upload,
fileCopyUpload,
))
}
return result, nil
}
// OutputsFromTask returns the Outputs that running the task produces.
// If the outputs do not exist, the function might fail.
func OutputsFromTask(dockerClient DockerInfoClient, task *Task) ([]Output, error) {
dockerImages, err := dockerOutputs(dockerClient, task)
if err != nil {
return nil, err
}
files, err := fileOutputs(task)
if err != nil {
return nil, err
}
result := make([]Output, 0, len(dockerImages)+len(files))
result = append(result, dockerImages...)
result = append(result, files...)
return result, nil
}