Skip to content

Commit

Permalink
initial stab at GH-39
Browse files Browse the repository at this point in the history
  • Loading branch information
frapposelli committed Mar 11, 2024
1 parent 8c1b558 commit 3b50d28
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
22 changes: 16 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ type List struct {
NoColor bool `long:"no-color" description:"disable colored output"`
CoverageThreshold float64 `short:"c" long:"coverage" description:"coverage threshold is the minimum percentage of the file that must contain license text" default:"75"`
CheckTestFiles bool `short:"t" long:"check-test-files" description:"check imported dependencies for test files"`
ExperimentalModCheck bool `long:"experimental-mod-check" description:"inspect packages in go mod cache instead of vendor folder"`
}

type Check struct {
File string `short:"f" long:"file" description:"input file, use - for stdin" default:".wwhrd.yml"`
NoColor bool `long:"no-color" description:"disable colored output"`
CoverageThreshold float64 `short:"c" long:"coverage" description:"coverage threshold is the minimum percentage of the file that must contain license text" default:"75"`
CheckTestFiles bool `short:"t" long:"check-test-files" description:"check imported dependencies for test files"`
ExperimentalModCheck bool `long:"experimental-mod-check" description:"inspect packages in go mod cache instead of vendor folder"`
}

type Graph struct {
Expand Down Expand Up @@ -128,11 +130,19 @@ func (l *List) Execute(args []string) error {
if err != nil {
return err
}

pkgs, err := WalkImports(root, l.CheckTestFiles)
if err != nil {
return err
var pkgs map[string]bool
if l.ExperimentalModCheck {
pkgs, err = WalkImportsFromModCache(root, l.CheckTestFiles)
if err != nil {
return err
}
} else {
pkgs, err = WalkImports(root, l.CheckTestFiles)
if err != nil {
return err
}
}

lics := GetLicenses(root, pkgs, l.CoverageThreshold)

for k, v := range lics {
Expand Down Expand Up @@ -243,15 +253,15 @@ PackageList:
for wc := range exceptionsWildcard {
if strings.HasPrefix(pkg, wc) {
// we have a match
contextLogger.Warn("Found exceptioned package")
contextLogger.Warn("Found exception package")
continue PackageList
}
}
}

// match single-package exceptions
if _, exists := exceptions[pkg]; exists {
contextLogger.Warn("Found exceptioned package")
contextLogger.Warn("Found exception package")
continue PackageList
}

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -8,6 +9,7 @@ github.com/google/licensecheck v0.3.1 h1:QoxgoDkaeC4nFrtGN1jV7IPmDCHFNIVh54e5hSt
github.com/google/licensecheck v0.3.1/go.mod h1:ORkR35t/JjW+emNKtfJDII0zlciG9JgbT7SmsohlHmY=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
84 changes: 81 additions & 3 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/token"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -245,6 +246,69 @@ func (g *dependencies) WalkNode(n *node) {

}

func (g *dependencies) WalkNodeFromModCache(n *node) {
var walkFn = func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

log.Debugf("walking %q", path)

// check if we need to skip this
if ok, err := shouldSkip(path, info, g.checkTest); ok {
return err
}

fs := token.NewFileSet()
f, err := parser.ParseFile(fs, path, nil, parser.ImportsOnly)
if err != nil {
return err
}

for _, s := range f.Imports {
vendorpkg := strings.Replace(s.Path.Value, "\"", "", -1)
log.Debugf("found import %q", vendorpkg)

args := []string{"list", "-mod=mod", "-f", `'{{.Dir}}'`, "-m", vendorpkg}
cmd := exec.Command("go", args...)
log.Debugf("checking import with go command: %q", cmd)
cmd.Env = append(os.Environ(),
"PATH=/usr/local/Cellar/go/1.16.3/libexec/bin",
)
out, err := cmd.Output()
if err != nil {
log.Debugf("error returned: %q", err)
//return err
}
pkgdir := strings.TrimSuffix(string(out), "\n")
pkgdir = strings.Trim(pkgdir, "'")
log.Debugf("import check result: %q", pkgdir)

if _, err := os.Stat(pkgdir); !os.IsNotExist(err) {

// Add imported pkg to the graph
var vendornode = node{pkg: vendorpkg, dir: pkgdir, vendor: n.vendor}
log.Debugf("[%s] adding node", vendornode.pkg)
if err := g.addNode(&vendornode); err != nil {
log.Debug(err.Error())
continue
}
log.Debugf("[%s] adding node as edge of %s", vendornode.pkg, n.pkg)
g.addEdge(n, &vendornode)
log.Debugf("[%s] walking node", vendornode.pkg)
g.WalkNode(&vendornode)
}

}
return nil
}

if err := filepath.Walk(n.dir, walkFn); err != nil {
return
}

}

func WalkImports(root string, checkTest bool) (map[string]bool, error) {

graph := newGraph(checkTest)
Expand All @@ -259,6 +323,20 @@ func WalkImports(root string, checkTest bool) (map[string]bool, error) {
return graph.nodesList, nil
}

func WalkImportsFromModCache(root string, checkTest bool) (map[string]bool, error) {

graph := newGraph(checkTest)
rootNode := node{pkg: "root", dir: root, vendor: root}
if err := graph.addNode(&rootNode); err != nil {
log.Debug(err.Error())
}

log.Debugf("[%s] walking root node", rootNode.pkg)
graph.WalkNodeFromModCache(&rootNode)

return graph.nodesList, nil
}

func GraphImports(root string, checkTest bool) (string, error) {

graph := newGraph(checkTest)
Expand All @@ -282,9 +360,9 @@ func GetLicenses(root string, list map[string]bool, threshold float64) map[strin

var lics = make(map[string]string)

if !strings.HasSuffix(root, "vendor") {
root = filepath.Join(root, "vendor")
}
//if !strings.HasSuffix(root, "vendor") {
// root = filepath.Join(root, "vendor")
//}
log.Debug("Start walking paths for LICENSE discovery")
for k := range list {

Expand Down

0 comments on commit 3b50d28

Please sign in to comment.