Skip to content
This repository has been archived by the owner on Mar 2, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
context: fix issue with copying license
Fixes #173
Fixes #168
  • Loading branch information
kardianos committed Jul 19, 2016
1 parent 36255b7 commit 97cf8a3
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 23 deletions.
28 changes: 16 additions & 12 deletions context/copy.go
Expand Up @@ -15,6 +15,7 @@ import (
"strings"

"github.com/kardianos/govendor/internal/pathos"
"github.com/pkg/errors"
)

type fileInfoSort []os.FileInfo
Expand Down Expand Up @@ -66,14 +67,14 @@ func (ctx *Context) CopyPackage(destPath, srcPath, lookRoot, pkgPath string, ign
for _, fi := range fl {
if fi.IsDir() {
if tree {
err = os.RemoveAll(filepath.Join(destPath, fi.Name()))
err = errors.Wrap(os.RemoveAll(filepath.Join(destPath, fi.Name())), "remove all existing tree entries")
if err != nil {
return err
}
}
continue
}
err = os.Remove(filepath.Join(destPath, fi.Name()))
err = errors.Wrap(os.Remove(filepath.Join(destPath, fi.Name())), "remove existing file")
if err != nil {
return err
}
Expand All @@ -82,13 +83,13 @@ func (ctx *Context) CopyPackage(destPath, srcPath, lookRoot, pkgPath string, ign
// Copy files into dest.
srcDir, err := os.Open(srcPath)
if err != nil {
return err
return errors.Wrap(err, "open srcPath directory")
}

fl, err = srcDir.Readdir(-1)
srcDir.Close()
if err != nil {
return err
return errors.Wrap(err, "src readdir")
}
if h != nil {
// Write relative path to GOPATH.
Expand Down Expand Up @@ -127,12 +128,15 @@ fileLoop:
if beforeCopy != nil {
err = beforeCopy(deps)
if err != nil {
return err
return errors.Wrap(err, "beforeCopy")
}
}
err = ctx.CopyPackage(nextDestPath, nextSrcPath, lookRoot, path.Join(pkgPath, name), nextIgnoreFiles, true, h, beforeCopy)
if err != nil {
return err
return errors.Wrapf(err,
"CopyPackage dest=%q src=%q lookRoot=%q pkgPath=%q ignoreFiles=%q tree=%t has beforeCopy=%t",
nextDestPath, nextSrcPath, lookRoot, path.Join(pkgPath, name), nextIgnoreFiles, true, beforeCopy != nil,
)
}
continue
}
Expand All @@ -150,21 +154,21 @@ fileLoop:
h,
)
if err != nil {
return err
return errors.Wrapf(err, "copyFile dest=%q src=%q", filepath.Join(destPath, name), filepath.Join(srcPath, name))
}
}

return licenseCopy(lookRoot, srcPath, filepath.Join(ctx.RootDir, ctx.VendorFolder), pkgPath)
return errors.Wrapf(licenseCopy(lookRoot, srcPath, filepath.Join(ctx.RootDir, ctx.VendorFolder), pkgPath), "licenseCopy srcPath=%q", srcPath)
}

func copyFile(destPath, srcPath string, h hash.Hash) error {
ss, err := os.Stat(srcPath)
if err != nil {
return err
return errors.Wrap(err, "copyFile Stat")
}
src, err := os.Open(srcPath)
if err != nil {
return err
return errors.Wrapf(err, "open src=%q", srcPath)
}
defer src.Close()
// Ensure we are not trying to copy a directory. May happen with symlinks.
Expand All @@ -176,7 +180,7 @@ func copyFile(destPath, srcPath string, h hash.Hash) error {

dest, err := os.Create(destPath)
if err != nil {
return err
return errors.Wrapf(err, "create dest=%q", destPath)
}

r := io.Reader(src)
Expand All @@ -189,7 +193,7 @@ func copyFile(destPath, srcPath string, h hash.Hash) error {
// Close before setting mod and time.
dest.Close()
if err != nil {
return err
return errors.Wrap(err, "copy")
}
err = os.Chmod(destPath, ss.Mode())
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions context/license.go
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/kardianos/govendor/internal/pathos"
"github.com/pkg/errors"
)

type License struct {
Expand Down Expand Up @@ -171,12 +172,13 @@ func licenseCopy(root, startIn, vendorRoot, pkgPath string) error {
destPath := filepath.Join(vendorRoot, addTo, trimTo, name)

// Only copy if file does not exist.
_, err := os.Stat(destPath)
if err == nil {
return nil
_, err := os.Stat(srcPath)
if err != nil {
return errors.Errorf("Source license path doesn't exist %q", srcPath)
}

return copyFile(destPath, srcPath, nil)
destDir, _ := filepath.Split(destPath)
os.MkdirAll(destDir, 0777)
return errors.Wrapf(copyFile(destPath, srcPath, nil), "copyFile dest=%q src=%q", destPath, srcPath)
})
}

Expand Down
14 changes: 9 additions & 5 deletions context/modify.go
Expand Up @@ -11,7 +11,6 @@ import (
"bytes"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"math"
"path"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/kardianos/govendor/pkgspec"
"github.com/kardianos/govendor/vcs"
"github.com/kardianos/govendor/vendorfile"
"github.com/pkg/errors"
)

// OperationState is the state of the given package move operation.
Expand Down Expand Up @@ -697,7 +697,7 @@ func (ctx *Context) Alter() error {
}
}
if err != nil {
return fmt.Errorf("Failed to fetch package %q: %v", op.Pkg.Path, err)
return errors.Wrapf(err, "Failed to fetch package %q", op.Pkg.Path)
}
}
if len(nextOps) == 0 {
Expand Down Expand Up @@ -725,10 +725,14 @@ func (ctx *Context) Alter() error {
err = RemovePackage(op.Src, filepath.Join(ctx.RootDir, ctx.VendorFolder), pkg.IncludeTree)
op.State = OpDone
case OpCopy:
ctx.copyOperation(op, nil)
err = ctx.copyOperation(op, nil)
if os.IsNotExist(errors.Cause(err)) {
// Ignore packages that don't exist, like appengine.
err = nil
}
}
if err != nil {
return fmt.Errorf("Failed to %v package %q -> %q: %v", op.Type, op.Src, op.Dest, err)
return errors.Wrapf(err, "Failed to %v package %q -> %q", op.Type, op.Src, op.Dest)
}
}
if ctx.rewriteImports {
Expand Down Expand Up @@ -756,7 +760,7 @@ func (ctx *Context) copyOperation(op *Operation, beforeCopy func(deps []string)
}
op.State = OpDone
if err != nil {
return fmt.Errorf("copy failed. dest: %q, src: %q, pkgPath %q, err %v", op.Dest, op.Src, root, err)
return errors.Wrapf(err, "copy failed. dest: %q, src: %q, pkgPath %q", op.Dest, op.Src, root)
}
return nil
}
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -37,7 +37,7 @@ func main() {
err = nil
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
fmt.Fprintf(os.Stderr, "Error: %+v\n", err)
}
msgText := msg.String()
if len(msgText) > 0 {
Expand Down
23 changes: 23 additions & 0 deletions vendor/github.com/pkg/errors/LICENSE

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

50 changes: 50 additions & 0 deletions vendor/github.com/pkg/errors/README.md

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

32 changes: 32 additions & 0 deletions vendor/github.com/pkg/errors/appveyor.yml

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

0 comments on commit 97cf8a3

Please sign in to comment.