diff --git a/archive/archive.go b/archive/archive.go index 63b91eb..931ce10 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -6,18 +6,24 @@ package archive import ( + "errors" "fmt" "io" "os" "path/filepath" "strings" - "time" "github.com/martinplaner/gunarchiver/debug" "github.com/martinplaner/gunarchiver/progress" ) +var ErrCanceled = errors.New("the process was canceled by user request") + +func IsCanceled(err error) bool { + return err == ErrCanceled +} + // Archive represent an archive and provides methods for iterating through its files. type Archive interface { // Basename returns the base name of the archive file, without file extension or path. @@ -43,7 +49,8 @@ type File interface { } // Extract extracts the Archive a to the specified path and reports progress to the supplied progressChan. -// If the supplied shouldCancel func returns true, the extraction will get canceled as soon as possible. +// If the supplied shouldCancel func returns true, the extraction will get canceled as soon as possible +// and return ErrCanceled. Callers should check using IsCanceled. func Extract(a Archive, path string, progressChan chan progress.Progress, shouldCancel func() bool) error { progressChan <- progress.Progress{CurrentFile: "Starting extraction..."} @@ -53,7 +60,7 @@ func Extract(a Archive, path string, progressChan chan progress.Progress, should if shouldCancel() { // Aborting extraction as per user's request progressChan <- progress.Progress{CurrentFile: "Canceled extraction!", Percentage: 100} - return nil + return ErrCanceled } file, err := a.Next() if err == io.EOF { diff --git a/main.go b/main.go index 43266e1..8eb937a 100644 --- a/main.go +++ b/main.go @@ -63,7 +63,11 @@ func main() { } func extractArchiveAndDelete(path string, progressChan chan progress.Progress, shouldCancel func() bool) error { - if err := extractArchive(path, progressChan, shouldCancel); err != nil { + + err := extractArchive(path, progressChan, shouldCancel) + if archive.IsCanceled(err) { + return nil + } else if err != nil { return err } @@ -94,7 +98,10 @@ func extractArchive(path string, progressChan chan progress.Progress, shouldCanc archive.CreateDir(baseDir) } - if err := archive.Extract(a, baseDir, progressChan, shouldCancel); err != nil { + err = archive.Extract(a, baseDir, progressChan, shouldCancel) + if archive.IsCanceled(err) { + return err + } else if err != nil { return fmt.Errorf("could not extract archive: %v", err) }