forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitpaths.go
58 lines (47 loc) · 1.36 KB
/
gitpaths.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
package baur
import (
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/simplesurance/baur/fs"
"github.com/simplesurance/baur/git"
)
//GitPaths resolves multiple git filepath patterns to paths in the filesystem.
type GitPaths struct {
repositoryRootPath string
relAppPath string
paths []string
}
// NewGitPaths returns a new GitPaths
func NewGitPaths(repositoryRootPath, relAppPath string, gitPaths []string) *GitPaths {
return &GitPaths{
repositoryRootPath: repositoryRootPath,
relAppPath: relAppPath,
paths: gitPaths,
}
}
// Resolve returns a list of files that are matching it's path
func (g *GitPaths) Resolve() ([]BuildInput, error) {
baseDir := filepath.Join(g.repositoryRootPath, g.relAppPath)
arg := strings.Join(g.paths, " ")
out, err := git.LsFiles(baseDir, arg)
if err != nil {
return nil, err
}
paths := strings.Split(out, "\n")
res := make([]BuildInput, 0, len(paths))
for _, p := range paths {
isFile, err := fs.IsFile(filepath.Join(baseDir, p))
if err != nil {
return nil, errors.Wrapf(err, "resolved path %q does not exist", p)
}
if isFile {
res = append(res, NewFile(g.repositoryRootPath, filepath.Join(g.relAppPath, p)))
}
}
return res, nil
}
// String returns the paths, separated by ", "
func (g *GitPaths) String() string {
return strings.Join(g.paths, ", ")
}