Skip to content

Commit

Permalink
Merge pull request #168 from mtulio/support-xz
Browse files Browse the repository at this point in the history
Support archive/must-gather files compressed with XZ
  • Loading branch information
gmeghnag authored Jul 8, 2024
2 parents c89bb12 + 15b955d commit 63cfc53
Show file tree
Hide file tree
Showing 55 changed files with 7,607 additions and 35 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)
}
4 changes: 3 additions & 1 deletion cmd/use/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package use

import (
"os"
"path/filepath"
"testing"
)

Expand All @@ -11,6 +12,7 @@ func TestDecompression(t *testing.T) {
"testdata/must-gather.zip": ExtractZip,
"testdata/must-gather.tar": ExtractTar,
"testdata/must-gather.tar.gz": ExtractTarGz,
"testdata/must-gather.tar.xz": extractTarXZ,
}

for path, f := range tests {
Expand All @@ -20,7 +22,7 @@ func TestDecompression(t *testing.T) {
}
defer clearTestFiles(t)

if want != mgRootDir {
if filepath.Clean(want) != filepath.Clean(mgRootDir) {
t.Errorf("expected %q, got %q", want, mgRootDir)
}
clearTestFiles(t)
Expand Down
Binary file added cmd/use/testdata/must-gather.tar.xz
Binary file not shown.
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/schollz/progressbar/v3 v3.13.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.13.0
github.com/ulikunitz/xz v0.5.11
github.com/vincent-petithory/dataurl v1.0.0
go.etcd.io/etcd/api/v3 v3.5.9
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -142,7 +143,7 @@ require (
k8s.io/apiserver v0.28.5 // indirect
k8s.io/component-base v0.28.5 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230711102312-30195339c3c7 // indirect
k8s.io/utils v0.0.0-20230711102312-30195339c3c7
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/vincent-petithory/dataurl v0.0.0-20160330182126-9a301d65acbb/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U=
github.com/vincent-petithory/dataurl v1.0.0 h1:cXw+kPto8NLuJtlMsI152irrVw9fRDX8AbShPRpg2CI=
github.com/vincent-petithory/dataurl v1.0.0/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U=
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/ulikunitz/xz/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/ulikunitz/xz/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions vendor/github.com/ulikunitz/xz/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/ulikunitz/xz/SECURITY.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 63cfc53

Please sign in to comment.