-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
438 lines (370 loc) · 16 KB
/
command.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package riza
import (
"context"
"net/http"
"github.com/riza-io/riza-api-go/internal/apijson"
"github.com/riza-io/riza-api-go/internal/param"
"github.com/riza-io/riza-api-go/internal/requestconfig"
"github.com/riza-io/riza-api-go/option"
)
// CommandService contains methods and other services that help with interacting
// with the riza API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewCommandService] method instead.
type CommandService struct {
Options []option.RequestOption
}
// NewCommandService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewCommandService(opts ...option.RequestOption) (r *CommandService) {
r = &CommandService{}
r.Options = opts
return
}
// Run a script in a secure, isolated environment. Scripts can read from `stdin`
// and write to `stdout` or `stderr`. They can access input files, environment
// variables and command line arguments.
func (r *CommandService) Exec(ctx context.Context, body CommandExecParams, opts ...option.RequestOption) (res *CommandExecResponse, err error) {
opts = append(r.Options[:], opts...)
path := "v1/execute"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Run a function in a secure, isolated environment. Define a function named
// `execute`. The function will be passed `input` as an object.
func (r *CommandService) ExecFunc(ctx context.Context, body CommandExecFuncParams, opts ...option.RequestOption) (res *CommandExecFuncResponse, err error) {
opts = append(r.Options[:], opts...)
path := "v1/execute-function"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
type CommandExecResponse struct {
// The exit code returned by the script. Will often be '0' on success and non-zero
// on failure.
ExitCode int64 `json:"exit_code,required"`
// The contents of 'stderr' after executing the script.
Stderr string `json:"stderr,required"`
// The contents of 'stdout' after executing the script.
Stdout string `json:"stdout,required"`
JSON commandExecResponseJSON `json:"-"`
}
// commandExecResponseJSON contains the JSON metadata for the struct
// [CommandExecResponse]
type commandExecResponseJSON struct {
ExitCode apijson.Field
Stderr apijson.Field
Stdout apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CommandExecResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r commandExecResponseJSON) RawJSON() string {
return r.raw
}
type CommandExecFuncResponse struct {
Execution CommandExecFuncResponseExecution `json:"execution,required"`
// The output of the function.
Output interface{} `json:"output,required"`
// The status of the output. "valid" means your function executed successfully and
// returned a valid JSON-serializable object, or void. "json_serialization_error"
// means your function executed successfully, but returned a nonserializable
// object. "error" means your function failed to execute.
OutputStatus CommandExecFuncResponseOutputStatus `json:"output_status,required"`
JSON commandExecFuncResponseJSON `json:"-"`
}
// commandExecFuncResponseJSON contains the JSON metadata for the struct
// [CommandExecFuncResponse]
type commandExecFuncResponseJSON struct {
Execution apijson.Field
Output apijson.Field
OutputStatus apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CommandExecFuncResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r commandExecFuncResponseJSON) RawJSON() string {
return r.raw
}
type CommandExecFuncResponseExecution struct {
// The exit code returned by the script. Will often be '0' on success and non-zero
// on failure.
ExitCode int64 `json:"exit_code,required"`
// The contents of 'stderr' after executing the script.
Stderr string `json:"stderr,required"`
// The contents of 'stdout' after executing the script.
Stdout string `json:"stdout,required"`
JSON commandExecFuncResponseExecutionJSON `json:"-"`
}
// commandExecFuncResponseExecutionJSON contains the JSON metadata for the struct
// [CommandExecFuncResponseExecution]
type commandExecFuncResponseExecutionJSON struct {
ExitCode apijson.Field
Stderr apijson.Field
Stdout apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CommandExecFuncResponseExecution) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r commandExecFuncResponseExecutionJSON) RawJSON() string {
return r.raw
}
// The status of the output. "valid" means your function executed successfully and
// returned a valid JSON-serializable object, or void. "json_serialization_error"
// means your function executed successfully, but returned a nonserializable
// object. "error" means your function failed to execute.
type CommandExecFuncResponseOutputStatus string
const (
CommandExecFuncResponseOutputStatusError CommandExecFuncResponseOutputStatus = "error"
CommandExecFuncResponseOutputStatusJsonSerializationError CommandExecFuncResponseOutputStatus = "json_serialization_error"
CommandExecFuncResponseOutputStatusValid CommandExecFuncResponseOutputStatus = "valid"
)
func (r CommandExecFuncResponseOutputStatus) IsKnown() bool {
switch r {
case CommandExecFuncResponseOutputStatusError, CommandExecFuncResponseOutputStatusJsonSerializationError, CommandExecFuncResponseOutputStatusValid:
return true
}
return false
}
type CommandExecParams struct {
// The code to execute.
Code param.Field[string] `json:"code,required"`
// The interpreter to use when executing code.
Language param.Field[CommandExecParamsLanguage] `json:"language,required"`
// List of allowed hosts for HTTP requests.
AllowHTTPHosts param.Field[[]string] `json:"allow_http_hosts"`
// List of command line arguments to pass to the script.
Args param.Field[[]string] `json:"args"`
// Set of key-value pairs to add to the script's execution environment.
Env param.Field[map[string]string] `json:"env"`
// List of input files.
Files param.Field[[]CommandExecParamsFile] `json:"files"`
// Configuration for HTTP requests and authentication.
HTTP param.Field[CommandExecParamsHTTP] `json:"http"`
// Configuration for execution environment limits.
Limits param.Field[CommandExecParamsLimits] `json:"limits"`
// The ID of the runtime revision to use when executing code.
RuntimeRevisionID param.Field[string] `json:"runtime_revision_id"`
// Input made available to the script via 'stdin'.
Stdin param.Field[string] `json:"stdin"`
}
func (r CommandExecParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The interpreter to use when executing code.
type CommandExecParamsLanguage string
const (
CommandExecParamsLanguagePython CommandExecParamsLanguage = "python"
CommandExecParamsLanguageJavascript CommandExecParamsLanguage = "javascript"
CommandExecParamsLanguageTypescript CommandExecParamsLanguage = "typescript"
CommandExecParamsLanguageRuby CommandExecParamsLanguage = "ruby"
CommandExecParamsLanguagePhp CommandExecParamsLanguage = "php"
)
func (r CommandExecParamsLanguage) IsKnown() bool {
switch r {
case CommandExecParamsLanguagePython, CommandExecParamsLanguageJavascript, CommandExecParamsLanguageTypescript, CommandExecParamsLanguageRuby, CommandExecParamsLanguagePhp:
return true
}
return false
}
type CommandExecParamsFile struct {
// The contents of the file.
Contents param.Field[string] `json:"contents"`
// The relative path of the file.
Path param.Field[string] `json:"path"`
}
func (r CommandExecParamsFile) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Configuration for HTTP requests and authentication.
type CommandExecParamsHTTP struct {
// List of allowed HTTP hosts and associated authentication.
Allow param.Field[[]CommandExecParamsHTTPAllow] `json:"allow"`
}
func (r CommandExecParamsHTTP) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// List of allowed HTTP hosts and associated authentication.
type CommandExecParamsHTTPAllow struct {
// Authentication configuration for outbound requests to this host.
Auth param.Field[CommandExecParamsHTTPAllowAuth] `json:"auth"`
// The hostname to allow.
Host param.Field[string] `json:"host"`
}
func (r CommandExecParamsHTTPAllow) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Authentication configuration for outbound requests to this host.
type CommandExecParamsHTTPAllowAuth struct {
Basic param.Field[CommandExecParamsHTTPAllowAuthBasic] `json:"basic"`
// Configuration to add an 'Authorization' header using the 'Bearer' scheme.
Bearer param.Field[CommandExecParamsHTTPAllowAuthBearer] `json:"bearer"`
Header param.Field[CommandExecParamsHTTPAllowAuthHeader] `json:"header"`
Query param.Field[CommandExecParamsHTTPAllowAuthQuery] `json:"query"`
}
func (r CommandExecParamsHTTPAllowAuth) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CommandExecParamsHTTPAllowAuthBasic struct {
Password param.Field[string] `json:"password"`
UserID param.Field[string] `json:"user_id"`
}
func (r CommandExecParamsHTTPAllowAuthBasic) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Configuration to add an 'Authorization' header using the 'Bearer' scheme.
type CommandExecParamsHTTPAllowAuthBearer struct {
// The token to set, e.g. 'Authorization: Bearer <token>'.
Token param.Field[string] `json:"token"`
}
func (r CommandExecParamsHTTPAllowAuthBearer) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CommandExecParamsHTTPAllowAuthHeader struct {
Name param.Field[string] `json:"name"`
Value param.Field[string] `json:"value"`
}
func (r CommandExecParamsHTTPAllowAuthHeader) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CommandExecParamsHTTPAllowAuthQuery struct {
Key param.Field[string] `json:"key"`
Value param.Field[string] `json:"value"`
}
func (r CommandExecParamsHTTPAllowAuthQuery) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Configuration for execution environment limits.
type CommandExecParamsLimits struct {
// The maximum time allowed for execution (in seconds). Default is 30.
ExecutionTimeout param.Field[int64] `json:"execution_timeout"`
// The maximum memory allowed for execution (in MiB). Default is 128.
MemorySize param.Field[int64] `json:"memory_size"`
}
func (r CommandExecParamsLimits) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CommandExecFuncParams struct {
// The function to execute. Your code must define a function named "execute" that
// takes in a single argument and returns a JSON-serializable value.
Code param.Field[string] `json:"code,required"`
// The interpreter to use when executing code.
Language param.Field[CommandExecFuncParamsLanguage] `json:"language,required"`
// Set of key-value pairs to add to the function's execution environment.
Env param.Field[map[string]string] `json:"env"`
// List of input files.
Files param.Field[[]CommandExecFuncParamsFile] `json:"files"`
// Configuration for HTTP requests and authentication.
HTTP param.Field[CommandExecFuncParamsHTTP] `json:"http"`
// The input to the function. This must be a valid JSON-serializable object. If you
// do not pass an input, your function will be called with None (Python) or null
// (JavaScript/TypeScript) as the argument.
Input param.Field[interface{}] `json:"input"`
// Configuration for execution environment limits.
Limits param.Field[CommandExecFuncParamsLimits] `json:"limits"`
// The ID of the runtime revision to use when executing code.
RuntimeRevisionID param.Field[string] `json:"runtime_revision_id"`
}
func (r CommandExecFuncParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The interpreter to use when executing code.
type CommandExecFuncParamsLanguage string
const (
CommandExecFuncParamsLanguagePython CommandExecFuncParamsLanguage = "python"
CommandExecFuncParamsLanguageJavascript CommandExecFuncParamsLanguage = "javascript"
CommandExecFuncParamsLanguageTypescript CommandExecFuncParamsLanguage = "typescript"
)
func (r CommandExecFuncParamsLanguage) IsKnown() bool {
switch r {
case CommandExecFuncParamsLanguagePython, CommandExecFuncParamsLanguageJavascript, CommandExecFuncParamsLanguageTypescript:
return true
}
return false
}
type CommandExecFuncParamsFile struct {
// The contents of the file.
Contents param.Field[string] `json:"contents"`
// The relative path of the file.
Path param.Field[string] `json:"path"`
}
func (r CommandExecFuncParamsFile) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Configuration for HTTP requests and authentication.
type CommandExecFuncParamsHTTP struct {
// List of allowed HTTP hosts and associated authentication.
Allow param.Field[[]CommandExecFuncParamsHTTPAllow] `json:"allow"`
}
func (r CommandExecFuncParamsHTTP) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// List of allowed HTTP hosts and associated authentication.
type CommandExecFuncParamsHTTPAllow struct {
// Authentication configuration for outbound requests to this host.
Auth param.Field[CommandExecFuncParamsHTTPAllowAuth] `json:"auth"`
// The hostname to allow.
Host param.Field[string] `json:"host"`
}
func (r CommandExecFuncParamsHTTPAllow) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Authentication configuration for outbound requests to this host.
type CommandExecFuncParamsHTTPAllowAuth struct {
Basic param.Field[CommandExecFuncParamsHTTPAllowAuthBasic] `json:"basic"`
// Configuration to add an 'Authorization' header using the 'Bearer' scheme.
Bearer param.Field[CommandExecFuncParamsHTTPAllowAuthBearer] `json:"bearer"`
Header param.Field[CommandExecFuncParamsHTTPAllowAuthHeader] `json:"header"`
Query param.Field[CommandExecFuncParamsHTTPAllowAuthQuery] `json:"query"`
}
func (r CommandExecFuncParamsHTTPAllowAuth) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CommandExecFuncParamsHTTPAllowAuthBasic struct {
Password param.Field[string] `json:"password"`
UserID param.Field[string] `json:"user_id"`
}
func (r CommandExecFuncParamsHTTPAllowAuthBasic) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Configuration to add an 'Authorization' header using the 'Bearer' scheme.
type CommandExecFuncParamsHTTPAllowAuthBearer struct {
// The token to set, e.g. 'Authorization: Bearer <token>'.
Token param.Field[string] `json:"token"`
}
func (r CommandExecFuncParamsHTTPAllowAuthBearer) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CommandExecFuncParamsHTTPAllowAuthHeader struct {
Name param.Field[string] `json:"name"`
Value param.Field[string] `json:"value"`
}
func (r CommandExecFuncParamsHTTPAllowAuthHeader) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CommandExecFuncParamsHTTPAllowAuthQuery struct {
Key param.Field[string] `json:"key"`
Value param.Field[string] `json:"value"`
}
func (r CommandExecFuncParamsHTTPAllowAuthQuery) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Configuration for execution environment limits.
type CommandExecFuncParamsLimits struct {
// The maximum time allowed for execution (in seconds). Default is 30.
ExecutionTimeout param.Field[int64] `json:"execution_timeout"`
// The maximum memory allowed for execution (in MiB). Default is 128.
MemorySize param.Field[int64] `json:"memory_size"`
}
func (r CommandExecFuncParamsLimits) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}