Skip to content

Commit

Permalink
xz-support: add support of XZ compressed must-gathers
Browse files Browse the repository at this point in the history
  • Loading branch information
mtulio committed Jul 6, 2024
1 parent b1aaa44 commit 15b955d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 33 deletions.
102 changes: 71 additions & 31 deletions cmd/use/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import (
"path/filepath"
"strings"
"time"

"github.com/ulikunitz/xz"
)

const (
fileTypeTar string = "tar"
fileTypeTarGzip string = "tar.gz"
fileTypeXZ string = "tar.xz"
fileTypeZip string = "zip"
)

func humanizeBytes(bytes int64) string {
Expand Down Expand Up @@ -102,11 +111,10 @@ func isTarFile(path string) (bool, error) {
return false, err
}
defer file.Close()

tarReader := tar.NewReader(file)
_, err = tarReader.Next()
if err != nil {
return false, nil
return false, fmt.Errorf("unable to read tarbal file: %w", err)
}

return true, nil
Expand All @@ -128,24 +136,48 @@ func isGzip(path string) (bool, error) {
return false, err
}

func IsCompressedFile(path string) (bool, error) {
result, err := isGzip(path)
func isXZ(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, err
} else if result == true {
return result, nil
}
result, err = isZip(path)
defer file.Close()

_, err = xz.NewReader(file)
if err != nil {
return false, err
} else if result == true {
return result, nil
}
return true, nil
}

func IsCompressedFile(path string) (bool, string, error) {
result, err := isGzip(path)
if err != nil {
return false, "", err
} else if result {
return result, fileTypeTarGzip, nil
}

result, err = isZip(path)
if err != nil {
return false, "", err
} else if result {
return result, fileTypeZip, nil
}

result, err = isXZ(path)
if err != nil {
return false, "", err
} else if result {
return result, fileTypeXZ, nil
}

result, err = isTarFile(path)
if err != nil {
return false, err
return false, "", err
}
return result, nil

return result, fileTypeTar, nil
}

func IsRemoteFile(path string) bool {
Expand Down Expand Up @@ -220,28 +252,22 @@ func CopyFile(path string, destinationfile string) error {
return err
}

func DecompressFile(path string, outpath string) (string, error) {
func DecompressFile(path string, outpath string, fileType string) (string, error) {
fmt.Println("decompressing file " + path + " in " + outpath)
var err error
var mgRootDir string = ""
result, err := isGzip(path)
if err == nil {
if result {
mgRootDir, err = ExtractTarGz(path, outpath)
} else {
result, err := isTarFile(path)
if err == nil {
if result {
mgRootDir, err = ExtractTar(path, outpath)
} else {
result, err := isZip(path)
if err == nil {
if result {
mgRootDir, err = ExtractZip(path, outpath)
}
}
}
}
}

switch fileType {
case fileTypeTar:
mgRootDir, err = ExtractTar(path, outpath)
case fileTypeTarGzip:
mgRootDir, err = ExtractTarGz(path, outpath)
case fileTypeXZ:
mgRootDir, err = extractTarXZ(path, outpath)
case fileTypeZip:
mgRootDir, err = ExtractZip(path, outpath)
default:
return "", fmt.Errorf("unable to decompress file: unknown file type %s", fileType)
}

return mgRootDir, err
Expand Down Expand Up @@ -397,3 +423,17 @@ func ExtractTarGz(gzipfile string, destinationdir string) (string, error) {
}
return ExtractTarStream(uncompressedStream, destinationdir)
}

func extractTarXZ(xzFile string, destinationdir string) (string, error) {
stream, err := os.Open(xzFile)
if err != nil {
return "", fmt.Errorf("error: cannot open %q: %w", xzFile, err)
}
defer stream.Close()

xzReader, err := xz.NewReader(stream)
if err != nil {
return "", fmt.Errorf("error: cannot uncompress xz file %q: %w", xzFile, err)
}
return ExtractTarStream(xzReader, destinationdir)
}
5 changes: 3 additions & 2 deletions cmd/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ var UseCmd = &cobra.Command{
var err error
idFlag, _ := cmd.Flags().GetString("id")
path := ""
fileType := ""
isCompressedFile := false
if len(args) == 0 && idFlag == "" {
MustGatherInfo()
Expand Down Expand Up @@ -205,7 +206,7 @@ var UseCmd = &cobra.Command{

isDir, _ := helpers.IsDirectory(path)
if !isDir {
isCompressedFile, _ = IsCompressedFile(path)
isCompressedFile, fileType, _ = IsCompressedFile(path)
if !isCompressedFile {
fmt.Fprintln(os.Stderr, "Error: "+path+" is not a directory not a compressed file.")
os.Exit(1)
Expand All @@ -215,7 +216,7 @@ var UseCmd = &cobra.Command{

if isCompressedFile {
outputpath := filepath.Dir(path)
rootfile, err := DecompressFile(path, outputpath)
rootfile, err := DecompressFile(path, outputpath, fileType)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: decompressing "+path+" in "+outputpath+": "+err.Error())
os.Exit(1)
Expand Down

0 comments on commit 15b955d

Please sign in to comment.