-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wagon_stones.go
310 lines (261 loc) · 6.67 KB
/
wagon_stones.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright © 2022 Macaroni OS Linux
See AUTHORS and LICENSE for the license details and contributors.
*/
package repository
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/geaaru/luet/pkg/compiler"
artifact "github.com/geaaru/luet/pkg/compiler/types/artifact"
. "github.com/geaaru/luet/pkg/logger"
pkg "github.com/geaaru/luet/pkg/package"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
type StonesSearchOpts struct {
Packages pkg.Packages
Categories []string
Labels []string
LabelsMatches []string
Matches []string
FilesOwner []string
Annotations []string
Hidden bool
AndCondition bool
WithFiles bool
}
type StonesCatalog struct {
Index compiler.ArtifactIndex `json:"index" yaml:"index"`
}
type Stone struct {
Name string `json:"name" yaml:"name"`
Category string `json:"category" yaml:"category"`
Version string `json:"version" yaml:"version"`
License string `json:"license,omitempty" yaml:"license,omitempty"`
Repository string `json:"repository" yaml:"repository"`
Hidden bool `json:"hidden,omitempty" yaml:"hidden,omitempty"`
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
Annotations map[string]interface{} `json:"annotations,omitempty" yaml:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
}
type StonesPack struct {
Stones []*Stone `json:"stones" yaml:"stones"`
}
type WagonStones struct {
Catalog *StonesCatalog
}
func NewWagonStones() *WagonStones {
return &WagonStones{
Catalog: nil,
}
}
func NewStone(p *artifact.PackageArtifact, repo string, withFiles bool) *Stone {
ans := &Stone{
Name: p.Runtime.Name,
Category: p.Runtime.Category,
Version: p.Runtime.Version,
License: p.Runtime.License,
Repository: repo,
Hidden: p.Runtime.Hidden,
Annotations: p.Runtime.Annotations,
Labels: p.Runtime.Labels,
}
if withFiles {
ans.Files = p.Files
}
return ans
}
func (s *WagonStones) Search(opts *StonesSearchOpts, repoName string) (*[]*Stone, error) {
ans := []*Stone{}
if s.Catalog == nil {
return &ans, nil
}
if len(opts.LabelsMatches) > 0 && len(opts.Matches) > 0 {
return nil, errors.New("Searching for both regex and labels regex is not supported.")
}
// Create regexes array
regs := []*regexp.Regexp{}
lRegs := []*regexp.Regexp{}
catRegs := []*regexp.Regexp{}
if len(opts.Matches) > 0 {
for _, m := range opts.Matches {
r := regexp.MustCompile(m)
if r != nil {
regs = append(regs, r)
}
}
}
if len(opts.LabelsMatches) > 0 {
for _, m := range opts.LabelsMatches {
r := regexp.MustCompile(m)
if r != nil {
lRegs = append(lRegs, r)
}
}
}
if len(opts.Categories) > 0 {
for _, m := range opts.Categories {
r := regexp.MustCompile(m)
if r != nil {
catRegs = append(catRegs, r)
}
}
}
for idx, _ := range s.Catalog.Index {
artifact := s.Catalog.Index[idx]
if artifact.Runtime == nil {
//fmt.Println("ARTIFACT ", artifact, repoName)
Warning(fmt.Sprintf("[%s/%s-%s] Skipping artifact without runtime pkg.",
artifact.CompileSpec.Package.Category,
artifact.CompileSpec.Package.Name,
artifact.CompileSpec.Package.Version,
))
continue
}
if !opts.Hidden && artifact.Runtime.Hidden {
// Exclude hidden packages
continue
}
match := false
// For now only match category and name
if len(opts.Packages) > 0 {
for idx, _ := range opts.Packages {
if artifact.Runtime.Category != opts.Packages[idx].GetCategory() {
continue
}
if artifact.Runtime.Name != opts.Packages[idx].GetName() {
continue
}
match = true
break
}
}
if len(opts.Matches) > 0 {
pstring := artifact.Runtime.PackageName()
for ri, _ := range regs {
if regs[ri].MatchString(pstring) {
match = true
break
}
}
}
if len(opts.LabelsMatches) > 0 {
if opts.AndCondition {
match = false
} else {
goto matched
}
if !match {
for ri, _ := range lRegs {
if artifact.Runtime.MatchLabel(lRegs[ri]) {
match = true
break
}
}
}
}
if len(opts.Labels) > 0 {
if opts.AndCondition {
match = false
} else {
goto matched
}
if !match {
for _, l := range opts.Labels {
if artifact.Runtime.HasLabel(l) {
match = true
break
}
}
}
}
if len(opts.Categories) > 0 {
if opts.AndCondition {
match = false
} else {
goto matched
}
if !match {
for ri, _ := range catRegs {
if catRegs[ri].MatchString(artifact.Runtime.Category) {
match = true
break
}
}
}
}
if len(opts.Annotations) > 0 {
for _, a := range opts.Annotations {
if artifact.Runtime.HasAnnotation(a) {
match = true
break
}
}
}
if len(opts.FilesOwner) > 0 {
if opts.AndCondition {
match = false
}
if len(artifact.Files) > 0 {
for _, f := range opts.FilesOwner {
for fidx, _ := range artifact.Files {
if strings.Index(artifact.Files[fidx], f) >= 0 {
match = true
goto matched
}
}
}
}
}
matched:
if match {
ans = append(ans, NewStone(artifact, repoName, opts.WithFiles))
}
}
return &ans, nil
}
func (s *WagonStones) LoadCatalog(identity *WagonIdentity) (*StonesCatalog, error) {
ans := &StonesCatalog{}
repobasedir := filepath.Dir(identity.IdentityFile)
start := time.Now()
// TODO: Here we need to handle the new repository style
// when ready.
if _, ok := identity.RepositoryFiles[REPOFILE_META_KEY]; ok {
metafs := filepath.Join(repobasedir, "metafs")
metafile := filepath.Join(metafs, REPOSITORY_METAFILE)
Debug(fmt.Sprintf("[%s] Found metafile %s", identity.Name, metafile))
/*
data, err := ioutil.ReadFile(metafile)
if err != nil {
return nil, errors.Wrap(err, "Error on reading file "+metafile)
}
err = yaml.Unmarshal(data, &ans)
if err != nil {
return nil, errors.Wrap(err, "Error on parse file "+metafile)
}
*/
file, err := os.Open(metafile)
if err != nil {
return nil, errors.Wrap(err, "Error on reading file "+metafile)
}
defer file.Close()
//decoder := yaml.NewDecoder(file)
decoder := yaml.NewDecoder(bufio.NewReader(file))
err = decoder.Decode(&ans)
if err != nil {
return nil, errors.Wrap(err, "Error on parse file "+metafile)
}
} else {
return nil, errors.New("No meta field found. Repository is corrupted or to sync.")
}
s.Catalog = ans
Debug(fmt.Sprintf("[%s] metadata loaded in %d µs.", identity.Name,
time.Now().Sub(start).Nanoseconds()/1e3))
return ans, nil
}