/
local.go
96 lines (81 loc) · 2.4 KB
/
local.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
// SPDX-License-Identifier: Apache-2.0
package local
import (
"os"
"reflect"
"sync"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/worker/internal/message"
"github.com/go-vela/worker/runtime"
)
type (
// client manages communication with the pipeline resources.
client struct {
Vela *vela.Client
Runtime runtime.Engine
Hostname string
Version string
// private fields
init *pipeline.Container
build *library.Build
pipeline *pipeline.Build
repo *library.Repo
services sync.Map
steps sync.Map
user *library.User
err error
streamRequests chan message.StreamRequest
// internal field partially exported for tests
stdout *os.File
mockStdoutReader *os.File
}
// MockedClient is for internal use to facilitate testing the local executor.
MockedClient interface {
MockStdout() *os.File
}
)
// MockStdout is for internal use to facilitate testing the local executor.
// MockStdout returns a reader over a mocked Stdout.
func (c *client) MockStdout() *os.File {
return c.mockStdoutReader
}
// equal returns true if the other client is the equivalent.
func Equal(a, b *client) bool {
// handle any nil comparisons
if a == nil || b == nil {
return a == nil && b == nil
}
return reflect.DeepEqual(a.Vela, b.Vela) &&
reflect.DeepEqual(a.Runtime, b.Runtime) &&
a.Hostname == b.Hostname &&
a.Version == b.Version &&
reflect.DeepEqual(a.init, b.init) &&
reflect.DeepEqual(a.build, b.build) &&
reflect.DeepEqual(a.pipeline, b.pipeline) &&
reflect.DeepEqual(a.repo, b.repo) &&
reflect.DeepEqual(&a.services, &b.services) &&
reflect.DeepEqual(&a.steps, &b.steps) &&
reflect.DeepEqual(a.user, b.user) &&
reflect.DeepEqual(a.err, b.err)
}
// New returns an Executor implementation that integrates with the local system.
//
//nolint:revive // ignore unexported type as it is intentional
func New(opts ...Opt) (*client, error) {
// create new local client
c := new(client)
// Add stdout by default
c.stdout = os.Stdout
// instantiate streamRequests channel (which may be overridden using withStreamRequests()).
c.streamRequests = make(chan message.StreamRequest)
// apply all provided configuration options
for _, opt := range opts {
err := opt(c)
if err != nil {
return nil, err
}
}
return c, nil
}