Skip to content

Commit

Permalink
Fixes lint, adds ability to turn off log file. (#8)
Browse files Browse the repository at this point in the history
* Fix match for files with partNN in name.

* fix comments

* Fix lint and add log ignore
  • Loading branch information
davidnewhall authored Dec 17, 2021
1 parent 6d0bdd1 commit 1dcd148
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 160 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ os: linux
dist: bionic
language: go
go:
- 1.16.x
- 1.17.x
install:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.40.1
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.43.0
script:
- make test
12 changes: 6 additions & 6 deletions 7z.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

// Extract7z extracts a 7zip archive. This wraps https://github.com/saracen/go7z.
func Extract7z(x *XFile) (int64, []string, error) {
sz, err := go7z.OpenReader(x.FilePath)
func Extract7z(xFile *XFile) (int64, []string, error) {
sevenZip, err := go7z.OpenReader(xFile.FilePath)
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer sz.Close()
defer sevenZip.Close()

return x.un7zip(sz)
return xFile.un7zip(sevenZip)
}

func (x *XFile) un7zip(szreader *go7z.ReadCloser) (int64, []string, error) {
Expand Down Expand Up @@ -52,12 +52,12 @@ func (x *XFile) un7zip(szreader *go7z.ReadCloser) (int64, []string, error) {
continue
}

s, err := writeFile(wfile, szreader, x.FileMode, x.DirMode)
fileSize, err := writeFile(wfile, szreader, x.FileMode, x.DirMode)
if err != nil {
return size, files, err
}

files = append(files, wfile)
size += s
size += fileSize
}
}
28 changes: 14 additions & 14 deletions cmd/xt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,40 @@ func processInput(paths []string, output string) {
log.Println("==> No archives found in:", paths)
}

for i, f := range archives {
log.Printf("==> Extracting Archive (%d/%d): %s", i+1, len(archives), f)
for i, fileName := range archives {
log.Printf("==> Extracting Archive (%d/%d): %s", i+1, len(archives), fileName)

start := time.Now()

size, files, _, err := xtractr.ExtractFile(&xtractr.XFile{
FilePath: f, // Path to archive being extracted.
OutputDir: output, // Folder to extract archive into.
FileMode: 0644, // nolint:gomnd // Write files with this mode.
DirMode: 0755, // nolint:gomnd // Write folders with this mode.
Password: "", // (RAR) Archive password. Blank for none.
FilePath: fileName, // Path to archive being extracted.
OutputDir: output, // Folder to extract archive into.
FileMode: 0o644, // nolint:gomnd // Write files with this mode.
DirMode: 0o755, // nolint:gomnd // Write folders with this mode.
Password: "", // (RAR) Archive password. Blank for none.
})
if err != nil {
log.Printf("[ERROR] Archive: %s: %v", f, err)
log.Printf("[ERROR] Archive: %s: %v", fileName, err)
continue
}

elapsed := time.Since(start).Round(time.Millisecond)
log.Printf("==> Extracted Archive %s in %v: bytes: %d, files: %d", f, elapsed, size, len(files))
log.Printf("==> Extracted Archive %s in %v: bytes: %d, files: %d", fileName, elapsed, size, len(files))
log.Printf("==> Files:\n - %s", strings.Join(files, "\n - "))
}
}

func getArchives(paths []string) []string {
archives := []string{}

for _, f := range paths {
switch fileInfo, err := os.Stat(f); {
for _, fileName := range paths {
switch fileInfo, err := os.Stat(fileName); {
case err != nil:
log.Fatalf("[ERROR] Reading Path: %s: %s", f, err)
log.Fatalf("[ERROR] Reading Path: %s: %s", fileName, err)
case fileInfo.IsDir():
archives = append(archives, xtractr.FindCompressedFiles(f)...)
archives = append(archives, xtractr.FindCompressedFiles(fileName)...)
default:
archives = append(archives, f)
archives = append(archives, fileName)
}
}

Expand Down
64 changes: 31 additions & 33 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ func (x *Xtractr) GetFileList(path string) (files []string) {
// Used to find new files in a file list from a path. ie. those we extracted.
// This is a helper method and only exposed for convenience. You do not have to call this.
func Difference(slice1 []string, slice2 []string) (diff []string) {
for _, s2 := range slice2 {
for _, s2p := range slice2 {
var found bool

for _, s1 := range slice1 {
if s1 == s2 {
if s1 == s2p {
found = true

break
}
}

if !found { // String not found, so it's a new string, add it to the diff.
diff = append(diff, s2)
diff = append(diff, s2p)
}
}

Expand Down Expand Up @@ -86,15 +86,13 @@ func FindCompressedFiles(path string) []string {
}

// Check (save) if the current path has any rar files.
// So we can ignore r01 if it does.
if r, _ := filepath.Glob(filepath.Join(path, "*.rar")); len(r) > 0 {
return getCompressedFiles(true, path, fileList)
}
// So we can ignore r00 if it does.
r, _ := filepath.Glob(filepath.Join(path, "*.rar"))

return getCompressedFiles(false, path, fileList)
return getCompressedFiles(len(r) > 0, path, fileList)
}

// getCompressedFiles checks file suffixes to fine the archives to decompress.
// getCompressedFiles checks file suffixes to find archives to decompress.
// This pays special attention to the widely accepted variance of rar formats.
func getCompressedFiles(hasrar bool, path string, fileList []os.FileInfo) []string { //nolint:cyclop
files := []string{}
Expand Down Expand Up @@ -133,36 +131,36 @@ func (x *XFile) Extract() (int64, []string, []string, error) {

// ExtractFile calls the correct procedure for the type of file being extracted.
// Returns size of extracted data, list of extracted files, list of archives processed, and/or error.
func ExtractFile(x *XFile) (int64, []string, []string, error) { //nolint:cyclop
func ExtractFile(xFile *XFile) (int64, []string, []string, error) {
var (
size int64
files []string
err error
)

switch s := strings.ToLower(x.FilePath); {
case strings.HasSuffix(s, ".rar"), strings.HasSuffix(s, ".r00"):
return ExtractRAR(x)
case strings.HasSuffix(s, ".7z"):
size, files, err = Extract7z(x)
case strings.HasSuffix(s, ".zip"):
size, files, err = ExtractZIP(x)
case strings.HasSuffix(s, ".tar.gz") || strings.HasSuffix(s, ".tgz"):
size, files, err = ExtractTarGzip(x)
case strings.HasSuffix(s, ".tar.bz2") || strings.HasSuffix(s, ".tbz2") ||
strings.HasSuffix(s, ".tbz") || strings.HasSuffix(s, ".tar.bz"):
size, files, err = ExtractTarBzip(x)
case strings.HasSuffix(s, ".bz") || strings.HasSuffix(s, ".bz2"):
size, files, err = ExtractBzip(x)
case strings.HasSuffix(s, ".gz"):
size, files, err = ExtractGzip(x)
case strings.HasSuffix(s, ".tar"):
size, files, err = ExtractTar(x)
switch sName := strings.ToLower(xFile.FilePath); {
case strings.HasSuffix(sName, ".rar"), strings.HasSuffix(sName, ".r00"):
return ExtractRAR(xFile)
case strings.HasSuffix(sName, ".7z"):
size, files, err = Extract7z(xFile)
case strings.HasSuffix(sName, ".zip"):
size, files, err = ExtractZIP(xFile)
case strings.HasSuffix(sName, ".tar.gz"), strings.HasSuffix(sName, ".tgz"):
size, files, err = ExtractTarGzip(xFile)
case strings.HasSuffix(sName, ".tar.bz2"), strings.HasSuffix(sName, ".tbz2"),
strings.HasSuffix(sName, ".tbz"), strings.HasSuffix(sName, ".tar.bz"):
size, files, err = ExtractTarBzip(xFile)
case strings.HasSuffix(sName, ".bz"), strings.HasSuffix(sName, ".bz2"):
size, files, err = ExtractBzip(xFile)
case strings.HasSuffix(sName, ".gz"):
size, files, err = ExtractGzip(xFile)
case strings.HasSuffix(sName, ".tar"):
size, files, err = ExtractTar(xFile)
default:
return 0, nil, nil, fmt.Errorf("%w: %s", ErrUnknownArchiveType, x.FilePath)
return 0, nil, nil, fmt.Errorf("%w: %s", ErrUnknownArchiveType, xFile.FilePath)
}

return size, files, []string{x.FilePath}, err
return size, files, []string{xFile.FilePath}, err
}

// MoveFiles relocates files then removes the folder they were in.
Expand Down Expand Up @@ -226,8 +224,8 @@ func (x *Xtractr) DeleteFiles(files ...string) {
}

// writeFile writes a file from an io reader, making sure all parent directories exist.
func writeFile(fpath string, fdata io.Reader, fm, dm os.FileMode) (int64, error) {
if err := os.MkdirAll(filepath.Dir(fpath), dm); err != nil {
func writeFile(fpath string, fdata io.Reader, fMode, dMode os.FileMode) (int64, error) {
if err := os.MkdirAll(filepath.Dir(fpath), dMode); err != nil {
return 0, fmt.Errorf("os.MkdirAll: %w", err)
}

Expand All @@ -238,7 +236,7 @@ func writeFile(fpath string, fdata io.Reader, fm, dm os.FileMode) (int64, error)
defer fout.Close()

if runtime.GOOS != "windows" {
if err = fout.Chmod(fm); err != nil {
if err = fout.Chmod(fMode); err != nil {
return 0, fmt.Errorf("chmod: %w", err)
}
}
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module golift.io/xtractr

go 1.16
go 1.17

require (
github.com/nwaples/rardecode v1.1.2
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda
)

require (
github.com/nwaples/rardecode v1.1.0
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda // indirect
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/nwaples/rardecode v1.1.2 h1:Cj0yZY6T1Zx1R7AhTbyGSALm44/Mmq+BAPc4B/p/d3M=
github.com/nwaples/rardecode v1.1.2/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda h1:h+YpzUB/bGVJcLqW+d5GghcCmE/A25KbzjXvWJQi/+o=
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda/go.mod h1:MSotTrCv1PwoR8QgU1JurEx+lNNbtr25I+m0zbLyAGw=
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f h1:1cJITU3JUI8qNS5T0BlXwANsVdyoJQHQ4hvOxbunPCw=
Expand Down
Loading

0 comments on commit 1dcd148

Please sign in to comment.