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: Allow copying local directories instead of using symlinks #478

Open
wants to merge 2 commits 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
43 changes: 43 additions & 0 deletions get_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ func TestFileGetter(t *testing.T) {
}
}

func TestFileGetter_Copy(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)

g.Copy = true

// With a dir that doesn't exist
if err := g.Get(dst, testModuleURL("basic")); err != nil {
t.Fatalf("err: %s", err)
}

// Verify the destination folder is not a symlink
fi, err := os.Lstat(dst)
if err != nil {
t.Fatalf("err: %s", err)
}
if fi.Mode()&os.ModeSymlink == 1 {
t.Fatal("destination is a symlink")
}

// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestFileGetter_sourceFile(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
Expand Down Expand Up @@ -73,6 +100,22 @@ func TestFileGetter_dir(t *testing.T) {
}
}

func TestFileGetter_dir_Copy(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)

g.Copy = true

if err := os.MkdirAll(dst, 0755); err != nil {
t.Fatalf("err: %s", err)
}

// With a dir that exists that isn't a symlink
if err := g.Get(dst, testModuleURL("basic")); err != nil {
t.Fatal("should not error")
}
}

func TestFileGetter_dirSymlink(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
Expand Down
21 changes: 18 additions & 3 deletions get_file_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package getter
Expand Down Expand Up @@ -30,12 +31,12 @@ func (g *FileGetter) Get(dst string, u *url.URL) error {
// If the destination already exists, it must be a symlink
if err == nil {
mode := fi.Mode()
if mode&os.ModeSymlink == 0 {
if mode&os.ModeSymlink == 0 && !g.Copy {
return fmt.Errorf("destination exists and is not a symlink")
}

// Remove the destination
if err := os.Remove(dst); err != nil {
if err := os.RemoveAll(dst); err != nil {
return err
}
}
Expand All @@ -45,7 +46,21 @@ func (g *FileGetter) Get(dst string, u *url.URL) error {
return err
}

return os.Symlink(path, dst)
if !g.Copy {
return os.Symlink(path, dst)
}

if err := os.Mkdir(dst, g.client.mode(0755)); err != nil {
return err
}

var disableSymlinks bool

if g.client != nil && g.client.DisableSymlinks {
disableSymlinks = true
}

return copyDir(g.Context(), dst, path, false, disableSymlinks, g.client.umask())
}

func (g *FileGetter) GetFile(dst string, u *url.URL) error {
Expand Down
29 changes: 21 additions & 8 deletions get_file_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package getter
Expand Down Expand Up @@ -34,12 +35,12 @@ func (g *FileGetter) Get(dst string, u *url.URL) error {
// If the destination already exists, it must be a symlink
if err == nil {
mode := fi.Mode()
if mode&os.ModeSymlink == 0 {
if mode&os.ModeSymlink == 0 && !g.Copy {
return fmt.Errorf("destination exists and is not a symlink")
}

// Remove the destination
if err := os.Remove(dst); err != nil {
if err := os.RemoveAll(dst); err != nil {
return err
}
}
Expand All @@ -49,15 +50,27 @@ func (g *FileGetter) Get(dst string, u *url.URL) error {
return err
}

sourcePath := toBackslash(path)
if !g.Copy {
sourcePath := toBackslash(path)

// Use mklink to create a junction point
output, err := exec.CommandContext(ctx, "cmd", "/c", "mklink", "/J", dst, sourcePath).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run mklink %v %v: %v %q", dst, sourcePath, err, output)
}
}

if err := os.Mkdir(dst, g.client.mode(0755)); err != nil {
return err
}

var disableSymlinks bool

// Use mklink to create a junction point
output, err := exec.CommandContext(ctx, "cmd", "/c", "mklink", "/J", dst, sourcePath).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run mklink %v %v: %v %q", dst, sourcePath, err, output)
if g.client != nil && g.client.DisableSymlinks {
disableSymlinks = true
}

return nil
return copyDir(g.Context(), dst, path, false, disableSymlinks, g.client.umask())
}

func (g *FileGetter) GetFile(dst string, u *url.URL) error {
Expand Down