github.com/replicatedhq/ship@v0.55.0/pkg/specs/localgetter/local_getter.go (about)

     1  package localgetter
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/go-kit/kit/log"
     9  	"github.com/go-kit/kit/log/level"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/afero"
    12  )
    13  
    14  type LocalGetter struct {
    15  	Logger log.Logger
    16  	FS     afero.Afero
    17  }
    18  
    19  func (g *LocalGetter) GetFiles(ctx context.Context, upstream, savePath string) (string, error) {
    20  	debug := level.Debug(g.Logger)
    21  	debug.Log("event", "localgetter.GetFiles", "upstream", upstream, "savePath", savePath)
    22  
    23  	err := g.copyDir(ctx, upstream, savePath)
    24  	if err != nil {
    25  		return "", errors.Wrap(err, "copy files")
    26  	}
    27  	return savePath, nil
    28  }
    29  
    30  func (g *LocalGetter) copyDir(ctx context.Context, upstream, savePath string) error {
    31  	isDir, err := g.FS.IsDir(upstream)
    32  	if err != nil {
    33  		return errors.Wrapf(err, "check if %s is dir", upstream)
    34  	}
    35  	if !isDir {
    36  		// copy a single file
    37  		return g.copyFile(ctx, upstream, savePath, os.FileMode(777))
    38  	}
    39  
    40  	files, err := g.FS.ReadDir(upstream)
    41  	if err != nil {
    42  		return errors.Wrapf(err, "read files in dir %s", upstream)
    43  	}
    44  
    45  	for _, file := range files {
    46  		loopFile := filepath.Join(upstream, file.Name())
    47  		loopDest := filepath.Join(savePath, file.Name())
    48  		if file.IsDir() {
    49  			err = g.FS.MkdirAll(loopDest, file.Mode())
    50  			if err != nil {
    51  				return errors.Wrapf(err, "create dest dir %s", loopDest)
    52  			}
    53  
    54  			err = g.copyDir(ctx, loopFile, loopDest)
    55  			if err != nil {
    56  				return errors.Wrapf(err, "copy dir %s", file.Name())
    57  			}
    58  		} else {
    59  			err = g.copyFile(ctx, loopFile, loopDest, file.Mode())
    60  			if err != nil {
    61  				return errors.Wrapf(err, "copy file %s", file.Name())
    62  			}
    63  		}
    64  	}
    65  	return nil
    66  }
    67  
    68  func (g *LocalGetter) copyFile(ctx context.Context, upstream, savePath string, mode os.FileMode) error {
    69  	saveDir := filepath.Dir(savePath)
    70  	exists, err := g.FS.Exists(saveDir)
    71  	if err != nil {
    72  		return errors.Wrapf(err, "determine if path %s exists", saveDir)
    73  	}
    74  	if !exists {
    75  		err = g.FS.MkdirAll(saveDir, os.ModePerm)
    76  		if err != nil {
    77  			return errors.Wrapf(err, "create dest dir %s", saveDir)
    78  		}
    79  	}
    80  
    81  	contents, err := g.FS.ReadFile(upstream)
    82  	if err != nil {
    83  		return errors.Wrapf(err, "read %s file contents", upstream)
    84  	}
    85  
    86  	err = g.FS.WriteFile(savePath, contents, mode)
    87  	return errors.Wrapf(err, "write %s file contents", savePath)
    88  }
    89  
    90  func IsLocalFile(FS *afero.Afero, upstream string) bool {
    91  	exists, err := FS.Exists(upstream)
    92  	if err != nil {
    93  		return false
    94  	}
    95  	return exists
    96  }