mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
config/module: Get can support subdirs
This commit is contained in:
parent
615192a6c4
commit
c91fd76fe8
@ -4,6 +4,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// copyDir copies the src directory contents into dst. Both directories
|
||||
@ -14,7 +15,13 @@ func copyDir(dst, src string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
dstPath := filepath.Join(dst, filepath.Base(path))
|
||||
basePath := filepath.Base(path)
|
||||
if strings.HasPrefix(basePath, ".") {
|
||||
// Skip any dot files
|
||||
return nil
|
||||
}
|
||||
|
||||
dstPath := filepath.Join(dst, basePath)
|
||||
|
||||
// If we have a directory, make that subdirectory, then continue
|
||||
// the walk.
|
||||
|
@ -3,8 +3,11 @@ package module
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
@ -51,6 +54,21 @@ func Get(dst, src string) error {
|
||||
var force string
|
||||
force, src = getForcedGetter(src)
|
||||
|
||||
// If there is a subdir component, then we download the root separately
|
||||
// and then copy over the proper subdir.
|
||||
var realDst string
|
||||
src, subDir := getDirSubdir(src)
|
||||
if subDir != "" {
|
||||
tmpDir, err := ioutil.TempDir("", "tf")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
realDst = dst
|
||||
dst = subDir
|
||||
}
|
||||
|
||||
u, err := url.Parse(src)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -68,9 +86,22 @@ func Get(dst, src string) error {
|
||||
err = g.Get(dst, u)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("error downloading module '%s': %s", src, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
// If we have a subdir, copy that over
|
||||
if subDir != "" {
|
||||
if err := os.RemoveAll(realDst); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(realDst, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return copyDir(realDst, filepath.Join(dst, subDir))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getRunCommand is a helper that will run a command and capture the output
|
||||
|
@ -46,6 +46,20 @@ func TestGet_fileForced(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet_fileSubdir(t *testing.T) {
|
||||
dst := tempDir(t)
|
||||
u := testModule("basic//subdir")
|
||||
|
||||
if err := Get(dst, u); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
mainPath := filepath.Join(dst, "sub.tf")
|
||||
if _, err := os.Stat(mainPath); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDirSubdir(t *testing.T) {
|
||||
cases := []struct {
|
||||
Input string
|
||||
|
Loading…
Reference in New Issue
Block a user