/
bucket_collector.go
77 lines (69 loc) · 1.89 KB
/
bucket_collector.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 collector
import (
"io"
"os"
"path/filepath"
"time"
"github.com/jenkins-x/jx/v2/pkg/cloud/buckets"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
// BucketCollector stores the state for the git collector
type BucketCollector struct {
Timeout time.Duration
bucketURL string
classifier string
provider buckets.Provider
}
// NewBucketCollector creates a new git based collector
func NewBucketCollector(bucketURL string, classifier string, provider buckets.Provider) (Collector, error) {
return &BucketCollector{
Timeout: time.Second * 20,
classifier: classifier,
provider: provider,
bucketURL: bucketURL,
}, nil
}
// CollectFiles collects files and returns the URLs
func (c *BucketCollector) CollectFiles(patterns []string, outputPath string, basedir string) ([]string, error) {
urls := []string{}
for _, p := range patterns {
fn := func(name string) error {
var err error
toName := name
if basedir != "" {
toName, err = filepath.Rel(basedir, name)
if err != nil {
return errors.Wrapf(err, "failed to remove basedir %s from %s", basedir, name)
}
}
if outputPath != "" {
toName = filepath.Join(outputPath, toName)
}
f, err := os.Open(name)
if err != nil {
return errors.Wrapf(err, "failed to read file %s", name)
}
defer f.Close()
url, err := c.provider.UploadFileToBucket(f, toName, c.bucketURL)
if err != nil {
return err
}
urls = append(urls, url)
return nil
}
err := util.GlobAllFiles("", p, fn)
if err != nil {
return urls, err
}
}
return urls, nil
}
// CollectData collects the data storing it at the given output path and returning the URL to access it
func (c *BucketCollector) CollectData(data io.Reader, outputName string) (string, error) {
url, err := c.provider.UploadFileToBucket(data, outputName, c.bucketURL)
if err != nil {
return "", err
}
return url, nil
}