/
passthrough.go
75 lines (62 loc) · 1.83 KB
/
passthrough.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
package runner
import (
"path/filepath"
"strings"
"djinn-ci.com/errors"
)
// Passthrough represents files we want passing between the guest and host
// environments. This is a simple map, whereby the key is the source file and
// the value is the destination. Objects and artifacts are the two entities
// that can be passed from one environment to the next.
//
// Objects are passed from the host to the guest. The key for an object
// passthrough represents the source file on the host, and the value represents
// the destination on the guest environment.
//
// Artifacts are passed from the guest to the host. The key for an artifact
// passthrough represents the source file on the guest, and the value represents
// the destination on the host environment.
type Passthrough struct {
Values map[string]string
}
func (p *Passthrough) Set(key, val string) {
if p.Values == nil {
p.Values = make(map[string]string)
}
p.Values[key] = val
}
func (p Passthrough) MarshalYAML() (interface{}, error) {
if p.Values == nil {
return []string{}, nil
}
ss := make([]string, 0, len(p.Values))
for k, v := range p.Values {
ss = append(ss, k+" => "+v)
}
return ss, nil
}
// In the manifest YAML file passthrough is expected to be presented like so:
//
// [source] => [destination]
//
// The [destination] is optional, and if not provided the based of the [source]
// will be used intstead.
func (p *Passthrough) UnmarshalYAML(unmarshal func(interface{}) error) error {
if p.Values == nil {
p.Values = make(map[string]string)
}
ss := make([]string, 0)
if err := unmarshal(&ss); err != nil {
return errors.Err(err)
}
for _, s := range ss {
parts := strings.Split(s, "=>")
key := strings.TrimSpace(parts[0])
val := filepath.Base(key)
if len(parts) > 1 {
val = strings.TrimSpace(parts[1])
}
p.Values[key] = val
}
return nil
}