forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosrcdir.go
76 lines (61 loc) · 1.81 KB
/
gosrcdir.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package baur
import (
"fmt"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/simplesurance/baur/fs"
"github.com/simplesurance/baur/golang/golang"
"github.com/simplesurance/baur/log"
)
// GoSrcDirs resolves Golang source files in directories to files including
// resolving all imports to files
type GoSrcDirs struct {
repositoryRootPath string
relAppPath string
paths []string
gopath string
}
// NewGoSrcDirs returns a GoSrcDirs
func NewGoSrcDirs(repositoryRootPath, relAppPath, gopath string, paths []string) *GoSrcDirs {
return &GoSrcDirs{
repositoryRootPath: repositoryRootPath,
relAppPath: relAppPath,
paths: paths,
gopath: gopath,
}
}
// Resolve returns list of Go src files
func (g *GoSrcDirs) Resolve() ([]BuildInput, error) {
baseDir := filepath.Join(g.repositoryRootPath, g.relAppPath)
fullpaths := make([]string, 0, len(g.paths))
for _, p := range g.paths {
absPath := filepath.Join(baseDir, p)
isDir, err := fs.IsDir(absPath)
if err != nil {
return nil, err
}
if !isDir {
return nil, fmt.Errorf("%q is not a directory", p)
}
fullpaths = append(fullpaths, absPath)
}
log.Debugf("resolving go src files, GOPATH=%q, srcdirs=%q\n", g.gopath, fullpaths)
absSrcPaths, err := golang.SrcFiles(g.gopath, fullpaths...)
if err != nil {
return nil, err
}
res := make([]BuildInput, 0, len(absSrcPaths))
for _, p := range absSrcPaths {
relPath, err := filepath.Rel(g.repositoryRootPath, p)
if err != nil {
return nil, errors.Wrapf(err, "converting %q to relpath with basedir %q failed", p, g.repositoryRootPath)
}
res = append(res, NewFile(g.repositoryRootPath, relPath))
}
return res, nil
}
// String returns the path
func (g *GoSrcDirs) String() string {
return strings.Join(g.paths, ", ")
}