Skip to content

Commit 915f55e

Browse files
committed
refactor(monad): Remove polymorphic monads
1 parent 89661a4 commit 915f55e

File tree

9 files changed

+89
-166
lines changed

9 files changed

+89
-166
lines changed

analyzers/golang/find.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

analyzers/golang/lockfile.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/fossas/fossa-cli/analyzers/golang/resolver"
77
"github.com/fossas/fossa-cli/files"
88
"github.com/fossas/fossa-cli/log"
9-
"github.com/fossas/fossa-cli/monad"
109
"github.com/pkg/errors"
1110
)
1211

@@ -21,22 +20,25 @@ var (
2120
func LockfileIn(dirname string) (resolver.Type, error) {
2221
log.Logger.Debugf("%#v", dirname)
2322

24-
either := monad.EitherStr{}
25-
result := either.
26-
Bind(findFile("godep", filepath.Join(dirname, "Godeps", "Godeps.json"))).
27-
Bind(findFile("govendor", filepath.Join(dirname, "vendor", "vendor.json"))).
28-
Bind(findFile("dep", filepath.Join(dirname, "Gopkg.toml"))).
29-
Bind(findFile("vndr", filepath.Join(dirname, "vendor.conf"))).
30-
Bind(findFile("glide", filepath.Join(dirname, "glide.yaml"))).
31-
Bind(findFile("gdm", filepath.Join(dirname, "Godeps")))
32-
if result.Err != nil {
33-
log.Logger.Debugf("Err: %#v", result.Err.Error())
34-
return "", result.Err
23+
lockfiles := [][2]string{
24+
[2]string{"godep", filepath.Join(dirname, "Godeps", "Godeps.json")},
25+
[2]string{"govendor", filepath.Join(dirname, "vendor", "vendor.json")},
26+
[2]string{"dep", filepath.Join(dirname, "Gopkg.toml")},
27+
[2]string{"vndr", filepath.Join(dirname, "vendor.conf")},
28+
[2]string{"glide", filepath.Join(dirname, "glide.yaml")},
29+
[2]string{"gdm", filepath.Join(dirname, "Godeps")},
3530
}
36-
if result.Result == "" {
37-
return "", ErrNoLockfileInDir
31+
32+
for _, lockfile := range lockfiles {
33+
ok, err := files.Exists(lockfile[1])
34+
if err != nil {
35+
return "", err
36+
}
37+
if ok {
38+
return resolver.Type(lockfile[0]), nil
39+
}
3840
}
39-
return resolver.Type(result.Result), nil
41+
return "", ErrNoLockfileInDir
4042
}
4143

4244
// NearestLockfile returns the type and directory of the nearest lockfile in an

cli.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

files/files_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package files_test
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/fossas/fossa-cli/files"
10+
)
11+
12+
func TestNonExistentParentIsNotErr(t *testing.T) {
13+
ok, err := files.Exists(filepath.Join("testdata", "parent", "does", "not", "exist", "file"))
14+
assert.NoError(t, err)
15+
assert.False(t, ok)
16+
}

files/find.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ package files
33
import (
44
"errors"
55
"path/filepath"
6-
7-
"github.com/fossas/fossa-cli/monad"
86
)
97

10-
// blaf
118
var (
129
ErrDirNotFound = errors.New("no directory found during walk")
1310
ErrStopWalk = errors.New("WalkUp: stop")
@@ -50,20 +47,3 @@ func WalkUp(startdir string, walker WalkUpFunc) (string, error) {
5047
}
5148
return "", ErrDirNotFound
5249
}
53-
54-
// finder, bindFinder, and friends are EitherStr functions for finding whether
55-
// one of many files exist.
56-
type finder func(pathElems ...string) (bool, error)
57-
58-
func BindFinder(name string, find finder, pathElems ...string) monad.EitherStrFunc {
59-
return func(prev string) (string, error) {
60-
ok, err := find(pathElems...)
61-
if err != nil {
62-
return "", err
63-
}
64-
if ok {
65-
return name, nil
66-
}
67-
return prev, nil
68-
}
69-
}

monad/monad.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

vcs/find.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

vcs/types.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package vcs
2+
3+
// VCS represents a type of version control system.
4+
type VCS int
5+
6+
const (
7+
_ VCS = iota
8+
Subversion
9+
Git
10+
Mercurial
11+
Bazaar
12+
)
13+
14+
var Types = [4]VCS{
15+
Subversion,
16+
Git,
17+
Mercurial,
18+
Bazaar,
19+
}
20+
21+
func MetadataFolder(vcs VCS) string {
22+
switch vcs {
23+
case Subversion:
24+
return ".svn"
25+
case Git:
26+
return ".git"
27+
case Mercurial:
28+
return ".hg"
29+
case Bazaar:
30+
return ".bzr"
31+
default:
32+
return ""
33+
}
34+
}

vcs/vcs.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import (
66
"errors"
77
"path/filepath"
88

9-
"github.com/fossas/fossa-cli"
109
"github.com/fossas/fossa-cli/errutil"
1110
"github.com/fossas/fossa-cli/files"
12-
"github.com/fossas/fossa-cli/monad"
1311
)
1412

1513
// Errors that occur when finding VCS repositories.
@@ -20,61 +18,59 @@ var (
2018

2119
// VCSIn returns the type of VCS repository rooted at a directory, or
2220
// ErrNoVCSInDir if none is found.
23-
func vcsIn(dirname string) (cli.VCS, error) {
24-
either := monad.EitherVCS{}
25-
result := either.
26-
BindVCS(findVCSFolder(cli.Git, filepath.Join(dirname, ".git"))).
27-
BindVCS(findVCSFolder(cli.Subversion, filepath.Join(dirname, ".svn"))).
28-
BindVCS(findVCSFolder(cli.Mercurial, filepath.Join(dirname, ".hg"))).
29-
BindVCS(findVCSFolder(cli.Bazaar, filepath.Join(dirname, ".bzr")))
30-
if result.Err != nil {
31-
return 0, result.Err
32-
}
33-
if result.Result == 0 {
34-
return 0, ErrNoVCSInDir
21+
func vcsIn(dirname string) (VCS, error) {
22+
for _, vcs := range Types {
23+
ok, err := files.ExistsFolder(filepath.Join(dirname, MetadataFolder(vcs)))
24+
if err != nil {
25+
return 0, err
26+
}
27+
if ok {
28+
return vcs, nil
29+
}
3530
}
36-
return result.Result, nil
31+
return 0, ErrNoVCSInDir
3732
}
3833

3934
// NearestVCS returns the type and directory of the nearest VCS repository
4035
// containing the directory, or ErrNoNearestVCS if none is found.
41-
func NearestVCS(dirname string) (vcsType cli.VCS, vcsDir string, err error) {
42-
vcsDir, err = files.WalkUp(dirname, func(d string) error {
36+
func NearestVCS(dirname string) (VCS, string, error) {
37+
var vcs VCS
38+
dir, err := files.WalkUp(dirname, func(d string) error {
4339
tool, err := vcsIn(d)
4440
if err == ErrNoVCSInDir {
4541
return nil
4642
}
4743
if err != nil {
4844
return err
4945
}
50-
vcsType = tool
46+
vcs = tool
5147
return files.ErrStopWalk
5248
})
5349
if err == files.ErrDirNotFound {
5450
return 0, "", ErrNoNearestVCS
5551
}
56-
return vcsType, vcsDir, err
52+
return vcs, dir, err
5753
}
5854

5955
// GetRepository returns the location of the repository containing dirname,
6056
// errutil.ErrRepositoryNotFound if none is found, or errutil.ErrNotImplemented
6157
// if an unsupported VCS is found.
6258
func GetRepository(dirname string) (string, error) {
63-
vcsType, vcsDir, err := NearestVCS(dirname)
59+
vcs, dir, err := NearestVCS(dirname)
6460
if err == ErrNoNearestVCS {
6561
return "", errutil.ErrRepositoryNotFound
6662
}
6763
if err != nil {
6864
return "", err
6965
}
70-
switch vcsType {
71-
case cli.Git:
72-
return vcsDir, nil
73-
case cli.Subversion:
66+
switch vcs {
67+
case Git:
68+
return dir, nil
69+
case Subversion:
7470
return "", errutil.ErrNotImplemented
75-
case cli.Mercurial:
71+
case Mercurial:
7672
return "", errutil.ErrNotImplemented
77-
case cli.Bazaar:
73+
case Bazaar:
7874
return "", errutil.ErrNotImplemented
7975
default:
8076
return "", errutil.ErrNotImplemented

0 commit comments

Comments
 (0)