Skip to content

Commit

Permalink
refactor(monad): Remove polymorphic monads
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch committed Aug 7, 2018
1 parent 9a99d35 commit e3e1a93
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 166 deletions.
10 changes: 0 additions & 10 deletions analyzers/golang/find.go

This file was deleted.

32 changes: 17 additions & 15 deletions analyzers/golang/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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 @@ -21,22 +20,25 @@ var (
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"))).
Bind(findFile("govendor", filepath.Join(dirname, "vendor", "vendor.json"))).
Bind(findFile("dep", filepath.Join(dirname, "Gopkg.toml"))).
Bind(findFile("vndr", filepath.Join(dirname, "vendor.conf"))).
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
lockfiles := [][2]string{
[2]string{"godep", filepath.Join(dirname, "Godeps", "Godeps.json")},
[2]string{"govendor", filepath.Join(dirname, "vendor", "vendor.json")},
[2]string{"dep", filepath.Join(dirname, "Gopkg.toml")},
[2]string{"vndr", filepath.Join(dirname, "vendor.conf")},
[2]string{"glide", filepath.Join(dirname, "glide.yaml")},
[2]string{"gdm", filepath.Join(dirname, "Godeps")},
}
if result.Result == "" {
return "", ErrNoLockfileInDir

for _, lockfile := range lockfiles {
ok, err := files.Exists(lockfile[1])
if err != nil {
return "", err
}
if ok {
return resolver.Type(lockfile[0]), nil
}
}
return resolver.Type(result.Result), nil
return "", ErrNoLockfileInDir
}

// NearestLockfile returns the type and directory of the nearest lockfile in an
Expand Down
13 changes: 0 additions & 13 deletions cli.go

This file was deleted.

16 changes: 16 additions & 0 deletions files/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package files_test

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

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

func TestNonExistentParentIsNotErr(t *testing.T) {
ok, err := files.Exists(filepath.Join("testdata", "parent", "does", "not", "exist", "file"))
assert.NoError(t, err)
assert.False(t, ok)
}
20 changes: 0 additions & 20 deletions files/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package files
import (
"errors"
"path/filepath"

"github.com/fossas/fossa-cli/monad"
)

// blaf
var (
ErrDirNotFound = errors.New("no directory found during walk")
ErrStopWalk = errors.New("WalkUp: stop")
Expand Down Expand Up @@ -50,20 +47,3 @@ func WalkUp(startdir string, walker WalkUpFunc) (string, error) {
}
return "", ErrDirNotFound
}

// finder, bindFinder, and friends are EitherStr functions for finding whether
// one of many files exist.
type finder func(pathElems ...string) (bool, error)

func BindFinder(name string, find finder, pathElems ...string) monad.EitherStrFunc {
return func(prev string) (string, error) {
ok, err := find(pathElems...)
if err != nil {
return "", err
}
if ok {
return name, nil
}
return prev, nil
}
}
54 changes: 0 additions & 54 deletions monad/monad.go

This file was deleted.

28 changes: 0 additions & 28 deletions vcs/find.go

This file was deleted.

34 changes: 34 additions & 0 deletions vcs/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package vcs

// VCS represents a type of version control system.
type VCS int

const (
_ VCS = iota
Subversion
Git
Mercurial
Bazaar
)

var Types = [4]VCS{
Subversion,
Git,
Mercurial,
Bazaar,
}

func MetadataFolder(vcs VCS) string {
switch vcs {
case Subversion:
return ".svn"
case Git:
return ".git"
case Mercurial:
return ".hg"
case Bazaar:
return ".bzr"
default:
return ""
}
}
48 changes: 22 additions & 26 deletions vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"errors"
"path/filepath"

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

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

// VCSIn returns the type of VCS repository rooted at a directory, or
// ErrNoVCSInDir if none is found.
func vcsIn(dirname string) (cli.VCS, error) {
either := monad.EitherVCS{}
result := either.
BindVCS(findVCSFolder(cli.Git, filepath.Join(dirname, ".git"))).
BindVCS(findVCSFolder(cli.Subversion, filepath.Join(dirname, ".svn"))).
BindVCS(findVCSFolder(cli.Mercurial, filepath.Join(dirname, ".hg"))).
BindVCS(findVCSFolder(cli.Bazaar, filepath.Join(dirname, ".bzr")))
if result.Err != nil {
return 0, result.Err
}
if result.Result == 0 {
return 0, ErrNoVCSInDir
func vcsIn(dirname string) (VCS, error) {
for _, vcs := range Types {
ok, err := files.ExistsFolder(filepath.Join(dirname, MetadataFolder(vcs)))
if err != nil {
return 0, err
}
if ok {
return vcs, nil
}
}
return result.Result, nil
return 0, ErrNoVCSInDir
}

// NearestVCS returns the type and directory of the nearest VCS repository
// containing the directory, or ErrNoNearestVCS if none is found.
func NearestVCS(dirname string) (vcsType cli.VCS, vcsDir string, err error) {
vcsDir, err = files.WalkUp(dirname, func(d string) error {
func NearestVCS(dirname string) (VCS, string, error) {
var vcs VCS
dir, err := files.WalkUp(dirname, func(d string) error {
tool, err := vcsIn(d)
if err == ErrNoVCSInDir {
return nil
}
if err != nil {
return err
}
vcsType = tool
vcs = tool
return files.ErrStopWalk
})
if err == files.ErrDirNotFound {
return 0, "", ErrNoNearestVCS
}
return vcsType, vcsDir, err
return vcs, dir, err
}

// GetRepository returns the location of the repository containing dirname,
// errutil.ErrRepositoryNotFound if none is found, or errutil.ErrNotImplemented
// if an unsupported VCS is found.
func GetRepository(dirname string) (string, error) {
vcsType, vcsDir, err := NearestVCS(dirname)
vcs, dir, err := NearestVCS(dirname)
if err == ErrNoNearestVCS {
return "", errutil.ErrRepositoryNotFound
}
if err != nil {
return "", err
}
switch vcsType {
case cli.Git:
return vcsDir, nil
case cli.Subversion:
switch vcs {
case Git:
return dir, nil
case Subversion:
return "", errutil.ErrNotImplemented
case cli.Mercurial:
case Mercurial:
return "", errutil.ErrNotImplemented
case cli.Bazaar:
case Bazaar:
return "", errutil.ErrNotImplemented
default:
return "", errutil.ErrNotImplemented
Expand Down

0 comments on commit e3e1a93

Please sign in to comment.