github.com/jenkins-x/jx/v2@v2.1.155/pkg/collector/bucket_collector.go (about)

     1  package collector
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/jenkins-x/jx/v2/pkg/cloud/buckets"
    10  	"github.com/jenkins-x/jx/v2/pkg/util"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  // BucketCollector stores the state for the git collector
    15  type BucketCollector struct {
    16  	Timeout time.Duration
    17  
    18  	bucketURL  string
    19  	classifier string
    20  	provider   buckets.Provider
    21  }
    22  
    23  // NewBucketCollector creates a new git based collector
    24  func NewBucketCollector(bucketURL string, classifier string, provider buckets.Provider) (Collector, error) {
    25  	return &BucketCollector{
    26  		Timeout:    time.Second * 20,
    27  		classifier: classifier,
    28  		provider:   provider,
    29  		bucketURL:  bucketURL,
    30  	}, nil
    31  }
    32  
    33  // CollectFiles collects files and returns the URLs
    34  func (c *BucketCollector) CollectFiles(patterns []string, outputPath string, basedir string) ([]string, error) {
    35  	urls := []string{}
    36  	for _, p := range patterns {
    37  		fn := func(name string) error {
    38  			var err error
    39  			toName := name
    40  			if basedir != "" {
    41  				toName, err = filepath.Rel(basedir, name)
    42  				if err != nil {
    43  					return errors.Wrapf(err, "failed to remove basedir %s from %s", basedir, name)
    44  				}
    45  			}
    46  			if outputPath != "" {
    47  				toName = filepath.Join(outputPath, toName)
    48  			}
    49  			f, err := os.Open(name)
    50  			if err != nil {
    51  				return errors.Wrapf(err, "failed to read file %s", name)
    52  			}
    53  			defer f.Close()
    54  			url, err := c.provider.UploadFileToBucket(f, toName, c.bucketURL)
    55  			if err != nil {
    56  				return err
    57  			}
    58  			urls = append(urls, url)
    59  			return nil
    60  		}
    61  
    62  		err := util.GlobAllFiles("", p, fn)
    63  		if err != nil {
    64  			return urls, err
    65  		}
    66  	}
    67  	return urls, nil
    68  }
    69  
    70  // CollectData collects the data storing it at the given output path and returning the URL to access it
    71  func (c *BucketCollector) CollectData(data io.Reader, outputName string) (string, error) {
    72  	url, err := c.provider.UploadFileToBucket(data, outputName, c.bucketURL)
    73  	if err != nil {
    74  		return "", err
    75  	}
    76  	return url, nil
    77  }