Skip to content

Commit

Permalink
Fix & update test
Browse files Browse the repository at this point in the history
  • Loading branch information
nabbar committed Sep 22, 2020
1 parent bbf9749 commit 10e9385
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Test binary, built with `go test -c`
*.test
*.cover*

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand All @@ -22,3 +23,4 @@ go.sum
.idea/*
.idea/**/*

/test-*
114 changes: 114 additions & 0 deletions archive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Package Archive
This package will try to do all uncompress/unarchive and extract one file or all file.
This package will expose 2 functions :
- ExtractFile : for one file extracted
- ExtractAll : to extract all file

## Example of implementation

### Example of one file extracted

To use `ExtractFile` function, you will need this parameters :
- `src` : is the source file into a `ioutils.FileProgress` struct to expose an `os.File` pointer with interface `io.WriteTo`, `io.ReadFrom`, `io.ReaderAt` and progress capabilities
- `dst` : is the source file into a `ioutils.FileProgress` struct to expose an `os.File` pointer with interface `io.WriteTo`, `io.ReadFrom`, `io.ReaderAt` and progress capabilities
- `filenameContain` : is a `string` to search in the file name to find it and extract it. This string will be search into the `strings.Contains` function
- `filenameRegex` : is a regex pattern `string` to search in the file name any match and extract it. This string will be search into the `regexp.MatchString` function

You can implement this function as it. This example is available in [`test/test-archive`](../test/test-archive/main.go) folder.
```go
import (
"io"
"io/ioutil"

"github.com/nabbar/golib/archive"
"github.com/nabbar/golib/ioutils"
)

const fileName = "fullpath to my archive file"

func main() {
var (
src ioutils.FileProgress
dst ioutils.FileProgress
err errors.Error
)

// register closing function in output function callback
defer func() {
if src != nil {
_ = src.Close()
}
if dst != nil {
_ = dst.Close()
}
}()

// open archive with a ioutils NewFileProgress function
if src, err = ioutils.NewFileProgressPathOpen(fileName); err != nil {
panic(err)
}

// open a destination with a ioutils NewFileProgress function, as a temporary file
if dst, err = ioutils.NewFileProgressTemp(); err != nil {
panic(err)
}

// call the extract file function
if err = archive.ExtractFile(tmp, rio, "path/to/my/file/into/archive", "archive name regex"); err != nil {
panic(err)
}

}
```


### Example of all files extracted

To use `ExtractAll` function, you will need this parameters :
- `src` : is the source file into a `ioutils.FileProgress` struct to expose an `os.File` pointer with interface `io.WriteTo`, `io.ReadFrom`, `io.ReaderAt` and progress capabilities
- `originalName` : is a `string` to define the originalName of the archive. This params is used to create a unique file created into the outputPath if the archive is not an archive or just compressed with a not catalogued compress type like gzip or bzip2.
- `outputPath` : is a `string` to precise the destination output directory (full path). All extracted file will be extracted with this directory as base of path.
- `defaultDirPerm` : is a `os.FileMode` to precise the permission of directory. This parameters is usefull if the output directory is not existing.

