From 654332673c7519bb3c59110e349a49674fcb17ad Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 4 Mar 2016 20:14:54 +0000 Subject: [PATCH] fix untargz --- .travis.yml | 1 + hugo/hugo.go | 95 +++------------------------------------------------- 2 files changed, 6 insertions(+), 90 deletions(-) diff --git a/.travis.yml b/.travis.yml index a0868c0d..fd3114ef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ install: - go get github.com/mitchellh/go-homedir - go get github.com/caddyserver/caddydev - go get github.com/caddyserver/caddyext +- go get github.com/pivotal-golang/archiver - go install github.com/caddyserver/caddydev - go install github.com/caddyserver/caddyext - go install github.com/mitchellh/gox diff --git a/hugo/hugo.go b/hugo/hugo.go index 57299817..d59c06fc 100644 --- a/hugo/hugo.go +++ b/hugo/hugo.go @@ -1,8 +1,6 @@ package hugo import ( - "archive/zip" - "compress/gzip" "crypto/sha256" "encoding/hex" "fmt" @@ -16,6 +14,7 @@ import ( "runtime" "github.com/mitchellh/go-homedir" + "github.com/pivotal-golang/archiver/extractor" ) const ( @@ -80,9 +79,11 @@ func GetPath() string { // Unzip or Ungzip the file switch runtime.GOOS { case "darwin", "windows": - err = unzip(tempfile, temp) + zp := extractor.NewZip() + zp.Extract(tempfile, temp) default: - err = ungzip(tempfile, temp) + gz := extractor.NewTgz() + gz.Extract(tempfile, temp) } if err != nil { @@ -199,89 +200,3 @@ func checkSHA256() { fmt.Println("checked!") } - -func unzip(src, dest string) error { - r, err := zip.OpenReader(src) - if err != nil { - return err - } - defer func() { - if err := r.Close(); err != nil { - panic(err) - } - }() - - os.MkdirAll(dest, 0755) - - // Closure to address file descriptors issue with all the deferred .Close() methods - extractAndWriteFile := func(f *zip.File) error { - rc, err := f.Open() - if err != nil { - return err - } - defer func() { - if err := rc.Close(); err != nil { - panic(err) - } - }() - - path := filepath.Join(dest, f.Name) - - if f.FileInfo().IsDir() { - os.MkdirAll(path, f.Mode()) - } else { - if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) { - os.MkdirAll(filepath.Dir(path), 0755) - } - - f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) - if err != nil { - return err - } - defer func() { - if err := f.Close(); err != nil { - panic(err) - } - }() - _, err = io.Copy(f, rc) - - if err != nil { - return err - } - } - return nil - } - - for _, f := range r.File { - err := extractAndWriteFile(f) - if err != nil { - return err - } - } - - return nil -} - -func ungzip(source, target string) error { - reader, err := os.Open(source) - if err != nil { - return err - } - defer reader.Close() - - archive, err := gzip.NewReader(reader) - if err != nil { - return err - } - defer archive.Close() - - target = filepath.Join(target, archive.Name) - writer, err := os.Create(target) - if err != nil { - return err - } - defer writer.Close() - - _, err = io.Copy(writer, archive) - return err -}