fileutils: copy LICENSE files from repo root - fixes #23
diff --git a/README.md b/README.md index 9db9ac4..5b5071d 100644 --- a/README.md +++ b/README.md
@@ -49,7 +49,7 @@ `gvt fetch` downloads the dependency into the `vendor` folder. -Files and folders starting with `.` or `_` are ignored. Only [files relevant to the Go compiler](https://golang.org/cmd/go/#hdr-File_types) are fetched. +Files and folders starting with `.` or `_` are ignored. Only [files relevant to the Go compiler](https://golang.org/cmd/go/#hdr-File_types) are fetched. LICENSE files are always included, too. Test files and `testdata` folders can be included with `-t`.
diff --git a/fetch.go b/fetch.go index cf8bb2c..13d90fb 100644 --- a/fetch.go +++ b/fetch.go
@@ -130,6 +130,10 @@ return err } + if err := fileutils.CopyLicense(dst, wc.Dir()); err != nil { + return err + } + if err := vendor.WriteManifest(manifestFile(), m); err != nil { return err }
diff --git a/fileutils/fileutils.go b/fileutils/fileutils.go index af05ae9..26f5ad9 100644 --- a/fileutils/fileutils.go +++ b/fileutils/fileutils.go
@@ -4,6 +4,7 @@ import ( "fmt" "io" + "io/ioutil" "os" "path/filepath" "runtime" @@ -21,6 +22,10 @@ ".syso", } +var licenseFiles = []string{ + "LICENSE", "LICENCE", "UNLICENSE", "COPYING", "COPYRIGHT", +} + // Copypath copies the contents of src to dst, excluding any file that is not // relevant to the Go compiler. func Copypath(dst string, src string, tests bool) error { @@ -153,6 +158,27 @@ return os.RemoveAll(path) } +// CopyLicense copies the license file from folder src to folder dst. +func CopyLicense(dst, src string) error { + files, err := ioutil.ReadDir(src) + if err != nil { + return err + } + for _, f := range files { + if f.IsDir() { + continue + } + name := filepath.Base(f.Name()) + for _, candidate := range licenseFiles { + if strings.ToLower(candidate) == strings.TrimSuffix( + strings.TrimSuffix(strings.ToLower(name), ".md"), ".txt") { + return Copyfile(filepath.Join(dst, name), f.Name()) + } + } + } + return nil +} + func mkdir(path string) error { return os.MkdirAll(path, 0755) }
diff --git a/restore.go b/restore.go index d2a3309..27eb024 100644 --- a/restore.go +++ b/restore.go
@@ -124,6 +124,10 @@ return err } + if err := fileutils.CopyLicense(dst, wc.Dir()); err != nil { + return err + } + if err := wc.Destroy(); err != nil { return err }
diff --git a/update.go b/update.go index 1fe48a5..17f0358 100644 --- a/update.go +++ b/update.go
@@ -111,6 +111,10 @@ return err } + if err := fileutils.CopyLicense(dst, wc.Dir()); err != nil { + return err + } + if err := m.AddDependency(dep); err != nil { return err }