Skip to content

Commit

Permalink
Merge pull request #7 from JaSei/change_crypto_api
Browse files Browse the repository at this point in the history
change crypto API
  • Loading branch information
JaSei authored Nov 15, 2019
2 parents 70131fd + 96495a9 commit 769ce32
Show file tree
Hide file tree
Showing 29 changed files with 588 additions and 159 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ os:
- linux

go:
- 1.5
- 1.6
- 1.7
- 1.8
- 1.9
- "1.10"
- 1.11
- 1.12
- 1.13

install:
- make setup
Expand Down
30 changes: 21 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
[[constraint]]
name = "github.com/mitchellh/go-homedir"
version = "1.0.0"

[[constraint]]
name = "github.com/JaSei/hashutil-go"
version = "2.0.0"
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
HELP?=$$(go run main.go --help 2>&1)
VERSION?=$$(cat VERSION)
GONEWER?=$(shell go version | grep -E "go1\.1[01]")
DEP?=$$(which dep)
LINTER?=$$(which golangci-lint)
LINTER_VERSION=1.15.0
Expand Down Expand Up @@ -28,7 +27,7 @@ setup: ## Install all the build and lint dependencies
curl -L https://github.com/golang/dep/releases/download/v0.5.1/$(DEP_VERS) >| $$GOPATH/bin/dep;\
chmod +x $$GOPATH/bin/dep;\
fi
dep ensure
dep ensure -v

@if [ "$(LINTER)" = "" ]; then\
curl -L https://github.com/golangci/golangci-lint/releases/download/v$(LINTER_VERSION)/$(LINTER_FILE) $(LINTER_UNPACK) ;\
Expand Down
194 changes: 161 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,75 @@ functions which isn't in core libraries (like `Copy` for example)

* [pathlib](https://docs.python.org/3/library/pathlib.html) for python

## Usage
BREAKING CHANGE 0.3.1 -> 1.0.0

#### type CryptoHash
1. `NewTempFile` or `NewTempDir` don't need TempOpt struct

```go
type CryptoHash struct {
hash.Hash
}
```
//0.3.1 default
pathutil.NewTempFile(pathutil.TempOpt{})
//0.3.1 custom
pathutil.NewTempFile(pathutil.TempOpt{Dir: "/test", Prefix: "pre"})

//1.0.0 default
pathutil.NewTempFile()
//1.0.0 custom
pathutil.NewTempFile(Dir("/test"), Prefix("pre"))

#### func (*CryptoHash) BinSum
2. `New` method parameter allowed `string` type or type implements
`fmt.Stringer` interface

```go
func (hash *CryptoHash) BinSum() []byte
```
BinSum method is like hash.Sum(nil)
//0.3.1
pathutil.New(otherPath.String(), "test")

#### func (*CryptoHash) HexSum
//1.0.0
pathutil.New(otherPath, "test")

```go
func (hash *CryptoHash) HexSum() string
```
HexSum method retun hexstring representation of hash.Sum
This shouldn't be breaking change, but if you use in some code variadic
parameter as input of `pathutil.New`, then can be problem

//0.3.1
func(p ...string) {
pathutil.New(p...)
}("a", "b")

//1.0.0
func(p ...string) {
n := make([]interface{}, len(p))
for i, v := range p {
n[i] = v
}
pathutil.New(n...)
}("a", "b")

3. There is new (more handfull) crypto API

//0.3.1
import (
"crypto"
"github.com/JaSei/pathutil-go"
)
...

hash, err := path.Crypto(crypto.SHA256)
if err == nil {
fmt.Printf("%s\t%s\n", hash.HexSum(), path.String())
}

//1.0.0
import (
"github.com/JaSei/pathutil-go"
)
...

hash, err := path.CryptoSha256()
if err == nil {
fmt.Printf("%s\t%s\n", hash, path.String())
}

This new crypto API return [hashutil](github.com/JaSei/hashutil-go) struct which
is more handfull for compare, transformation and next hash manipulation.

## Usage

#### type LinesFunc

Expand Down Expand Up @@ -111,7 +156,11 @@ type Path interface {

CopyFrom(io.Reader) (int64, error)

Crypto(crypto.Hash) (*CryptoHash, error)
CryptoMd5() (hashutil.Md5, error)
CryptoSha1() (hashutil.Sha1, error)
CryptoSha256() (hashutil.Sha256, error)
CryptoSha384() (hashutil.Sha384, error)
CryptoSha512() (hashutil.Sha512, error)

MakePath() error
MakePathMode(os.FileMode) error
Expand Down Expand Up @@ -168,32 +217,37 @@ for example
#### func New

```go
func New(path ...string) (Path, error)
func New(path ...interface{}) (Path, error)
```
New construct Path

for example

path := New("/home/test", ".vimrc")

if you can use `Path` in `New`, you must use `.String()` method
input can be `string` or type implements `fmt.Stringer` interface

#### func NewTempDir

```go
func NewTempDir(options TempOpt) (Path, error)
func NewTempDir(options ...TempOpt) (Path, error)
```
NewTempDir create temp directory

for cleanup use `defer`

tempdir, err := pathutil.NewTempDir(TempOpt{})
tempdir, err := pathutil.NewTempDir()
defer tempdir.RemoveTree()

if you need set directory or prefix, then use `TempDir` and/or `TempPrefix`

temp, err := NewTempFile(TempDir("/home/my/test"), TempPrefix("myfile"))
...

#### func NewTempFile

```go
func NewTempFile(options TempOpt) (p Path, err error)
func NewTempFile(options ...TempOpt) (p Path, err error)
```
NewTempFile create temp file

Expand All @@ -204,9 +258,14 @@ for cleanup use defer

if you need only temp file name, you must delete file

temp, err := NewTempFile(TempFileOpt{})
temp, err := NewTempFile()
temp.Remove()

if you need set directory or prefix, then use `TempDir` and/or `TempPrefix`

temp, err := NewTempFile(TempDir("/home/my/test"), TempPrefix("myfile"))
...

#### type PathImpl

```go
Expand Down Expand Up @@ -275,11 +334,70 @@ func (path PathImpl) CopyFrom(reader io.Reader) (copyied int64, err error)
```
CopyFrom copy stream from reader to path (file after copy close and sync)

#### func (PathImpl) Crypto
#### func (PathImpl) CryptoMd5

```go
func (path PathImpl) CryptoMd5() (hashutil.Md5, error)
```
CryptoMd5 method access hash funcionality like Path::Tiny Digest return
[hashutil.Md5](github.com/JaSei/hashutil-go) type

for example print of Md5 hexstring

hash, err := path.CryptoMd5()
fmt.Println(hash)

#### func (PathImpl) CryptoSha1

```go
func (path PathImpl) Crypto(hash crypto.Hash) (c *CryptoHash, err error)
func (path PathImpl) CryptoSha1() (hashutil.Sha1, error)
```
CryptoSha1 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha1](github.com/JaSei/hashutil-go) type

for example print of Sha1 hexstring

hash, err := path.CryptoSha1()
fmt.Println(hash)

#### func (PathImpl) CryptoSha256

```go
func (path PathImpl) CryptoSha256() (hashutil.Sha256, error)
```
CryptoSha256 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha256](github.com/JaSei/hashutil-go) type

