Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for file path #1141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions environment/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type ContainerEngine interface {
InspectImage(image string) (dockertypes.ImageInspect, error)
// TODO: Change paths from map to array
CopyDirsIntoImage(image, newImageName string, paths map[string]string) (err error)
CopyDirsIntoContainer(containerID string, paths map[string]string) (err error)
CopyDirsFromContainer(containerID string, paths map[string]string) (err error)
CopyDataIntoContainer(containerID string, paths map[string]string) (err error)
CopyDataFromContainer(containerID string, paths map[string]string) (err error)
BuildImage(image, context, dockerfile string) (err error)
RemoveImage(image string) (err error)
CreateContainer(container environmenttypes.Container) (containerid string, err error)
Expand Down
45 changes: 35 additions & 10 deletions environment/container/dockerengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"bytes"
"context"
"fmt"
"os"
"io"
"io/fs"
"path/filepath"
"strings"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -231,14 +233,26 @@ func (e *dockerEngine) CopyDirsIntoImage(image, newImageName string, paths map[s
return nil
}

// CopyDirsIntoContainer copies some directories into a container
func (e *dockerEngine) CopyDirsIntoContainer(containerID string, paths map[string]string) (err error) {
// CopyDataIntoContainer copies files or directories into a container
func (e *dockerEngine) CopyDataIntoContainer(containerID string, paths map[string]string) (err error) {
for sp, dp := range paths {
err = copyDirToContainer(e.ctx, e.cli, containerID, sp, dp)
fi, err := os.Stat(sp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation. Use go fmt.
If you are using a text editor like VSCode, VIM, Sublime, etc. installing the Golang extension automatically runs the go formatter when you save the file.

if err != nil {
return fmt.Errorf("container data copy failed for image '%s' with volume %s:%s . Error: %w", containerID, sp, dp, err)
return fmt.Errorf("failed to stat the source path '%s' . Error: %w", sp, err)
}
}
if fi.Mode().IsDir() {
err = copyDirToContainer(e.ctx, e.cli, containerID, sp, dp)
if err != nil {
return fmt.Errorf("container data copy failed for image '%s' with volume %s:%s . Error: %w", containerID, sp, dp, err)
}
} else {
newDestPath := filepath.Join(dp, fi.Name())
err = copyDirToContainer(e.ctx, e.cli, containerID, sp, newDestPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If copyDirToContainer is being used to copy files then we should rename it as well.

if err != nil {
return fmt.Errorf("container data copy failed for image '%s' with volume %s:%s . Error: %w", containerID, sp, newDestPath, err)
}
}
}
return nil
}

Expand All @@ -253,12 +267,23 @@ func (e *dockerEngine) Stat(containerID string, name string) (fs.FileInfo, error
}, err
}

// CopyDirsFromContainer copies a directory from inside the container
func (e *dockerEngine) CopyDirsFromContainer(containerID string, paths map[string]string) (err error) {
for sp, dp := range paths {
if err := copyFromContainer(e.ctx, e.cli, containerID, sp, dp); err != nil {
return fmt.Errorf("failed to copy data from the container with ID '%s' from source path '%s' to destination path '%s' . Error: %w", containerID, sp, dp, err)
// CopyDataFromContainer copies files or directories from inside the container
func (e *dockerEngine) CopyDataFromContainer(containerID string, paths map[string]string) (err error) {
for sp, dp := range paths {
fi, err := os.Stat(sp)
if err != nil {
return fmt.Errorf("failed to stat the source path '%s' . Error: %w", sp, err)
}
if fi.Mode().IsDir() {
if err := copyFromContainer(e.ctx, e.cli, containerID, sp, dp); err != nil {
return fmt.Errorf("failed to copy data from the container with ID '%s' from source path '%s' to destination path '%s' . Error: %w", containerID, sp, dp, err)
}
} else {
newDestPath := filepath.Join(dp, fi.Name())
if err := copyFromContainer(e.ctx, e.cli, containerID, sp, newDestPath); err != nil {
return fmt.Errorf("failed to copy data from the container with ID '%s' from source path '%s' to destination path '%s' . Error: %w", containerID, sp, newDestPath, err)
}
}
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions environment/container/dockerengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func TestIsBuilderAvailable(t *testing.T) {
paths[absDir1] = "/dir1"
paths[absDir2] = "/dir2"

err = provider.CopyDirsIntoContainer(containerID, paths)
err = provider.CopyDataIntoContainer(containerID, paths)
if err != nil {
t.Fatalf("failed to copy dirs into iamge '%s' . Error: %q", image, err)
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestIsBuilderAvailable(t *testing.T) {
paths[absDir1] = "/dir1"
paths[absDir2] = "/dir2"

err = provider.CopyDirsIntoContainer(containerID, paths)
err = provider.CopyDataIntoContainer(containerID, paths)
if err != nil {
t.Fatalf("failed to copy dirs into iamge '%s' . Error: %q", image, err)
}
Expand All @@ -383,7 +383,7 @@ func TestIsBuilderAvailable(t *testing.T) {
paths["/dir1"] = tempDir
paths["/dir2"] = tempDir

err = provider.CopyDirsFromContainer(containerID, paths)
err = provider.CopyDataFromContainer(containerID, paths)
if err != nil {
t.Fatal("failed to copy Directories from Container ", err)
}
Expand Down
11 changes: 2 additions & 9 deletions environment/peercontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,6 @@ func (e *PeerContainer) Destroy() error {

// Download downloads the path to outside the environment
func (e *PeerContainer) Download(path string) (string, error) {
fileInfo, err := e.Stat(path)
if err != nil {
return path, fmt.Errorf("failed to stat the given path : %s. Error: %v", path, err)
}
if !fileInfo.IsDir() {
return path, fmt.Errorf("download only supports directory paths. The path provided is %s. Error: %v", path, err)
}
output, err := os.MkdirTemp(e.TempPath, "*")
if err != nil {
return path, fmt.Errorf("failed to create temp dir. Error: %w", err)
Expand All @@ -201,7 +194,7 @@ func (e *PeerContainer) Download(path string) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to get the container engine. Error: %w", err)
}
if err := cengine.CopyDirsFromContainer(e.ContainerInfo.ID, map[string]string{path: output}); err != nil {
if err := cengine.CopyDataFromContainer(e.ContainerInfo.ID, map[string]string{path: output}); err != nil {
return path, fmt.Errorf("failed to copy data from the container with ID '%s' . Error: %w", e.ContainerInfo.ID, err)
}
return output, nil
Expand All @@ -221,7 +214,7 @@ func (e *PeerContainer) Upload(outpath string) (string, error) {
if err != nil {
return envpath, fmt.Errorf("failed to get the container engine. Error: %w", err)
}
if err := cengine.CopyDirsIntoContainer(e.ContainerInfo.ID, map[string]string{outpath: envpath}); err != nil {
if err := cengine.CopyDataIntoContainer(e.ContainerInfo.ID, map[string]string{outpath: envpath}); err != nil {
return envpath, fmt.Errorf("failed to copy data into the container with ID '%s' . Error: %w", e.ContainerInfo.ID, err)
}
return envpath, nil
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/docker/cli v23.0.3+incompatible
github.com/docker/docker v23.0.3+incompatible
github.com/docker/libcompose v0.4.1-0.20171025083809-57bd716502dc
github.com/go-git/go-billy/v5 v5.3.1
github.com/go-git/go-billy/v5 v5.4.1
github.com/go-git/go-git/v5 v5.7.0
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.5.9
Expand Down Expand Up @@ -116,7 +116,6 @@ require (
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
Expand Down
Loading