Skip to content

Commit e4bf442

Browse files
committed
feat(go): Manifest strategies
1 parent c6c959f commit e4bf442

File tree

10 files changed

+96
-97
lines changed

10 files changed

+96
-97
lines changed

analyzers/golang/analyze.go

+48-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ package golang
22

33
import (
44
"github.com/fossas/fossa-cli/analyzers/golang/resolver"
5+
"github.com/fossas/fossa-cli/buildtools/dep"
6+
"github.com/fossas/fossa-cli/buildtools/gdm"
7+
"github.com/fossas/fossa-cli/buildtools/glide"
58
"github.com/fossas/fossa-cli/buildtools/gocmd"
9+
"github.com/fossas/fossa-cli/buildtools/godep"
10+
"github.com/fossas/fossa-cli/buildtools/govendor"
11+
"github.com/fossas/fossa-cli/buildtools/vndr"
612
"github.com/fossas/fossa-cli/errutil"
713
"github.com/fossas/fossa-cli/log"
814
"github.com/fossas/fossa-cli/module"
@@ -25,20 +31,54 @@ func (a *Analyzer) Analyze(m module.Module) (module.Module, error) {
2531
// Read lockfiles to get revisions.
2632
var r resolver.Resolver
2733
switch a.Options.Strategy {
28-
// Read revisions from a tool manifest at a specified location.
29-
// TODO: implement these strategies.
3034
case "manifest:dep":
31-
return m, errutil.ErrNotImplemented
35+
if a.Options.LockfilePath == "" {
36+
return m, errors.New("manifest strategy specified without lockfile path")
37+
}
38+
r, err = dep.FromFile(a.Options.LockfilePath)
39+
if err != nil {
40+
return m, err
41+
}
3242
case "manifest:gdm":
33-
return m, errutil.ErrNotImplemented
43+
if a.Options.LockfilePath == "" {
44+
return m, errors.New("manifest strategy specified without lockfile path")
45+
}
46+
r, err = gdm.FromFile(a.Options.LockfilePath)
47+
if err != nil {
48+
return m, err
49+
}
3450
case "manifest:glide":
35-
return m, errutil.ErrNotImplemented
51+
if a.Options.LockfilePath == "" {
52+
return m, errors.New("manifest strategy specified without lockfile path")
53+
}
54+
r, err = glide.FromFile(a.Options.LockfilePath)
55+
if err != nil {
56+
return m, err
57+
}
3658
case "manifest:godep":
37-
return m, errutil.ErrNotImplemented
59+
if a.Options.LockfilePath == "" {
60+
return m, errors.New("manifest strategy specified without lockfile path")
61+
}
62+
r, err = godep.FromFile(a.Options.LockfilePath)
63+
if err != nil {
64+
return m, err
65+
}
3866
case "manifest:govendor":
39-
return m, errutil.ErrNotImplemented
67+
if a.Options.LockfilePath == "" {
68+
return m, errors.New("manifest strategy specified without lockfile path")
69+
}
70+
r, err = govendor.FromFile(a.Options.LockfilePath)
71+
if err != nil {
72+
return m, err
73+
}
4074
case "manifest:vndr":
41-
return m, errutil.ErrNotImplemented
75+
if a.Options.LockfilePath == "" {
76+
return m, errors.New("manifest strategy specified without lockfile path")
77+
}
78+
r, err = vndr.FromFile(a.Options.LockfilePath)
79+
if err != nil {
80+
return m, err
81+
}
4282

4383
// Resolve revisions by traversing the local $GOPATH and calling the package's
4484
// VCS.

buildtools/dep/dep.go

+3-18
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func New(dirname string) (Lockfile, error) {
4949
if !ok {
5050
return Lockfile{}, errors.New("directory does not use dep")
5151
}
52-
lockfile, err := ReadRaw(filepath.Join(dirname, "Gopkg.lock"))
52+
lockfile, err := FromFile(filepath.Join(dirname, "Gopkg.lock"))
5353
if err != nil {
5454
return Lockfile{}, err
5555
}
@@ -98,23 +98,8 @@ func UsedIn(dirname string) (bool, error) {
9898
return false, nil
9999
}
100100

101-
// Read returns the contents of a dep project.
102-
func Read(dirname string) ([]Project, error) {
103-
return ReadFile(filepath.Join(dirname, "Gopkg.lock"))
104-
}
105-
106-
// ReadFile returns the contents of a dep lockfile.
107-
func ReadFile(filename string) ([]Project, error) {
108-
var lockfile Lockfile
109-
err := files.ReadTOML(&lockfile, filename)
110-
if err != nil {
111-
return nil, err
112-
}
113-
return lockfile.Projects, nil
114-
}
115-
116-
// ReadRaw reads a raw Lockfile from a Gopkg.lock file.
117-
func ReadRaw(filename string) (Lockfile, error) {
101+
// FromFile reads a raw Lockfile from a Gopkg.lock file.
102+
func FromFile(filename string) (Lockfile, error) {
118103
var lockfile Lockfile
119104
err := files.ReadTOML(&lockfile, filename)
120105
if err != nil {

buildtools/gdm/gdm.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package gdm implements a Go package resolver for the gdm tool.
12
package gdm
23

34
import (
@@ -7,7 +8,7 @@ import (
78
"github.com/fossas/fossa-cli/files"
89
)
910

10-
// New constructs a gdm lockfile.
11+
// New constructs a gdm lockfile from a project directory.
1112
func New(dirname string) (gpm.Lockfile, error) {
1213
ok, err := UsedIn(dirname)
1314
if err != nil {
@@ -17,15 +18,20 @@ func New(dirname string) (gpm.Lockfile, error) {
1718
return nil, errors.New("directory does not use gdm")
1819
}
1920

20-
lockfile, err := gpm.New(dirname, "Godeps")
21+
lockfile, err := FromFile(dirname, "Godeps")
2122
if err != nil {
2223
return nil, err
2324
}
2425

2526
return lockfile, nil
2627
}
2728

28-
// UsedIn checks whether gdm is used correctly within a project folder.
29+
// UsedIn checks whether gdm is used correctly within a project directory.
2930
func UsedIn(dirname string) (bool, error) {
3031
return files.Exists(dirname, "Godeps")
3132
}
33+
34+
// FromFile constructs a gdm lockfile from a specific file.
35+
func FromFile(pathElems ...string) (gpm.Lockfile, error) {
36+
return gpm.FromFile(pathElems...)
37+
}

buildtools/glide/glide.go

+3-18
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func New(dirname string) (Lockfile, error) {
5252
if !ok {
5353
return Lockfile{}, errors.New("directory does not use glide")
5454
}
55-
lockfile, err := ReadRaw(filepath.Join(dirname, "glide.lock"))
55+
lockfile, err := FromFile(filepath.Join(dirname, "glide.lock"))
5656
if err != nil {
5757
return Lockfile{}, err
5858
}
@@ -112,23 +112,8 @@ func UsedIn(dirname string) (bool, error) {
112112
return false, nil
113113
}
114114

115-
// Read returns the contents of a glide project.
116-
func Read(dirname string) ([]Import, error) {
117-
return ReadFile(filepath.Join(dirname, "glide.lock"))
118-
}
119-
120-
// ReadFile returns the contents of a glide lockfile.
121-
func ReadFile(filename string) ([]Import, error) {
122-
var lockfile Lockfile
123-
err := files.ReadYAML(&lockfile, filename)
124-
if err != nil {
125-
return nil, err
126-
}
127-
return lockfile.Imports, nil
128-
}
129-
130-
// ReadRaw reads a raw Lockfile from a glide.lock file.
131-
func ReadRaw(filename string) (Lockfile, error) {
115+
// FromFile reads a raw Lockfile from a glide.lock file.
116+
func FromFile(filename string) (Lockfile, error) {
132117
var lockfile Lockfile
133118
err := files.ReadYAML(&lockfile, filename)
134119
if err != nil {

buildtools/godep/godep.go

+3-18
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func New(dirname string) (Lockfile, error) {
4545
if !ok {
4646
return Lockfile{}, errors.New("directory does not use godep")
4747
}
48-
lockfile, err := ReadRaw(filepath.Join(dirname, "Godeps", "Godeps.json"))
48+
lockfile, err := FromFile(filepath.Join(dirname, "Godeps", "Godeps.json"))
4949
if err != nil {
5050
return Lockfile{}, err
5151
}
@@ -71,23 +71,8 @@ func UsedIn(dirname string) (bool, error) {
7171
return files.Exists(dirname, "Godeps", "Godeps.json")
7272
}
7373

74-
// Read returns the contents of a godep project.
75-
func Read(dirname string) ([]Package, error) {
76-
return ReadFile(filepath.Join(dirname, "Godeps", "Godeps.json"))
77-
}
78-
79-
// ReadFile returns the contents of a godep lockfile.
80-
func ReadFile(filename string) ([]Package, error) {
81-
var lockfile Lockfile
82-
err := files.ReadJSON(&lockfile, filename)
83-
if err != nil {
84-
return nil, err
85-
}
86-
return lockfile.Deps, nil
87-
}
88-
89-
// ReadRaw reads a raw Lockfile from a Godeps/Godeps.json file.
90-
func ReadRaw(filename string) (Lockfile, error) {
74+
// FromFile reads a raw Lockfile from a Godeps/Godeps.json file.
75+
func FromFile(filename string) (Lockfile, error) {
9176
var lockfile Lockfile
9277
err := files.ReadJSON(&lockfile, filename)
9378
if err != nil {

buildtools/govendor/govendor.go

+3-18
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func New(dirname string) (Lockfile, error) {
5353
if !ok {
5454
return Lockfile{}, errors.New("directory does not use govendor")
5555
}
56-
lockfile, err := ReadRaw(filepath.Join(dirname, "vendor", "vendor.json"))
56+
lockfile, err := FromFile(filepath.Join(dirname, "vendor", "vendor.json"))
5757
if err != nil {
5858
return Lockfile{}, err
5959
}
@@ -79,23 +79,8 @@ func UsedIn(dirname string) (bool, error) {
7979
return files.Exists(dirname, "vendor", "vendor.json")
8080
}
8181

82-
// Read returns the contents of a govendor project.
83-
func Read(dirname string) ([]Package, error) {
84-
return ReadFile(filepath.Join(dirname, "vendor", "vendor.json"))
85-
}
86-
87-
// ReadFile returns the contents of a govendor lockfile.
88-
func ReadFile(filename string) ([]Package, error) {
89-
var lockfile Lockfile
90-
err := files.ReadJSON(&lockfile, filename)
91-
if err != nil {
92-
return nil, err
93-
}
94-
return lockfile.Package, nil
95-
}
96-
97-
// ReadRaw reads a raw Lockfile from a vendor/vendor.json file.
98-
func ReadRaw(filename string) (Lockfile, error) {
82+
// FromFile reads a raw Lockfile from a vendor/vendor.json file.
83+
func FromFile(filename string) (Lockfile, error) {
9984
var lockfile Lockfile
10085
err := files.ReadJSON(&lockfile, filename)
10186
if err != nil {

buildtools/gpm/gpm.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func (l Lockfile) ResolveStrict(importpath string) (pkg.Import, error) {
3737
return rev, nil
3838
}
3939

40-
// New reads and parses a GPM-format lockfile.
41-
func New(pathElems ...string) (Lockfile, error) {
40+
// FromFile reads and parses a GPM-format lockfile.
41+
func FromFile(filename ...string) (Lockfile, error) {
4242
// Read lockfile.
43-
data, err := files.ReadFile(pathElems...)
43+
data, err := files.ReadFile(filename...)
4444
if err != nil {
4545
return nil, err
4646
}

buildtools/vndr/vndr.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func New(dirname string) (gpm.Lockfile, error) {
1717
return nil, errors.New("directory does not use vndr")
1818
}
1919

20-
lockfile, err := gpm.New(dirname, "vendor.conf")
20+
lockfile, err := gpm.FromFile(dirname, "vendor.conf")
2121
if err != nil {
2222
return nil, err
2323
}
@@ -29,3 +29,7 @@ func New(dirname string) (gpm.Lockfile, error) {
2929
func UsedIn(dirname string) (bool, error) {
3030
return files.Exists(dirname, "vendor.conf")
3131
}
32+
33+
func FromFile(filename string) (gpm.Lockfile, error) {
34+
return gpm.FromFile(filename)
35+
}

files/files.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package files implements utility routines for finding and reading files.
12
package files
23

34
import (

files/unmarshal.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,44 @@ package files
22

33
import (
44
"encoding/json"
5+
"path/filepath"
56

67
"github.com/BurntSushi/toml"
78
yaml "gopkg.in/yaml.v2"
89

910
"github.com/fossas/fossa-cli/log"
1011
)
1112

12-
func ReadJSON(v interface{}, path string) error {
13-
return ReadUnmarshal(v, path, json.Unmarshal)
13+
// ReadJSON reads and unmarshals a file as if it contained JSON.
14+
func ReadJSON(v interface{}, pathElems ...string) error {
15+
return ReadUnmarshal(json.Unmarshal, v, pathElems...)
1416
}
1517

16-
func ReadTOML(v interface{}, path string) error {
17-
return ReadUnmarshal(v, path, toml.Unmarshal)
18+
// ReadTOML reads and unmarshals a file as if it contained TOML.
19+
func ReadTOML(v interface{}, pathElems ...string) error {
20+
return ReadUnmarshal(toml.Unmarshal, v, pathElems...)
1821
}
1922

20-
func ReadYAML(v interface{}, path string) error {
21-
return ReadUnmarshal(v, path, yaml.Unmarshal)
23+
// ReadYAML reads and unmarshals a file as if it contained YAML.
24+
func ReadYAML(v interface{}, pathElems ...string) error {
25+
return ReadUnmarshal(yaml.Unmarshal, v, pathElems...)
2226
}
2327

28+
// An UnmarshalFunc is a function for unmarshalling bytes into values.
2429
type UnmarshalFunc func(data []byte, v interface{}) error
2530

26-
func ReadUnmarshal(v interface{}, path string, unmarshal UnmarshalFunc) error {
27-
log.Logger.Debugf("Parsing file `%s`", path)
28-
contents, err := ReadFile(path)
31+
// ReadUnmarshal reads a file and then unmarshals its contents using an
32+
// UnmarshalFunc.
33+
func ReadUnmarshal(unmarshal UnmarshalFunc, v interface{}, pathElems ...string) error {
34+
filename := filepath.Join(pathElems...)
35+
log.Logger.Debugf("Parsing file `%s`", filename)
36+
contents, err := ReadFile(pathElems...)
2937
if err != nil {
3038
return err
3139
}
3240
err = unmarshal(contents, v)
3341
if err != nil {
34-
log.Logger.Debugf("Could not parse file `%s`: %s", path, err.Error())
42+
log.Logger.Debugf("Could not parse file `%s`: %s", filename, err.Error())
3543
}
3644
return err
3745
}

0 commit comments

Comments
 (0)