forked from vmware-archive/atc
/
resource.go
95 lines (69 loc) · 1.62 KB
/
resource.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
package resource
import (
"io"
"path/filepath"
"sync"
"github.com/concourse/atc"
"github.com/concourse/atc/worker"
"github.com/tedsuo/ifrit"
)
//go:generate counterfeiter . Resource
type Resource interface {
Type() ResourceType
Get(IOConfig, atc.Source, atc.Params, atc.Version) VersionedSource
Put(IOConfig, atc.Source, atc.Params, ArtifactSource) VersionedSource
Check(atc.Source, atc.Version) ([]atc.Version, error)
Release()
Destroy() error
}
type IOConfig struct {
Stdout io.Writer
Stderr io.Writer
}
//go:generate counterfeiter . ArtifactSource
type ArtifactSource interface {
StreamTo(ArtifactDestination) error
}
//go:generate counterfeiter . ArtifactDestination
type ArtifactDestination interface {
StreamIn(string, io.Reader) error
}
//go:generate counterfeiter . VersionedSource
type VersionedSource interface {
ifrit.Runner
Version() atc.Version
Metadata() []atc.MetadataField
StreamOut(string) (io.ReadCloser, error)
StreamIn(string, io.Reader) error
}
func ResourcesDir(suffix string) string {
return filepath.Join("/tmp", "build", suffix)
}
type resource struct {
container worker.Container
typ ResourceType
releaseOnce sync.Once
ScriptFailure bool
}
func NewResource(
container worker.Container,
typ ResourceType,
) Resource {
return &resource{
container: container,
typ: typ,
}
}
func (resource *resource) Type() ResourceType {
return resource.typ
}
func (resource *resource) Release() {
resource.container.Release()
}
func (resource *resource) Destroy() error {
var err error
resource.releaseOnce.Do(func() {
err = resource.container.Destroy()
})
return err
}