You can implement this function as it. This example is available in [`test/test-archive-all`](../test/test-archive-all/main.go) folder.
```go
import (
"io"
"io/ioutil"

"github.com/nabbar/golib/archive"
"github.com/nabbar/golib/ioutils"
)

const fileName = "fullpath to my archive file"

func main() {
var (
src ioutils.FileProgress
tmp ioutils.FileProgress
out string
err error
)

// open archive with a ioutils NewFileProgress function
if src, err = ioutils.NewFileProgressPathOpen(fileName); err != nil {
panic(err)
}

// create an new temporary file to use his name as output path
if tmp, err = ioutils.NewFileProgressTemp(); err != nil {
panic(err)
} else {
// get the filename of the temporary file
out = tmp.FilePath()

// close the temporary file will call the delete temporary file
_ = tmp.Close()
}

if err = archive.ExtractAll(src, path.Base(src.FilePath()), out, 0775); err != nil {
panic(err)
}

}
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ require (
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
Empty file removed httpcli/README.md
Empty file.
Empty file removed httpserver/README.md
Empty file.
95 changes: 95 additions & 0 deletions test/test-archive-all/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package main

import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/nabbar/golib/archive"
"github.com/nabbar/golib/errors"
"github.com/nabbar/golib/ioutils"
"github.com/nabbar/golib/logger"
)

// git archive --format=tar --output=git.tar HEAD && gzip git.tar
const fileName = "./git.tar.gz"

func main() {
var (
src ioutils.FileProgress
tmp ioutils.FileProgress
out string
err error
)

defer func() {
if src != nil {
_ = src.Close()
}
if tmp != nil {
_ = tmp.Close()
}
}()

logger.SetLevel(logger.DebugLevel)
logger.EnableColor()
logger.FileTrace(true)
errors.SetModeReturnError(errors.ErrorReturnCodeErrorTraceFull)

if src, err = ioutils.NewFileProgressPathOpen(fileName); err != nil {
panic(err)
}

if tmp, err = ioutils.NewFileProgressTemp(); err != nil {
panic(err)
} else {
out = tmp.FilePath()
_ = tmp.Close()
}

if err = archive.ExtractAll(src, path.Base(src.FilePath()), out, 0775); err != nil {
panic(err)
}

if list, e := ioutil.ReadDir(out); e != nil {
panic(e)
} else {
for _, f := range list {
var (
isDir bool
isLink bool
isFile bool
)
isDir = f.IsDir()
isLink = f.Mode()&os.ModeSymlink == os.ModeSymlink
isFile = !isLink && !isDir
println(fmt.Sprintf("Item '%s' is Dir '%v', is Link '%v', is File '%v'", f.Name(), isDir, isLink, isFile))
}
}
}
47 changes: 26 additions & 21 deletions test/test-archive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ package main
import (
"io"
"io/ioutil"
"os"

"github.com/nabbar/golib/errors"

"github.com/nabbar/golib/archive"
"github.com/nabbar/golib/ioutils"
Expand All @@ -46,44 +47,48 @@ const regex = "vendor\\.tar(\\.(?:gz|bz))?"

func main() {
var (
src *os.File
tmp *os.File
rio *os.File
err error
src ioutils.FileProgress
tmp ioutils.FileProgress
rio ioutils.FileProgress
err errors.Error
)

if src, err = os.Open(fileName); err != nil {
panic(err)
}

defer func() {
_ = src.Close()
if src != nil {
_ = src.Close()
}
if tmp != nil {
_ = tmp.Close()
}
if rio != nil {
_ = rio.Close()
}
}()

if tmp, err = ioutils.NewTempFile(); err != nil {
if src, err = ioutils.NewFileProgressPathOpen(fileName); err != nil {
panic(err)
}

defer func() {
_ = ioutils.DelTempFile(tmp)
}()

if _, err = io.Copy(tmp, src); err != nil {
if tmp, err = ioutils.NewFileProgressTemp(); err != nil {
panic(err)
}

if rio, err = archive.ExtractFile(tmp, contain, regex); err != nil {
if rio, err = ioutils.NewFileProgressTemp(); err != nil {
panic(err)
}

defer func() {
_ = rio.Close()
}()
if _, e := tmp.ReadFrom(src); e != nil {
panic(e)
}

if _, err = rio.Seek(0, 0); err != nil {
if err = archive.ExtractFile(tmp, rio, contain, regex); err != nil {
panic(err)
}

if _, e := rio.Seek(0, io.SeekStart); e != nil {
panic(e)
}

if b, e := ioutil.ReadAll(rio); e != nil {
panic(e)
} else {
Expand Down
Loading

0 comments on commit 10e9385

Please sign in to comment.