/
input.go
54 lines (48 loc) · 1.23 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package input
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/cweill/gotests/internal/models"
)
// Files returns all the Golang files for the given path. Ignores hidden files.
func Files(srcPath string) ([]models.Path, error) {
srcPath, err := filepath.Abs(srcPath)
if err != nil {
return nil, fmt.Errorf("filepath.Abs: %v\n", err)
}
var fi os.FileInfo
if fi, err = os.Stat(srcPath); err != nil {
return nil, fmt.Errorf("os.Stat: %v\n", err)
}
if fi.IsDir() {
return dirFiles(srcPath)
}
return file(srcPath)
}
func dirFiles(srcPath string) ([]models.Path, error) {
ps, err := filepath.Glob(path.Join(srcPath, "*.go"))
if err != nil {
return nil, fmt.Errorf("filepath.Glob: %v\n", err)
}
var srcPaths []models.Path
for _, p := range ps {
src := models.Path(p)
if isHiddenFile(p) || src.IsTestPath() {
continue
}
srcPaths = append(srcPaths, src)
}
return srcPaths, nil
}
func file(srcPath string) ([]models.Path, error) {
src := models.Path(srcPath)
if filepath.Ext(srcPath) != ".go" || isHiddenFile(srcPath) {
return nil, fmt.Errorf("no Go source files found at %v", srcPath)
}
return []models.Path{src}, nil
}
func isHiddenFile(path string) bool {
return []rune(filepath.Base(path))[0] == '.'
}