Skip to content

Commit

Permalink
bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
enr committed May 22, 2017
1 parent 436fcc3 commit e128d28
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
2 changes: 1 addition & 1 deletion hack/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ GH_OWNER='enr'
GH_REPO='zipp'
DIST_DIR='dist'

APP_VERSION='0.3.2'
APP_VERSION='0.4'
4 changes: 2 additions & 2 deletions vendor/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
{
"importpath": "github.com/enr/go-files/files",
"repository": "https://github.com/enr/go-files",
"revision": "c68ccccba7ac49960496a4d2355cf20732e2393e",
"revision": "7336b98a33ba8c37c4dbf78705c52d70538a8db6",
"branch": "master",
"path": "/files"
},
{
"importpath": "github.com/enr/go-zipext/zipext",
"repository": "https://github.com/enr/go-zipext",
"revision": "76731fea0e2d7485c4beb3be1580112ca093ab83",
"revision": "22db20b398b6c22c2011ec150fbcb75531c433f1",
"branch": "master",
"path": "/zipext"
},
Expand Down
25 changes: 25 additions & 0 deletions vendor/src/github.com/enr/go-files/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,28 @@ func EachLine(path string, walkFn EachLineFunc) error {
}
return err
}

func IsSamePath(p1 string, p2 string) bool {
first, err := normalizedPath(p1)
if err != nil {
panic(err)
}
second, err := normalizedPath(p2)
if err != nil {
panic(err)
}
return first == second
}

func normalizedPath(p string) (string, error) {
np := cleanPath(p)
if np == "" {
return "", nil
}
np = strings.Replace(np, `\`, `/`, -1)
np, err := filepath.Abs(np)
if err != nil {
return "", err
}
return np, nil
}
28 changes: 28 additions & 0 deletions vendor/src/github.com/enr/go-files/files/samepath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package files

import (
"testing"
)

type samepathTestCase struct {
p1 string
p2 string
equals bool
}

var testcases = []samepathTestCase{
{"", " ", true},
{".notfound", "../files/.notfound", true},
{".notfound", `..\files\.notfound`, true},
{".", "../files", true},
{"testdata/", "./testdata", true},
}

func TestSamePath(t *testing.T) {
for _, data := range testcases {
res := IsSamePath(data.p1, data.p2)
if res != data.equals {
t.Errorf(`Expected IsSamePath=%t for paths "%s" and "%s"`, data.equals, data.p1, data.p2)
}
}
}
34 changes: 25 additions & 9 deletions vendor/src/github.com/enr/go-zipext/zipext/zipext.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func addToZip(_path string, tw *zip.Writer, fi os.FileInfo, internalPath string)
// Preferred ReadDir to filepath.Walk because...
// From filepath.Walk docs:
// for very large directories Walk can be inefficient. Walk does not follow symbolic links.
func walkDirectory(startPath string, tw *zip.Writer, basePath string, flat bool) error {
func walkDirectory(startPath string, tw *zip.Writer, basePath string, ctx context) error {
dirPath := filepath.ToSlash(startPath)
dir, err := os.Open(dirPath)
defer dir.Close()
Expand All @@ -148,15 +148,18 @@ func walkDirectory(startPath string, tw *zip.Writer, basePath string, flat bool)
}
for _, fi := range fis {
curPath := dirPath + "/" + fi.Name()
if files.IsSamePath(curPath, ctx.zipPath) {
continue
}
if fi.IsDir() {
err = walkDirectory(curPath, tw, basePath, flat)
err = walkDirectory(curPath, tw, basePath, ctx)
if err != nil {
return err
}
} else {
baseName := filepath.Base(basePath)
if flat {
baseName = ""
baseName := ""
if ctx.createBaseDir {
baseName = filepath.Base(basePath)
}
internalPath := strings.Replace(curPath, basePath, baseName, 1)
internalPath = strings.TrimLeft(internalPath, "/")
Expand All @@ -169,16 +172,29 @@ func walkDirectory(startPath string, tw *zip.Writer, basePath string, flat bool)
return nil
}

type context struct {
createBaseDir bool
zipPath string
}

func CreateFlat(inputPath string, zipPath string) error {
return createZip(inputPath, zipPath, true)
ctx := context {
createBaseDir: false,
zipPath: zipPath,
}
return createZip(inputPath, zipPath, ctx)
}

// if inputPath is a directory the zip will contain the directory
func Create(inputPath string, zipPath string) error {
return createZip(inputPath, zipPath, false)
ctx := context {
createBaseDir: true,
zipPath: zipPath,
}
return createZip(inputPath, zipPath, ctx)
}

func createZip(inputPath string, zipPath string, flat bool) error {
func createZip(inputPath string, zipPath string, ctx context) error {
inPath := strings.TrimSpace(inputPath)
outFilePath := strings.TrimSpace(zipPath)
if inPath == "" || outFilePath == "" {
Expand All @@ -198,7 +214,7 @@ func createZip(inputPath string, zipPath string, flat bool) error {
zw := zip.NewWriter(fw)
defer zw.Close()
if files.IsDir(inPath) {
err = walkDirectory(inPath, zw, inPath, flat)
err = walkDirectory(inPath, zw, inPath, ctx)
if err != nil {
return err
}
Expand Down

0 comments on commit e128d28

Please sign in to comment.