From 0dc6e92cd018aa0ecea5349a14599279e1a2a574 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Mon, 6 Feb 2017 09:06:37 -0500 Subject: Simplified the Archive library. Added a way to pull the config from the web application --- lib/archive/archive.go | 27 +++++++++++++-------------- lib/archive/unarchive.go | 19 +++++++++---------- 2 files changed, 22 insertions(+), 24 deletions(-) (limited to 'lib/archive') diff --git a/lib/archive/archive.go b/lib/archive/archive.go index 639a864..2d0f4fd 100644 --- a/lib/archive/archive.go +++ b/lib/archive/archive.go @@ -12,26 +12,26 @@ import ( "strings" ) -type Archive struct { - Output string - Input []string - file *os.File -} - -func (a *Archive) Tar(compressionType string) error { +func TarToFile(Output string, Input []string, compressionType string) error { var err error - if a.file, err = os.Create(a.Output); err != nil { + file, err := os.Create(Output) + if err != nil { return err } - defer a.file.Close() + defer file.Close() + + return Tar(file, Input, compressionType) +} + +func Tar(writer io.Writer, Input []string, compressionType string) error { var twriter *tar.Writer // Set the compression type... if any switch compressionType { case "gz": - gzwriter, err := gzip.NewWriterLevel(a.file, gzip.BestSpeed) + gzwriter, err := gzip.NewWriterLevel(writer, gzip.BestSpeed) if err != nil { return err } @@ -39,21 +39,20 @@ func (a *Archive) Tar(compressionType string) error { // Write to the gzip writer twriter = tar.NewWriter(gzwriter) default: - // Write directly to the file - twriter = tar.NewWriter(a.file) + // Write directly to the io.Writer + twriter = tar.NewWriter(writer) } // Close off the tar writer when we're done defer twriter.Close() - for _, v := range a.Input { + for _, v := range Input { if err := filepath.Walk(v, tarWalkfn(twriter)); err != nil { return err } } return nil - } func tarWalkfn(writer *tar.Writer) filepath.WalkFunc { diff --git a/lib/archive/unarchive.go b/lib/archive/unarchive.go index 8a5617e..463216f 100644 --- a/lib/archive/unarchive.go +++ b/lib/archive/unarchive.go @@ -12,25 +12,24 @@ import ( "strings" ) -type Unarchive struct { - Input string - tarReader *tar.Reader -} - // Extracts a tar arcive to the current working directory // This will overwrite everything. So be careful -func (u *Unarchive) UnTar(compressionType string) error { - f, err := os.Open(u.Input) +func UnTarFromFile(InputFile, compressionType string) error { + r, err := os.Open(InputFile) if err != nil { return err } - defer f.Close() + defer r.Close() + + return UnTar(r, compressionType) +} +func UnTar(r io.Reader, compressionType string) error { var treader *tar.Reader switch compressionType { case "gz": - gzreader, err := gzip.NewReader(f) + gzreader, err := gzip.NewReader(r) if err != nil { return err } @@ -38,7 +37,7 @@ func (u *Unarchive) UnTar(compressionType string) error { treader = tar.NewReader(gzreader) default: // Read from the file directly - treader = tar.NewReader(f) + treader = tar.NewReader(r) } for { -- cgit v1.2.3