2 Star 0 Fork 1

mirrors_kardianos/govendor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
cliprompt
context
context.go
context_test.go
copy.go
err.go
fetch.go
label.go
label_test.go
license.go
license_test.go
modify.go
path.go
resolve.go
rewrite.go
status.go
sync.go
syslist.go
vendorFile.go
version.go
version_test.go
doc
internal
migrate
pkgspec
prompt
run
vcs
vendor
vendorfile
.travis.yml
LICENSE
README.md
helpText.go
main.go
克隆/下载
license.go 3.24 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context
import (
"os"
"path/filepath"
"strings"
"github.com/kardianos/govendor/internal/pathos"
)
type licenseSearchType byte
const (
licensePrefix licenseSearchType = iota
licenseSubstring
licenseSuffix
)
type license struct {
Text string
Search licenseSearchType
}
func (t licenseSearchType) Test(filename, test string) bool {
switch t {
case licensePrefix:
return strings.HasPrefix(filename, test)
case licenseSubstring:
return strings.Contains(filename, test)
case licenseSuffix:
return strings.HasSuffix(filename, test)
}
return false
}
type licenseTest interface {
Test(filename, test string) bool
}
// licenses lists the filenames to copy over to the vendor folder.
var licenses = []license{
{Text: "license", Search: licensePrefix},
{Text: "unlicense", Search: licensePrefix},
{Text: "copying", Search: licensePrefix},
{Text: "copyright", Search: licensePrefix},
{Text: "copyright", Search: licensePrefix},
{Text: "legal", Search: licenseSubstring},
{Text: "notice", Search: licenseSubstring},
{Text: "disclaimer", Search: licenseSubstring},
{Text: "patent", Search: licenseSubstring},
{Text: "third-party", Search: licenseSubstring},
{Text: "thirdparty", Search: licenseSubstring},
}
func isLicenseFile(name string) bool {
cname := strings.ToLower(name)
for _, L := range licenses {
if L.Search.Test(cname, L.Text) {
return true
}
}
return false
}
// licenseCopy starts the search in the parent of "startIn" folder.
// Looks in all sub-folders until root is reached. The root itself is not
// searched.
func licenseCopy(root, startIn, vendorRoot, pkgPath string) error {
addTo, _ := pathos.TrimCommonSuffix(pathos.SlashToFilepath(pkgPath), startIn)
folder := filepath.Clean(filepath.Join(startIn, ".."))
for i := 0; i <= looplimit; i++ {
dir, err := os.Open(folder)
if err != nil {
return err
}
fl, err := dir.Readdir(-1)
dir.Close()
if err != nil {
return err
}
for _, fi := range fl {
name := fi.Name()
if name[0] == '.' {
continue
}
if fi.IsDir() {
continue
}
if !isLicenseFile(name) {
continue
}
srcPath := filepath.Join(folder, name)
trimTo := pathos.FileTrimPrefix(getLastVendorRoot(folder), root)
/*
Path: "golang.org/x/tools/go/vcs"
Root: "/tmp/govendor-cache280388238/1"
StartIn: "/tmp/govendor-cache280388238/1/go/vcs"
addTo: "golang.org/x/tools"
$PROJ/vendor + addTo + pathos.FileTrimPrefix(folder, root) + "LICENSE"
*/
destPath := filepath.Join(vendorRoot, addTo, trimTo, name)
// Only copy if file does not exist.
_, err := os.Stat(destPath)
if err == nil {
continue
}
err = copyFile(destPath, srcPath, nil)
if err != nil {
return err
}
}
if len(folder) <= len(root) {
return nil
}
nextFolder := filepath.Clean(filepath.Join(folder, ".."))
if nextFolder == folder {
return nil
}
folder = nextFolder
}
panic("copyLicense loop limit")
}
func getLastVendorRoot(s string) string {
w := strings.Replace(s, "\\", "/", -1)
ix := strings.LastIndex(w, "/vendor/")
if ix < 0 {
return s
}
return s[ix+len("/vendor"):]
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_kardianos/govendor.git
git@gitee.com:mirrors_kardianos/govendor.git
mirrors_kardianos
govendor
govendor
v1.0.0-beta

搜索帮助