Skip to content

Commit

Permalink
feat(import): copy dir contents
Browse files Browse the repository at this point in the history
  import:
    - path: folder1/
      dest: /

vs

  import:
    - path: folder1
      dest: /

The former will copy the contents of folder1/ at / and the latter will
make and copy a /folder1 at /

Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
  • Loading branch information
rchincha committed Apr 3, 2023
1 parent 479fca8 commit ea14811
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/stacker/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func importFile(imp string, cacheDir string, hash string, idest string, mode *fs
}

dest := path.Join(cacheDir, path.Base(imp))

if err := os.MkdirAll(dest, 0755); err != nil {
return "", errors.Wrapf(err, "failed making cache dir")
}
Expand Down Expand Up @@ -156,7 +157,12 @@ func importFile(imp string, cacheDir string, hash string, idest string, mode *fs
fallthrough
case mtree.Extra:
srcpath := path.Join(imp, d.Path())
destpath := path.Join(cacheDir, path.Base(imp), d.Path())
var destpath string
if imp[len(imp)-1:] == "/" {
destpath = path.Join(cacheDir, d.Path())
} else {
destpath = path.Join(cacheDir, path.Base(imp), d.Path())
}

if d.New().IsDir() {
fi, err := os.Lstat(destpath)
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ func (l Layer) absolutify(referenceDirectory string) (Layer, error) {
if err != nil {
return ret, err
}
if rawImport.Path[len(rawImport.Path)-1:] == "/" {
absImportPath += "/"
}
absImport := Import{Hash: rawImport.Hash, Path: absImportPath, Dest: rawImport.Dest, Mode: rawImport.Mode, Uid: rawImport.Uid, Gid: rawImport.Gid}
ret.Imports = append(ret.Imports, absImport)
}
Expand Down
18 changes: 18 additions & 0 deletions test/import.bats
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,21 @@ EOF

stacker build
}

@test "import with dir contents" {
mkdir folder1
touch folder1/file1
cat > stacker.yaml <<EOF
first:
from:
type: oci
url: $CENTOS_OCI
import:
- path: folder1/
dest: /
run: |
ls -l /
[ -f /file1 ]
EOF
stacker build
}

0 comments on commit ea14811

Please sign in to comment.