-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexec.go
More file actions
240 lines (227 loc) · 6.66 KB
/
exec.go
File metadata and controls
240 lines (227 loc) · 6.66 KB
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
//nolint:lll
package builder
import (
"fmt"
"strings"
"time"
tuikitIO "github.com/flowexec/tuikit/io"
"github.com/flowexec/flow/types/executable"
)
func SimpleExec(opts ...Option) *executable.Executable {
name := "simple-print"
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Cmd: fmt.Sprintf("echo 'hello from %s'", name),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func NamelessExec(opts ...Option) *executable.Executable {
e := &executable.Executable{
Verb: "run",
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Cmd: "echo 'hello from nameless'",
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithPauses(opts ...Option) *executable.Executable {
name := "with-pauses"
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Cmd: fmt.Sprintf(
"echo 'hello from %[1]s'; sleep 1; echo 'hello from %[1]s'; sleep 1; echo 'hello from %[1]s'",
name,
),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithExit(opts ...Option) *executable.Executable {
name := "with-exit"
exitCode := 1
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Cmd: fmt.Sprintf("exit %d", exitCode),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithTimeout(opts ...Option) *executable.Executable {
name := "with-timeout"
timeout := 3 * time.Second
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Timeout: &timeout,
Exec: &executable.ExecExecutableType{
Cmd: fmt.Sprintf("sleep %d", int(timeout.Seconds()+10)),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithTmpDir(opts ...Option) *executable.Executable {
name := "with-tmp-dir"
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Dir: executable.Directory(executable.TmpDirLabel),
Cmd: fmt.Sprintf("echo 'hello from %[1]s';mkdir %[1]s; cd %[1]s; pwd", name),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithArgs(opts ...Option) *executable.Executable {
name := "with-args"
pos := 1
args := executable.ArgumentList{{EnvKey: "ARG1", Pos: &pos}, {EnvKey: "ARG2", Flag: "x", Default: "yz"}}
var argCmds []string
for _, arg := range args {
if arg.Pos != nil && *arg.Pos > 0 {
argCmds = append(argCmds, fmt.Sprintf("echo 'pos=%d, key=%s'", arg.Pos, arg.EnvKey))
} else if arg.Flag != "" {
argCmds = append(argCmds, fmt.Sprintf("echo 'flag=%s, key=%s'", arg.Flag, arg.EnvKey))
}
}
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Args: args,
Cmd: fmt.Sprintf("echo 'hello from %s'; %s", name, strings.Join(argCmds, "; ")),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithParams(opts ...Option) *executable.Executable {
name := "with-params"
params := executable.ParameterList{
{EnvKey: "PARAM1", Text: "value1"},
{EnvKey: "PARAM2", SecretRef: "flow-example-secret"},
{EnvKey: "PARAM3", Prompt: "Enter a value"},
}
var paramCmds []string
for _, param := range params {
switch {
case param.Text != "":
paramCmds = append(paramCmds, fmt.Sprintf(`echo "key=%s, value=$%[1]s"`, param.EnvKey))
case param.SecretRef != "":
paramCmds = append(
paramCmds,
fmt.Sprintf(`echo "key=%s, secret=%s, value=$%[1]s"`, param.EnvKey, param.SecretRef),
)
case param.Prompt != "":
paramCmds = append(
paramCmds,
fmt.Sprintf(`echo "key=%s, prompt=%s value=$%[1]s"`, param.EnvKey, param.Prompt),
)
}
}
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Params: params,
Cmd: fmt.Sprintf("echo 'hello from %s'; %s", name, strings.Join(paramCmds, "; ")),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithLogMode(opts ...Option) *executable.Executable {
name := "with-plaintext"
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
LogMode: tuikitIO.Text,
Cmd: fmt.Sprintf(
"echo 'hello from %s'; echo 'line 2'; echo 'line 3'; echo 'line 4'",
name,
),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}
func ExecWithEnvOutputFiles(opts ...Option) *executable.Executable {
name := "with-file-param"
params := executable.ParameterList{
{Text: "database:\n host: localhost\n port: 5432", OutputFile: "test-config.yaml"},
{Text: "#!/bin/bash\necho 'Hello from script'", OutputFile: "test-script.sh"},
}
pos := 1
args := executable.ArgumentList{
{Pos: &pos, Default: "notme", OutputFile: "output.txt"},
}
cmds := []string{
`echo "param config file content:"; cat test-config.yaml; echo`,
`echo "param script file content:"; cat test-script.sh; echo`,
`echo "arg txt file content:"; cat output.txt; echo`,
}
e := &executable.Executable{
Verb: "run",
Name: name,
Visibility: privateExecVisibility(),
Exec: &executable.ExecExecutableType{
Params: params,
Args: args,
Cmd: fmt.Sprintf("echo 'hello from %s'; %s", name, strings.Join(cmds, "; ")),
},
}
if len(opts) > 0 {
vals := NewOptionValues(opts...)
e.SetContext(vals.WorkspaceName, vals.WorkspacePath, vals.NamespaceName, vals.FlowFilePath)
}
return e
}