From 971cc396e1d01f53f65a86278fd0ac4490565335 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Fri, 20 Nov 2020 19:56:29 -0500 Subject: Reorganize. Update to use go modules. --- lib/archive/archive.go | 104 ----------------------------------------------- lib/archive/unarchive.go | 85 -------------------------------------- 2 files changed, 189 deletions(-) delete mode 100644 lib/archive/archive.go delete mode 100644 lib/archive/unarchive.go (limited to 'lib/archive') diff --git a/lib/archive/archive.go b/lib/archive/archive.go deleted file mode 100644 index 639a864..0000000 --- a/lib/archive/archive.go +++ /dev/null @@ -1,104 +0,0 @@ -package archive - -import ( - "archive/tar" - "path/filepath" - - "compress/gzip" - - "io" - "os" - - "strings" -) - -type Archive struct { - Output string - Input []string - file *os.File -} - -func (a *Archive) Tar(compressionType string) error { - var err error - if a.file, err = os.Create(a.Output); err != nil { - return err - } - - defer a.file.Close() - - var twriter *tar.Writer - - // Set the compression type... if any - switch compressionType { - case "gz": - gzwriter, err := gzip.NewWriterLevel(a.file, gzip.BestSpeed) - if err != nil { - return err - } - defer gzwriter.Close() - // Write to the gzip writer - twriter = tar.NewWriter(gzwriter) - default: - // Write directly to the file - twriter = tar.NewWriter(a.file) - } - - // Close off the tar writer when we're done - defer twriter.Close() - - for _, v := range a.Input { - if err := filepath.Walk(v, tarWalkfn(twriter)); err != nil { - return err - } - } - - return nil - -} - -func tarWalkfn(writer *tar.Writer) filepath.WalkFunc { - // This is an interesting trick to get around scoping issues - return func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if info.IsDir() { - return nil - } - - f, err := os.Open(path) - if err != nil { - return err - } - defer f.Close() - - // Convert Windows paths to Unix paths - path = strings.Replace(path, "\\", "/", -1) - - // TODO; See if tar.FileInfoheader() could be used instead - // without the pathing issues I encountered - h := &tar.Header{ - Name: path, - Size: info.Size(), - // I don't like it... but it helps with platform compatibility - Mode: 0664, - ModTime: info.ModTime(), - } - - err = writer.WriteHeader(h) - if err != nil { - return err - } - - _, err = io.Copy(writer, f) - if err != nil { - // TODO: Figure out how to add more useful information to - // These errors - // fmt.Fprintln(os.Stderr, f.Name()) - return err - } - - return nil - } -} diff --git a/lib/archive/unarchive.go b/lib/archive/unarchive.go deleted file mode 100644 index 8a5617e..0000000 --- a/lib/archive/unarchive.go +++ /dev/null @@ -1,85 +0,0 @@ -package archive - -import ( - "archive/tar" - fp "path/filepath" - - "compress/gzip" - - "io" - "os" - - "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) - if err != nil { - return err - } - defer f.Close() - - var treader *tar.Reader - - switch compressionType { - case "gz": - gzreader, err := gzip.NewReader(f) - if err != nil { - return err - } - // Read from the gzip reader instead of the file - treader = tar.NewReader(gzreader) - default: - // Read from the file directly - treader = tar.NewReader(f) - } - - for { - hdr, err := treader.Next() - if err == io.EOF { - // We've reached the end! Whoee - break - } - if err != nil { - return err - } - - // Fix windows slashes... - fileName := strings.Replace(hdr.Name, "\\", "/", -1) - - info := hdr.FileInfo() - if info.IsDir() { - // I don't like hard-coded permissions but it - // it helps with overall platform compatibility - if err = os.MkdirAll(fileName, 0775); err != nil { - return err - } - continue - } - - if err = os.MkdirAll(fp.Dir(fileName), 0775); err != nil { - return err - } - - // Create a file handle to work with - // f, err := os.Create(fileName) - f, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664) - if err != nil { - return err - } - defer f.Close() - if _, err := io.Copy(f, treader); err != nil { - return err - } - - } - - return nil -} -- cgit v1.2.3