github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/loader/loader.go (about)

     1  package loader
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"os"
    10  	"path"
    11  
    12  	"github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  type loader struct {
    17  	temporaryDir string
    18  
    19  	// for testing
    20  	osRemoveAllFunc func(string) error
    21  	osCreateFunc    func(name string) (*os.File, error)
    22  	httpGetFunc     func(url string) (*http.Response, error)
    23  	ioutilTempDir   func(dir, prefix string) (string, error)
    24  }
    25  
    26  //go:generate mockery -name=Loader -output=automock -outpkg=automock -case=underscore
    27  type Loader interface {
    28  	Load(src, assetName string, mode v1alpha2.AssetMode, filter string) (string, []string, error)
    29  	Clean(path string) error
    30  }
    31  
    32  func New(temporaryDir string, verifySSL bool) Loader {
    33  	if len(temporaryDir) == 0 {
    34  		temporaryDir = os.TempDir()
    35  	}
    36  
    37  	if !verifySSL {
    38  		cfg := &tls.Config{
    39  			InsecureSkipVerify: true,
    40  		}
    41  		http.DefaultClient.Transport = &http.Transport{
    42  			TLSClientConfig: cfg,
    43  		}
    44  	}
    45  
    46  	return &loader{
    47  		temporaryDir:    temporaryDir,
    48  		osRemoveAllFunc: os.RemoveAll,
    49  		osCreateFunc:    os.Create,
    50  		httpGetFunc:     http.Get,
    51  		ioutilTempDir:   ioutil.TempDir,
    52  	}
    53  }
    54  
    55  func (l *loader) Load(src, assetName string, mode v1alpha2.AssetMode, filter string) (string, []string, error) {
    56  	switch mode {
    57  	case v1alpha2.AssetSingle:
    58  		return l.loadSingle(src, assetName)
    59  	case v1alpha2.AssetPackage:
    60  		return l.loadPackage(src, assetName, filter)
    61  	}
    62  
    63  	return "", nil, fmt.Errorf("not supported source mode %+v", mode)
    64  }
    65  
    66  func (l *loader) Clean(path string) error {
    67  	return l.osRemoveAllFunc(path)
    68  }
    69  
    70  func (l *loader) download(destination, source string) error {
    71  	file, err := l.osCreateFunc(destination)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	defer file.Close()
    76  
    77  	response, err := l.httpGetFunc(source)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	defer response.Body.Close()
    82  
    83  	if response.StatusCode < 200 || response.StatusCode > 299 {
    84  		return errors.New(response.Status)
    85  	}
    86  
    87  	_, err = io.Copy(file, response.Body)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func (l *loader) fileName(source string) string {
    96  	_, filename := path.Split(source)
    97  	if len(filename) == 0 {
    98  		filename = "asset"
    99  	}
   100  
   101  	return filename
   102  }