Skip to content

Commit

Permalink
feat(import): allow dest dir renaming during import
Browse files Browse the repository at this point in the history
import:
    - path: folder1
    dest: /folder2

will cause contents of folder1 to show up as and under /folder2

Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
  • Loading branch information
rchincha committed Nov 2, 2023
1 parent faa7772 commit 91ed8ea
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
22 changes: 17 additions & 5 deletions pkg/stacker/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ func importFile(imp string, cacheDir string, hash string, idest string, mode *fs

var dest string
if imp[len(imp)-1:] != "/" {
dest = path.Join(cacheDir, path.Base(imp))
if idest != "" && path.Base(imp) != path.Base(idest) {
dest = path.Join(cacheDir, path.Base(idest))
} else {
dest = path.Join(cacheDir, path.Base(imp))
}
} else {
dest = cacheDir
}

Check warning on line 136 in pkg/stacker/import.go

View check run for this annotation

Codecov / codecov/patch

pkg/stacker/import.go#L127-L136

Added lines #L127 - L136 were not covered by tests
Expand Down Expand Up @@ -163,10 +167,18 @@ func importFile(imp string, cacheDir string, hash string, idest string, mode *fs
case mtree.Extra:
srcpath := path.Join(imp, d.Path())
var destpath string
if imp[len(imp)-1:] == "/" {
destpath = path.Join(cacheDir, d.Path())
if imp[len(imp)-1:] != "/" {
if idest != "" && path.Base(imp) != path.Base(idest) {
if idest[len(idest)-1:] != "/" {
destpath = path.Join(cacheDir, path.Base(idest), d.Path())
} else {
destpath = path.Join(cacheDir, path.Base(imp), d.Path())
}
} else {
destpath = path.Join(cacheDir, path.Base(imp), d.Path())
}
} else {
destpath = path.Join(cacheDir, path.Base(imp), d.Path())
destpath = path.Join(cacheDir, d.Path())
}

Check warning on line 182 in pkg/stacker/import.go

View check run for this annotation

Codecov / codecov/patch

pkg/stacker/import.go#L169-L182

Added lines #L169 - L182 were not covered by tests

if d.New().IsDir() {
Expand Down Expand Up @@ -360,7 +372,7 @@ func Import(c types.StackerConfig, storage types.Storage, name string, imports t

dest := i.Dest

if i.Dest[len(i.Dest)-1:] != "/" {
if i.Dest[len(i.Dest)-1:] != "/" && i.Path[len(i.Path)-1:] != "/" {

Check warning on line 375 in pkg/stacker/import.go

View check run for this annotation

Codecov / codecov/patch

pkg/stacker/import.go#L375

Added line #L375 was not covered by tests
dest = path.Dir(i.Dest)
}

Expand Down
57 changes: 54 additions & 3 deletions test/import.bats
Original file line number Diff line number Diff line change
Expand Up @@ -450,19 +450,70 @@ fifth:
- path: folder1/subfolder2/
dest: /folder3/
- path: folder1/subfolder2
dest: /folder4/
dest: /folder4
- path: stacker://fourth/folder4/subfolder5/
dest: /folder6/
- path: stacker://fourth/folder4/
dest: /folder7/
run: |
ls -l /
ls -l /folder*
[ -f /folder3/subfile1 ]
[ ! -e /folder3/subfolder2 ]
[ -f /folder4/subfolder2/subfile1 ]
[ -f /folder4/subfile1 ]
[ -f /folder6/subfile6 ]
[ ! -e /folder6/subfolder5 ]
[ -f /folder7/subfolder5/subfile6 ]
EOF
stacker build
}

@test "dir path behavior" {
mkdir -p folder1
touch folder1/file1
mkdir -p folder1/subfolder2
touch folder1/subfolder2/subfile1
cat > stacker.yaml <<EOF
src_folder_dest_non_existent_folder_case1:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1
dest: /folder2
run: |
[ -f /folder2/file1 ]
src_folder_dest_non_existent_folder_case2:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1/
dest: /folder2
run: |
[ -f /folder2/file1 ]
src_folder_dest_non_existent_folder_case3:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1
dest: /folder2/
run: |
ls -al /
ls -al /folder2
[ -f /folder2/folder1/file1 ]
src_folder_dest_non_existent_folder_case4:
from:
type: docker
url: docker://ubuntu:latest
import:
- path: folder1/
dest: /folder2/
run: |
[ -f /folder2/file1 ]
EOF
stacker build
}

0 comments on commit 91ed8ea

Please sign in to comment.