kubesphere.io/s2irun@v3.2.1+incompatible/pkg/scm/downloaders/file/download.go (about)

     1  package file
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/kubesphere/s2irun/pkg/api"
     9  	"github.com/kubesphere/s2irun/pkg/api/constants"
    10  	"github.com/kubesphere/s2irun/pkg/scm/git"
    11  	"github.com/kubesphere/s2irun/pkg/utils/fs"
    12  	utilglog "github.com/kubesphere/s2irun/pkg/utils/glog"
    13  )
    14  
    15  var glog = utilglog.StderrLog
    16  
    17  // RecursiveCopyError indicates a copy operation failed because the destination is within the copy's source tree.
    18  type RecursiveCopyError struct {
    19  	error
    20  }
    21  
    22  // File represents a simplest possible Downloader implementation where the
    23  // sources are just copied from local directory.
    24  type File struct {
    25  	fs.FileSystem
    26  }
    27  
    28  // Download copies sources from a local directory into the working directory.
    29  // Caller guarantees that config.Source.IsLocal() is true.
    30  func (f *File) Download(config *api.Config) (*git.SourceInfo, error) {
    31  	config.WorkingSourceDir = filepath.Join(config.WorkingDir, constants.Source)
    32  
    33  	copySrc := config.Source.LocalPath()
    34  	if len(config.ContextDir) > 0 {
    35  		copySrc = filepath.Join(copySrc, config.ContextDir)
    36  	}
    37  
    38  	glog.V(1).Infof("Copying sources from %q to %q", copySrc, config.WorkingSourceDir)
    39  	if strings.HasPrefix(filepath.Clean(config.WorkingSourceDir), filepath.Clean(copySrc)) {
    40  		return nil, RecursiveCopyError{error: fmt.Errorf("recursive copy requested, source directory %q contains the target directory %q", copySrc, config.WorkingSourceDir)}
    41  	}
    42  	if copySrc != config.WorkingSourceDir {
    43  		f.KeepSymlinks(config.KeepSymlinks)
    44  		err := f.CopyContents(copySrc, config.WorkingSourceDir)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  	}
    49  
    50  	return &git.SourceInfo{
    51  		Location:   config.Source.LocalPath(),
    52  		ContextDir: config.ContextDir,
    53  	}, nil
    54  }