/
input_source.go
67 lines (56 loc) · 1.57 KB
/
input_source.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
package models
import (
"errors"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/rs/zerolog"
"github.com/bacalhau-project/bacalhau/pkg/lib/validate"
)
type InputSource struct {
// Source is the source of the artifact to be downloaded, e.g a URL, S3 bucket, etc.
Source *SpecConfig `json:"Source"`
// Alias is an optional reference to this input source that can be used for
// dynamic linking to this input. (e.g. dynamic import in wasm by alias)
Alias string `json:"Alias"`
// Target is the path where the artifact should be mounted on
Target string `json:"Target"`
}
func (a *InputSource) MarshalZerologObject(e *zerolog.Event) {
e.Str("alias", a.Alias).
Str("target", a.Target).
Object("source", a.Source)
}
// Normalize normalizes the artifact's source and target
func (a *InputSource) Normalize() {
if a.Source == nil {
return
}
a.Source.Normalize()
a.Alias = strings.TrimSpace(a.Alias)
a.Target = strings.TrimSpace(a.Target)
}
// Copy returns a deep copy of the artifact
func (a *InputSource) Copy() *InputSource {
if a == nil {
return nil
}
return &InputSource{
Source: a.Source.Copy(),
Target: a.Target,
}
}
// Validate validates the artifact's source and target
func (a *InputSource) Validate() error {
if a == nil {
return nil
}
var mErr multierror.Error
if err := a.Source.Validate(); err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("invalid artifact source: %w", err))
}
if validate.IsBlank(a.Target) {
mErr.Errors = append(mErr.Errors, errors.New("missing artifact target"))
}
return mErr.ErrorOrNil()
}