/
validation_engine.go
52 lines (43 loc) · 1.43 KB
/
validation_engine.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
package assethook
import (
"bytes"
"context"
"github.com/pkg/errors"
"io"
"time"
"github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2"
)
type validationEngine struct {
processor httpProcessor
}
//go:generate mockery -name=Validator -output=automock -outpkg=automock -case=underscore
type Validator interface {
Validate(ctx context.Context, basePath string, files []string, services []v1alpha2.AssetWebhookService) (Result, error)
}
func NewValidator(httpClient HttpClient, timeout time.Duration, workers int) *validationEngine {
return &validationEngine{
processor: &processor{
timeout: timeout,
workers: workers,
onFail: validationFailureHandler,
continueOnFail: true,
httpClient: httpClient,
},
}
}
func validationFailureHandler(_ context.Context, _, filePath string, responseBody io.Reader, messagesChan chan Message, _ chan error) {
buffer := new(bytes.Buffer)
buffer.ReadFrom(responseBody)
message := Message{Filename: filePath, Message: buffer.String()}
messagesChan <- message
}
func (e *validationEngine) Validate(ctx context.Context, basePath string, files []string, services []v1alpha2.AssetWebhookService) (Result, error) {
results, err := e.processor.Do(ctx, basePath, files, services)
if err != nil {
return Result{}, errors.Wrap(err, "while validating")
}
return Result{
Success: len(results) == 0,
Messages: results,
}, nil
}