forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputresolver.go
164 lines (126 loc) · 4.28 KB
/
inputresolver.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package baur
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/simplesurance/baur/v1/cfg"
"github.com/simplesurance/baur/v1/internal/log"
"github.com/simplesurance/baur/v1/internal/resolve/gitpath"
"github.com/simplesurance/baur/v1/internal/resolve/glob"
"github.com/simplesurance/baur/v1/internal/resolve/gosource"
)
type InputResolver struct {
gitGlobPathResolver *gitpath.Resolver
globPathResolver *glob.Resolver
goSourceResolver *gosource.Resolver
}
func NewInputResolver() *InputResolver {
return &InputResolver{
gitGlobPathResolver: &gitpath.Resolver{},
globPathResolver: &glob.Resolver{},
goSourceResolver: gosource.NewResolver(log.Debugf),
}
}
// Resolves the input definition of the task to concrete Files.
// If an input definition does not resolve to >= paths, an error is returned.
// The resolved Files are deduplicated.
func (i *InputResolver) Resolve(ctx context.Context, repositoryDir string, task *Task) ([]Input, error) {
goSourcePaths, err := i.resolveGoSrcInputs(ctx, task.Directory, task.UnresolvedInputs.GolangSources)
if err != nil {
return nil, fmt.Errorf("resolving golang source inputs failed: %w", err)
}
gitPaths, err := i.resolveGitGlobPaths(repositoryDir, task.Directory, task.UnresolvedInputs.GitFiles)
if err != nil {
return nil, fmt.Errorf("resolving git-file inputs failed: %w", err)
}
globPaths, err := i.resolveGlobPaths(task.Directory, task.UnresolvedInputs.Files)
if err != nil {
return nil, fmt.Errorf("resolving glob file inputs failed: %w", err)
}
allInputsPaths := make([]string, 0, len(goSourcePaths)+len(globPaths)+len(gitPaths)+1)
allInputsPaths = append(allInputsPaths, gitPaths...)
allInputsPaths = append(allInputsPaths, globPaths...)
allInputsPaths = append(allInputsPaths, goSourcePaths...)
// Add the .app.toml file of the app to the inputs
// TODO: add the files that were included in the .app.toml and it's includes
allInputsPaths = append(allInputsPaths, task.CfgFilepaths...)
uniqInputs, err := i.pathsToUniqInputs(repositoryDir, allInputsPaths)
if err != nil {
return nil, err
}
return uniqInputs, nil
}
func (i *InputResolver) resolveGitGlobPaths(repositoryRootDir, appDir string, inputs []cfg.GitFileInputs) ([]string, error) {
var result []string
for _, in := range inputs {
if len(in.Paths) == 0 {
return nil, nil
}
gitPaths, err := i.gitGlobPathResolver.Resolve(appDir, !in.Optional, in.Paths...)
if err != nil {
return nil, err
}
if !in.Optional && len(gitPaths) == 0 {
return nil, fmt.Errorf("'%s' matched 0 files", strings.Join(in.Paths, ", "))
}
result = append(result, gitPaths...)
}
return result, nil
}
func (i *InputResolver) resolveGlobPaths(appDir string, inputs []cfg.FileInputs) ([]string, error) {
var result []string
for _, in := range inputs {
for _, path := range in.Paths {
var absGlobPath string
if filepath.IsAbs(path) {
absGlobPath = path
} else {
absGlobPath = filepath.Join(appDir, path)
}
resolvedPaths, err := i.globPathResolver.Resolve(absGlobPath)
if err != nil {
return nil, err
}
if !in.Optional && len(resolvedPaths) == 0 {
return nil, fmt.Errorf("'%s' matched 0 files", path)
}
result = append(result, resolvedPaths...)
}
}
return result, nil
}
func (i *InputResolver) resolveGoSrcInputs(ctx context.Context, appDir string, inputs []cfg.GolangSources) ([]string, error) {
var result []string
for _, gs := range inputs {
files, err := i.goSourceResolver.Resolve(ctx, appDir, gs.Environment, gs.BuildFlags, gs.Tests, gs.Queries)
if err != nil {
return nil, err
}
result = append(result, files...)
}
return result, nil
}
func (i *InputResolver) pathsToUniqInputs(repositoryRoot string, pathSlice ...[]string) ([]Input, error) {
var pathsCount int
for _, paths := range pathSlice {
pathsCount += len(paths)
}
res := make([]Input, 0, pathsCount)
dedupMap := make(map[string]struct{}, pathsCount)
for _, paths := range pathSlice {
for _, path := range paths {
if _, exist := dedupMap[path]; exist {
log.Debugf("removed duplicate input %q", path)
continue
}
dedupMap[path] = struct{}{}
relPath, err := filepath.Rel(repositoryRoot, path)
if err != nil {
return nil, err
}
res = append(res, NewFile(repositoryRoot, relPath))
}
}
return res, nil
}