Skip to content

Commit

Permalink
feat(go): glide, gpm support
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch committed Jun 15, 2018
1 parent 01edf8d commit c6c959f
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 10 deletions.
2 changes: 2 additions & 0 deletions analyzers/golang/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (a *Analyzer) Analyze(m module.Module) (module.Module, error) {
// Resolve dependency imports.
var imports []pkg.Import
for _, i := range gopkg.Imports {
log.Logger.Debugf("Resolving import of: %#v", gopkg)
log.Logger.Debugf("Resolving dependency import: %#v", i)
revision, err := a.Revision(project, r, gopkgMap[i])
if err != nil {
return m, errors.Wrapf(err, "could not resolve %s", i)
Expand Down
4 changes: 4 additions & 0 deletions analyzers/golang/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/fossas/fossa-cli/analyzers/golang/resolver"
"github.com/fossas/fossa-cli/files"
"github.com/fossas/fossa-cli/log"
"github.com/fossas/fossa-cli/monad"
"github.com/pkg/errors"
)
Expand All @@ -18,6 +19,8 @@ var (
// LockfileIn returns the type of lockfile within a directory, or
// ErrNoLockfileInDir if none is found.
func LockfileIn(dirname string) (resolver.Type, error) {
log.Logger.Debugf("%#v", dirname)

either := monad.EitherStr{}
result := either.
Bind(findFile("godep", filepath.Join(dirname, "Godeps", "Godeps.json"))).
Expand All @@ -27,6 +30,7 @@ func LockfileIn(dirname string) (resolver.Type, error) {
Bind(findFile("glide", filepath.Join(dirname, "glide.yaml"))).
Bind(findFile("gdm", filepath.Join(dirname, "Godeps")))
if result.Err != nil {
log.Logger.Debugf("Err: %#v", result.Err.Error())
return "", result.Err
}
if result.Result == "" {
Expand Down
3 changes: 3 additions & 0 deletions analyzers/golang/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package golang

import (
"github.com/fossas/fossa-cli/analyzers/golang/resolver"
"github.com/fossas/fossa-cli/log"
)

// A Project is a single folder that forms a coherent "project" for a developer
Expand Down Expand Up @@ -50,6 +51,8 @@ type Project struct {
//
// Both of these assumptions can be overridden by the user.
func (a *Analyzer) Project(pkg string) (Project, error) {
log.Logger.Debugf("%#v", pkg)

// Check for a cached project.
cached, ok := a.projectCache[pkg]
if ok {
Expand Down
6 changes: 4 additions & 2 deletions analyzers/golang/resolver/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"

"github.com/fossas/fossa-cli/buildtools/dep"
"github.com/fossas/fossa-cli/buildtools/gdm"
"github.com/fossas/fossa-cli/buildtools/glide"
"github.com/fossas/fossa-cli/buildtools/godep"
"github.com/fossas/fossa-cli/buildtools/govendor"
"github.com/fossas/fossa-cli/buildtools/vndr"
Expand All @@ -30,9 +32,9 @@ func FromLockfile(tool Type, dir string) (Resolver, error) {
case Dep:
return dep.New(dir)
case Gdm:
return nil, errors.New("not yet implemented")
return gdm.New(dir)
case Glide:
return nil, errors.New("not yet implemented")
return glide.New(dir)
case Godep:
return godep.New(dir)
case Govendor:
Expand Down
22 changes: 22 additions & 0 deletions buildtools/gdm/gdm.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
package gdm

import (
"errors"

"github.com/fossas/fossa-cli/buildtools/gpm"
"github.com/fossas/fossa-cli/files"
)

// New constructs a gdm lockfile.
func New(dirname string) (gpm.Lockfile, error) {
ok, err := UsedIn(dirname)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("directory does not use gdm")
}

lockfile, err := gpm.New(dirname, "Godeps")
if err != nil {
return nil, err
}

return lockfile, nil
}

// UsedIn checks whether gdm is used correctly within a project folder.
func UsedIn(dirname string) (bool, error) {
return files.Exists(dirname, "Godeps")
}
112 changes: 108 additions & 4 deletions buildtools/glide/glide.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,93 @@
// Package glide provides functions for working with the glide tool.
package glide

import (
"errors"
"path"
"path/filepath"
"time"

"github.com/fossas/fossa-cli/errutil"
"github.com/fossas/fossa-cli/files"
"github.com/fossas/fossa-cli/pkg"
)

// ErrNoLockfile is returned if a dep manifest is found without an accompanying lockfile.
var ErrNoLockfile = errors.New("found Gopkg.toml but not Gopkg.lock")
// ErrNoLockfile is returned if a glide manifest is found without an accompanying lockfile.
var ErrNoLockfile = errors.New("found glide.yaml but not glide.lock")

// An Import is a single imported repository within a glide project.
type Import struct {
Name string
Version string
Subpackages []string
}

// A Lockfile contains the contents of a glide lockfile. Lockfiles are resolvers.
type Lockfile struct {
Hash string
Updated time.Time
Imports []Import
TestImports []Import

normalized map[string]pkg.Import // A normalized map of package import paths to revisions.
}

// Resolve returns the revision of an imported Go package contained within the
// lockfile. If the package is not found, errutil.ErrNoRevisionForPackage is
// returned.
func (l Lockfile) Resolve(importpath string) (pkg.Import, error) {
rev, ok := l.normalized[importpath]
if !ok {
return pkg.Import{}, errutil.ErrNoRevisionForPackage
}
return rev, nil
}

// New constructs a golang.Resolver
func New(dirname string) (Lockfile, error) {
ok, err := UsedIn(dirname)
if err != nil {
return Lockfile{}, err
}
if !ok {
return Lockfile{}, errors.New("directory does not use glide")
}
lockfile, err := ReadRaw(filepath.Join(dirname, "glide.lock"))
if err != nil {
return Lockfile{}, err
}
normalized := make(map[string]pkg.Import)
for _, project := range lockfile.Imports {
for _, subpkg := range project.Subpackages {
importpath := path.Join(project.Name, subpkg)
normalized[importpath] = pkg.Import{
Target: project.Version,
Resolved: pkg.ID{
Type: pkg.Go,
Name: importpath,
Revision: project.Version,
Location: "",
},
}
}
// The repository in glide lockfiles might also be a package, but it won't
// be listed as a _subpackage_.
normalized[project.Name] = pkg.Import{
Target: project.Version,
Resolved: pkg.ID{
Type: pkg.Go,
Name: project.Name,
Revision: project.Version,
Location: "",
},
}
}

lockfile.normalized = normalized
return lockfile, nil
}

// UsedIn checks whether glide is used correctly within a project folder.
func UsedIn(dirname string) (bool, error) {
// Check whether there exists a manifest.
ok, err := files.Exists(dirname, "glide.yaml")
Expand All @@ -22,13 +101,38 @@ func UsedIn(dirname string) (bool, error) {
if err != nil {
return false, err
}
// If both exist, then dep is being used correctly.
// If both exist, then glide is being used correctly.
if ok {
return true, nil
}
// If only a manifest exists, then return ErrNoLockfile.
return true, ErrNoLockfile
}
// If there is no manifest, then dep is not being used.
// If there is no manifest, then glide is not being used.
return false, nil
}

// Read returns the contents of a glide project.
func Read(dirname string) ([]Import, error) {
return ReadFile(filepath.Join(dirname, "glide.lock"))
}

// ReadFile returns the contents of a glide lockfile.
func ReadFile(filename string) ([]Import, error) {
var lockfile Lockfile
err := files.ReadYAML(&lockfile, filename)
if err != nil {
return nil, err
}
return lockfile.Imports, nil
}

// ReadRaw reads a raw Lockfile from a glide.lock file.
func ReadRaw(filename string) (Lockfile, error) {
var lockfile Lockfile
err := files.ReadYAML(&lockfile, filename)
if err != nil {
return Lockfile{}, err
}
return lockfile, nil
}
1 change: 1 addition & 0 deletions buildtools/govendor/govendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (l Lockfile) Resolve(importpath string) (pkg.Import, error) {
log.Logger.Debugf("Trying: %#v", p)
rev, ok := l.normalized[p]
if ok {
rev.Resolved.Name = importpath
return rev, nil
}
}
Expand Down
6 changes: 6 additions & 0 deletions buildtools/gpm/gpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (l Lockfile) ResolveStrict(importpath string) (pkg.Import, error) {
if !ok {
return pkg.Import{}, errutil.ErrNoRevisionForPackage
}
rev.Resolved.Name = importpath
return rev, nil
}

Expand Down Expand Up @@ -81,3 +82,8 @@ func New(pathElems ...string) (Lockfile, error) {

return lockfile, nil
}

// UsedIn checks whether gpm is used conventionally within a project folder.
func UsedIn(dirname string) (bool, error) {
return files.Exists(dirname, "Godeps")
}
15 changes: 14 additions & 1 deletion docker/test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,24 @@ RUN mkdir -p $GOPATH/src/github.com/docker && \
git clone --depth=1 https://github.com/docker/docker && \
git clone --depth=1 https://github.com/docker/docker-ce

## InfluxDB (gdm)
## This is an older version of InfluxDB that still used GDM -- the current
## revision has migrated to Dep.
RUN mkdir -p $GOPATH/src/github.com/influxdata && \
cd $GOPATH/src/github.com/influxdata && \
git clone https://github.com/influxdata/influxdb && \
cd influxdb && \
git reset --hard d606fcf19a5bb81566aac58cd223638a8720be2c && \
go get github.com/influxdata/influxdb/cmd/influx

## rkt (glide)
RUN go get -u -v github.com/rkt/rkt/rkt

# Add FOSSA CLI
ADD . $GOPATH/src/github.com/fossas/fossa-cli
RUN sudo chown -R fossa:fossa $GOPATH/src/github.com/fossas && \
cd $GOPATH/src/github.com/fossas/fossa-cli && \
make

WORKDIR $GOPATH/src/github.com/fossas/fossa-cli
CMD [ "/bin/bash", "./test.sh" ]
CMD [ "/bin/bash", "./test.sh" ]
16 changes: 14 additions & 2 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func fileMode(elem ...string) (os.FileMode, error) {

func Exists(pathElems ...string) (bool, error) {
mode, err := fileMode(pathElems...)
if os.IsNotExist(err) {
if notExistErr(err) {
return false, nil
}
if err != nil {
Expand All @@ -30,7 +30,7 @@ func Exists(pathElems ...string) (bool, error) {

func ExistsFolder(pathElems ...string) (bool, error) {
mode, err := fileMode(pathElems...)
if os.IsNotExist(err) {
if notExistErr(err) {
return false, nil
}
if err != nil {
Expand All @@ -50,3 +50,15 @@ func ReadFile(pathElems ...string) ([]byte, error) {

return contents, err
}

// os.IsNotExist doesn't handle non-existent parent directories e.g.
// stat /some/path/without/a/parent.json: not a directory
func notExistErr(err error) bool {
if os.IsNotExist(err) {
return true
}
if _, ok := err.(*os.PathError); ok {
return true
}
return false
}
12 changes: 11 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ time fossa analyze --output --option allow-unresolved-prefix:"github.com/docker
## Docker CE (vndr)
cd $GOPATH/src/github.com/docker/docker-ce
fossa init
time fossa analyze --output --option allow-unresolved-prefix:"github.com/docker archive/tar" --option allow-external-vendor-prefix:github.com/docker/docker go:./components/engine/cmd/dockerd
time fossa analyze --output --option allow-unresolved-prefix:"github.com/docker archive/tar" --option allow-external-vendor-prefix:github.com/docker/docker go:./components/engine/cmd/dockerd

## InfluxDB (gdm)
cd $GOPATH/src/github.com/influxdata/influxdb
fossa init
time fossa analyze --output --option allow-unresolved-prefix:github.com/influxdata go:./cmd/influxd

## rkt (glide)
cd $GOPATH/src/github.com/rkt/rkt
fossa init
time fossa analyze --output go:./rkt

0 comments on commit c6c959f

Please sign in to comment.