for example print of Sha256 hexstring

hash, err := path.CryptoSha256()
fmt.Println(hash)

#### func (PathImpl) CryptoSha384

```go
func (path PathImpl) CryptoSha384() (hashutil.Sha384, error)
```
CryptoSha384 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha384](github.com/JaSei/hashutil-go) type

for example print of Sha284 hexstring

hash, err := path.CryptoSha284()
fmt.Println(hash)

#### func (PathImpl) CryptoSha512

```go
func (path PathImpl) CryptoSha512() (hashutil.Sha512, error)
```
CryptoSha512 method access hash funcionality like Path::Tiny Digest return
[hashutil.Sha512](github.com/JaSei/hashutil-go) type

for example print of Sha512 hexstring

hash, err := path.CryptoSha512()
fmt.Println(hash)

#### func (PathImpl) Exists

Expand Down Expand Up @@ -493,15 +611,25 @@ type ReadSeekCloser interface {
#### type TempOpt

```go
type TempOpt struct {
// directory where is temp file/dir create, empty string `""` (default) means TEMPDIR (`os.TempDir`)
Dir string
// name beginning with prefix
Prefix string
}
type TempOpt func(*tempOpt)
```

TempOpt is struct for configure new tempdir or tempfile
TempOpt is func for configure tempdir or tempfile

#### func TempDir

```go
func TempDir(dir string) TempOpt
```
TempDir set directory where is temp file/dir create, empty string `""` (default)
means TEMPDIR (`os.TempDir`)

#### func TempPrefix

```go
func TempPrefix(prefix string) TempOpt
```
TempPrefix set name beginning with prefix

#### type VisitFunc

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.1
1.0.0
2 changes: 1 addition & 1 deletion append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestAppend(t *testing.T) {
temp, err := NewTempFile(TempOpt{})
temp, err := NewTempFile()
assert.NoError(t, err)

assert.NoError(t, temp.Append("test\n"))
Expand Down
Loading

0 comments on commit 769ce32

Please sign in to comment.