MCPcopy
hub / github.com/nektos/act / copyDir

Method copyDir

pkg/container/docker_run.go:738–807  ·  view source on GitHub ↗
(dstPath string, srcPath string, useGitIgnore bool)

Source from the content-addressed store, hash-verified

736}
737
738func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgnore bool) common.Executor {
739 return func(ctx context.Context) error {
740 logger := common.Logger(ctx)
741 tarFile, err := os.CreateTemp("", "act")
742 if err != nil {
743 return err
744 }
745 logger.Debugf("Writing tarball %s from %s", tarFile.Name(), srcPath)
746 defer func(tarFile *os.File) {
747 name := tarFile.Name()
748 err := tarFile.Close()
749 if !errors.Is(err, os.ErrClosed) {
750 logger.Error(err)
751 }
752 err = os.Remove(name)
753 if err != nil {
754 logger.Error(err)
755 }
756 }(tarFile)
757 tw := tar.NewWriter(tarFile)
758
759 srcPrefix := filepath.Dir(srcPath)
760 if !strings.HasSuffix(srcPrefix, string(filepath.Separator)) {
761 srcPrefix += string(filepath.Separator)
762 }
763 logger.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
764
765 var ignorer gitignore.Matcher
766 if useGitIgnore {
767 ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
768 if err != nil {
769 logger.Debugf("Error loading .gitignore: %v", err)
770 }
771
772 ignorer = gitignore.NewMatcher(ps)
773 }
774
775 fc := &filecollector.FileCollector{
776 Fs: &filecollector.DefaultFs{},
777 Ignorer: ignorer,
778 SrcPath: srcPath,
779 SrcPrefix: srcPrefix,
780 Handler: &filecollector.TarCollector{
781 TarWriter: tw,
782 UID: cr.UID,
783 GID: cr.GID,
784 DstDir: dstPath[1:],
785 },
786 }
787
788 err = filepath.Walk(srcPath, fc.CollectFiles(ctx, []string{}))
789 if err != nil {
790 return err
791 }
792 if err := tw.Close(); err != nil {
793 return err
794 }
795

Callers 1

CopyDirMethod · 0.95

Calls 8

CollectFilesMethod · 0.95
LoggerFunction · 0.92
NameMethod · 0.80
CopyToContainerMethod · 0.80
CloseMethod · 0.65
RemoveMethod · 0.65
WalkMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected