func()

in command-runner/internal/containers/docker/docker_run.go [410:447]


func (cr *dockerContainer) copyIn(containerPath string, hostPath string, useGitIgnore bool) common.Executor {
	return func(ctx context.Context) error {
		log.Ctx(ctx).Debug().Msgf("Writing %s from %s", containerPath, hostPath)
		var tarFile *os.File
		if fs, err := os.Stat(hostPath); err != nil {
			return fmt.Errorf("unable to copyDir from srcPath=%s: %w", hostPath, err)
		} else if filepath.Ext(hostPath) == ".tar" {
			tarFile, err = os.Open(hostPath)
			if err != nil {
				return err
			}
			defer tarFile.Close()
		} else if fs.IsDir() {
			tarFile, err = shared.TarDirectory(ctx, hostPath, containerPath[1:], useGitIgnore, cr.UID, cr.GID)
			if tarFile != nil {
				defer func(tarFile *os.File) {
					if err := tarFile.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
						log.Ctx(ctx).Err(err)
					}
					if err := os.Remove(tarFile.Name()); err != nil {
						log.Ctx(ctx).Err(err)
					}
				}(tarFile)
			}
			if err != nil {
				return err
			}
		} else {
			return fmt.Errorf("unsupported hostPath=%s", hostPath)
		}

		log.Ctx(ctx).Printf("Extracting content from '%s' to '%s'", tarFile.Name(), containerPath)
		if err := cr.cli.CopyToContainer(ctx, cr.id, "/", tarFile, container.CopyToContainerOptions{}); err != nil {
			return fmt.Errorf("failed to copy content to container: %w", err)
		}
		return nil
	}
}