forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.go
150 lines (119 loc) · 2.95 KB
/
uploader.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package baur
import (
"fmt"
"path/filepath"
"reflect"
"time"
)
type S3Uploader interface {
Upload(filepath, bucket, key string) (string, error)
}
type DockerImgUploader interface {
Upload(image, registryAddr, repository, tag string) (string, error)
}
type FileCopyUploader interface {
Upload(src string, dst string) (string, error)
}
type Uploader struct {
dockerclient DockerImgUploader
s3client S3Uploader
filecopyUploader FileCopyUploader
}
func NewUploader(dockerClient DockerImgUploader, s3client S3Uploader, filecopyUploader FileCopyUploader) *Uploader {
return &Uploader{
dockerclient: dockerClient,
s3client: s3client,
filecopyUploader: filecopyUploader,
}
}
type UploadResult struct {
Output Output
URL string
Start time.Time
Stop time.Time
Method UploadMethod
}
type UploadStartFn func(Output, UploadInfo)
type UploadResultFn func(Output, *UploadResult)
func (u *Uploader) Upload(output Output, uploadStartCb UploadStartFn, resultCb UploadResultFn) error {
switch o := output.(type) {
case *OutputDockerImage:
if o.UploadDestination == nil {
break
}
uploadStartCb(o, o.UploadDestination)
result, err := u.DockerImage(o)
if err != nil {
return fmt.Errorf("docker upload failed: %w", err)
}
resultCb(o, result)
case *OutputFile:
if o.UploadsFilecopy != nil {
uploadStartCb(o, o.UploadsFilecopy)
result, err := u.FileCopy(o)
if err != nil {
return fmt.Errorf("filecopy failed: %w", err)
}
resultCb(o, result)
}
if o.UploadsS3 != nil {
uploadStartCb(o, o.UploadsS3)
result, err := u.S3(o)
if err != nil {
return fmt.Errorf("s3 upload failed: %w", err)
}
resultCb(o, result)
}
default:
return fmt.Errorf("unsupported output type: %s", reflect.TypeOf(output).Kind())
}
return nil
}
func (u *Uploader) DockerImage(o *OutputDockerImage) (*UploadResult, error) {
startTime := time.Now()
url, err := u.dockerclient.Upload(
o.ImageID,
o.UploadDestination.Registry,
o.UploadDestination.Repository,
o.UploadDestination.Tag,
)
if err != nil {
return nil, err
}
return &UploadResult{
Start: startTime,
Stop: time.Now(),
Method: UploadMethodDocker,
Output: o,
URL: url,
}, nil
}
func (u *Uploader) FileCopy(o *OutputFile) (*UploadResult, error) {
startTime := time.Now()
destFile := filepath.Join(o.UploadsFilecopy.DestinationPath, filepath.Base(o.AbsPath))
url, err := u.filecopyUploader.Upload(o.AbsPath, destFile)
if err != nil {
return nil, err
}
return &UploadResult{
Start: startTime,
Stop: time.Now(),
Method: UploadMethodFilecopy,
Output: o,
URL: url,
}, nil
}
func (u *Uploader) S3(o *OutputFile) (*UploadResult, error) {
startTime := time.Now()
url, err := u.s3client.Upload(o.AbsPath, o.UploadsS3.Bucket, o.UploadsS3.Key)
if err != nil {
return nil, err
}
return &UploadResult{
Start: startTime,
Stop: time.Now(),
Method: UploadMethodFilecopy,
Output: o,
URL: url,
}, nil
}