Skip to content

Commit

Permalink
Merge pull request #120 from gistella/decompress-root-dir
Browse files Browse the repository at this point in the history
Create the root dir if not included in an archive
  • Loading branch information
gmeghnag authored Nov 10, 2023
2 parents 806c2f4 + a196fc7 commit fc4fda9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/use/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ func ExtractTarStream(st io.Reader,destinationdir string) (string,error) {
}
}
case tar.TypeReg:
// Root dir is not part of the archive
if mgRootDir == "" {
mgRootDir = filepath.Join(destinationdir, filepath.Dir(header.Name))
firstDirectory = true
err := os.MkdirAll(mgRootDir, os.ModePerm)
if err != nil && !os.IsExist(err) {
return "", err
}
}
outpath := filepath.Join(destinationdir, header.Name)
if _, err := os.Stat(outpath); ! os.IsNotExist(err) {
fmt.Fprintln(os.Stderr,"create file failed extracting tar: file already exists")
Expand Down Expand Up @@ -329,6 +338,16 @@ func ExtractZip(zipfile string,destinationdir string) (string,error) {
for _, f := range archive.File {
filePath := filepath.Join(destinationdir, f.Name)

// Root dir is not part of the archive
if !f.FileInfo().IsDir() && mgRootDir == "" {
mgRootDir = filepath.Dir(filePath)
firstDirectory = true
err := os.MkdirAll(mgRootDir, os.ModePerm)
if err != nil && !os.IsExist(err) {
return "", err
}
}

if f.FileInfo().IsDir() {
if (!firstDirectory) {
firstDirectory = true
Expand All @@ -339,7 +358,7 @@ func ExtractZip(zipfile string,destinationdir string) (string,error) {
fmt.Fprintln(os.Stderr,"error: cannot create directory "+filePath+": "+err.Error())
return "",err
}
} else {
} else {
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
fmt.Fprintln(os.Stderr,"error: cannot create file "+filePath+": "+err.Error())
Expand Down
37 changes: 37 additions & 0 deletions cmd/use/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package use

import (
"os"
"testing"
)

func TestDecompression(t *testing.T) {
want := "testdata/must-gather.sample"
tests := map[string]func(string, string) (string, error){
"testdata/must-gather.zip": ExtractZip,
"testdata/must-gather.tar": ExtractTar,
"testdata/must-gather.tar.gz": ExtractTarGz,
}

for path, f := range tests {
mgRootDir, err := f(path, "testdata")
if err != nil {
t.Error(err)
}
defer clearTestFiles(t)

if want != mgRootDir {
t.Errorf("expected %q, got %q", want, mgRootDir)
}
clearTestFiles(t)
}
}

func clearTestFiles(t *testing.T) {
t.Helper()

err := os.RemoveAll("testdata/must-gather.sample")
if err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions cmd/use/testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
must-gather.sample
Binary file added cmd/use/testdata/must-gather.tar
Binary file not shown.
Binary file added cmd/use/testdata/must-gather.tar.gz
Binary file not shown.
Binary file added cmd/use/testdata/must-gather.zip
Binary file not shown.

0 comments on commit fc4fda9

Please sign in to comment.