Skip to content

Commit

Permalink
replace the tar xzf command by gzip package functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Aug 20, 2020
1 parent 57c29a7 commit dadebc2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
9 changes: 0 additions & 9 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,3 @@ func AnsiblePlaybook(vmName, ip string, playbook string) *cmd {
},
}
}

// Tar flags are (cxzf)
// Temporary, use go code instead
func Tar(args ...string) *cmd {
return &cmd{
command: "tar",
args: args,
}
}
46 changes: 46 additions & 0 deletions vagrant/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package vagrant

import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -36,3 +38,47 @@ func tarFiles(w io.Writer, base string, files []os.FileInfo) error {

return nil
}

func gunzip(baseDir string, gzipStream io.Reader) error {

uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return err
}

tarReader := tar.NewReader(uncompressedStream)

for true {
header, err := tarReader.Next()

if err == io.EOF {
break
}

if err != nil {
return err
}

switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(filepath.Join(baseDir, header.Name), 0755); err != nil {
return err
}
case tar.TypeReg:
outFile, err := os.Create(filepath.Join(baseDir, header.Name))
if err != nil {
return err
}
defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil {
return err
}

default:
return fmt.Errorf("ExtractTarGz: uknown type: %s in %s", header.Typeflag, header.Name)
}

}

return nil
}
11 changes: 8 additions & 3 deletions vagrant/image.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vagrant

import (
"github.com/mhewedy/vermin/command"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -41,8 +40,14 @@ func ProcessImage(imagePath string) error {
}

func gunzipVagrantBox(imagePath, imageDir string) error {
// TODO change from using tar command to golang code
return command.Tar("xzf", imagePath, "-C", imageDir).Run()

gzipFile, err := os.Open(imagePath)
if err != nil {
return err
}
defer gzipFile.Close()

return gunzip(imageDir, gzipFile)
}

func createOVAFile(imagePath, imageDir string) error {
Expand Down

0 comments on commit dadebc2

Please sign in to comment.