/
client.go
260 lines (208 loc) · 5.38 KB
/
client.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package client
import (
"context"
"fmt"
"io"
"path/filepath"
"github.com/go-git/go-billy/v5/osfs"
"github.com/muesli/cancelreader"
runnerv1 "github.com/stateful/runme/internal/gen/proto/go/runme/runner/v1"
"github.com/stateful/runme/internal/runner"
"github.com/stateful/runme/pkg/project"
"go.uber.org/zap"
)
type RunnerOption func(Runner) error
var ErrRunnerClientUnimplemented = fmt.Errorf("method unimplemented")
type RunnerSettings struct {
session *runner.Session
sessionID string
project project.Project
cleanupSession bool
sessionStrategy runnerv1.SessionStrategy
withinShellMaybe bool
customShell string
dir string
stdin io.Reader
stdout io.Writer
stderr io.Writer
logger *zap.Logger
enableBackground bool
insecure bool
tlsDir string
envs []string
}
func (rs *RunnerSettings) Clone() *RunnerSettings {
newRs := *rs
return &newRs
}
type Runner interface {
RunBlock(ctx context.Context, block project.FileCodeBlock) error
DryRunBlock(ctx context.Context, block project.FileCodeBlock, w io.Writer, opts ...RunnerOption) error
Cleanup(ctx context.Context) error
Clone() Runner
GetEnvs(ctx context.Context) ([]string, error)
getSettings() *RunnerSettings
setSettings(settings *RunnerSettings)
}
func withSettings(applySettings func(settings *RunnerSettings)) RunnerOption {
return withSettingsErr(func(settings *RunnerSettings) error {
applySettings(settings)
return nil
})
}
func withSettingsErr(applySettings func(settings *RunnerSettings) error) RunnerOption {
return func(r Runner) error {
return applySettings(r.getSettings())
}
}
func WithSession(s *runner.Session) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.session = s
})
}
func WithSessionID(id string) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.sessionID = id
})
}
func WithProject(proj project.Project) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.project = proj
})
}
func WithCleanupSession(cleanup bool) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.cleanupSession = cleanup
})
}
func WithSessionStrategy(strategy runnerv1.SessionStrategy) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.sessionStrategy = strategy
})
}
func WithinShellMaybe() RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.withinShellMaybe = true
})
}
func WithDir(dir string) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.dir = dir
})
}
func WithStdin(stdin io.Reader) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.stdin = stdin
})
}
func WithStdout(stdout io.Writer) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.stdout = stdout
})
}
func WithStderr(stderr io.Writer) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.stderr = stderr
})
}
func WithStdinTransform(op func(io.Reader) (io.Reader, error)) RunnerOption {
return withSettingsErr(func(rs *RunnerSettings) error {
stdin, err := op(rs.stdin)
if err != nil {
return err
}
rs.stdin = stdin
return nil
})
}
func WithStdoutTransform(op func(io.Writer) io.Writer) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.stdout = op(rs.stdout)
})
}
func WithStderrTransform(op func(io.Writer) io.Writer) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.stderr = op(rs.stderr)
})
}
func WithLogger(logger *zap.Logger) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.logger = logger
})
}
func WithInsecure(insecure bool) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.insecure = insecure
})
}
func WithTLSDir(tlsDir string) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.tlsDir = tlsDir
})
}
func WithEnableBackgroundProcesses(enableBackground bool) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.enableBackground = enableBackground
})
}
func WithEnvs(envs []string) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.envs = envs
})
}
func WithCustomShell(customShell string) RunnerOption {
return withSettings(func(rs *RunnerSettings) {
rs.customShell = customShell
})
}
func WithTempSettings(rc Runner, opts []RunnerOption, cb func() error) error {
oldSettings := rc.getSettings().Clone()
err := ApplyOptions(rc, opts...)
if err != nil {
return err
}
err = cb()
rc.setSettings(oldSettings)
if err != nil {
return err
}
return nil
}
func ApplyOptions(rc Runner, opts ...RunnerOption) error {
for _, opt := range opts {
if err := opt(rc); err != nil {
return err
}
}
return nil
}
func WrapWithCancelReader() RunnerOption {
return WithStdinTransform(func(r io.Reader) (io.Reader, error) {
return cancelreader.NewReader(r)
})
}
func ResolveDirectory(parentDir string, fileBlock project.FileCodeBlock) string {
for _, dir := range []string{
filepath.Dir(fileBlock.GetFile()),
filepath.FromSlash(fileBlock.GetFrontmatter().Cwd),
filepath.FromSlash(fileBlock.GetBlock().Cwd()),
} {
newDir := resolveOrAbsolute(parentDir, dir)
if stat, err := osfs.Default.Stat(newDir); err == nil && stat.IsDir() {
parentDir = newDir
}
}
return parentDir
}
func resolveOrAbsolute(parent string, child string) string {
if child == "" {
return parent
}
if filepath.IsAbs(child) {
return child
}
if parent != "" {
return filepath.Join(parent, child)
}
return child
}