match error for nonGoDirRx

This commit is contained in:
Mark Bates 2019-11-10 18:38:44 -05:00
parent 794943f042
commit ac15f56d3f
2 changed files with 6 additions and 38 deletions

View File

@ -2,6 +2,7 @@ package here
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
@ -9,7 +10,6 @@ import (
// Dir attempts to gather info for the requested directory.
func Dir(p string) (Info, error) {
i, err := Cache(p, func(p string) (Info, error) {
var i Info
fi, err := os.Stat(p)
@ -35,7 +35,10 @@ func Dir(p string) (Info, error) {
// build .: cannot find module for path .
// no Go files in
if err != nil {
return fromNonGoDir(p)
if nonGoDirRx.MatchString(err.Error()) {
return fromNonGoDir(p)
}
return i, fmt.Errorf("%w %s", err, p)
}
if err := json.Unmarshal(b, &i); err != nil {
@ -55,42 +58,6 @@ func Dir(p string) (Info, error) {
}
func dir(p string) (Info, error) {
var i Info
fi, err := os.Stat(p)
if err != nil {
return i, err
}
if !fi.IsDir() {
p = filepath.Dir(p)
}
pwd, err := os.Getwd()
if err != nil {
return i, err
}
defer os.Chdir(pwd)
os.Chdir(p)
b, err := run("go", "list", "-json")
// go: cannot find main module; see 'go help modules'
// build .: cannot find module for path .
// no Go files in
if err != nil {
return fromNonGoDir(p)
}
if err := json.Unmarshal(b, &i); err != nil {
return i, err
}
return i, nil
}
func fromNonGoDir(dir string) (Info, error) {
i := Info{
Dir: dir,

View File

@ -47,6 +47,7 @@ func Test_Dir(t *testing.T) {
}
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
here.ClearCache()
r := require.New(st)
info, err := here.Dir(tt.in)