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
use clap::Parser;
use concepts::{ComponentType, ExecutionId, FunctionFqn, prefixed_ulid::ExecutionIdDerived};
use std::path::PathBuf;
pub(crate) mod shadow {
pub(crate) const PKG_VERSION: &str = env!("PKG_VERSION");
}
#[derive(Parser, Debug)]
#[clap(name = "obelisk")]
#[command
(
version = const_format::formatcp!("{}", shadow::PKG_VERSION),
about = "Obelisk: deterministic workflow engine", disable_version_flag = true, disable_help_subcommand = true)]
pub(crate) struct Args {
#[command(subcommand)]
pub(crate) command: Subcommand,
/// Print version
#[arg(short, long, action = clap::ArgAction::Version)]
version: Option<bool>,
}
#[derive(Debug, clap::Subcommand)]
pub(crate) enum Subcommand {
#[command(subcommand)]
Server(Server),
#[command()]
Client(Client),
#[command(subcommand)]
Generate(Generate),
}
#[derive(Debug, clap::Subcommand)]
pub(crate) enum Generate {
/// Generate the Obelisk configuration schema in JSON schema format.
#[cfg(debug_assertions)]
ConfigSchema {
/// Filename to write the schema to, defaults to <stdout>.
output: Option<PathBuf>,
},
/// Generate extension WIT files that are automatically implemented by Obelisk
/// based on the exported interfaces of the component.
WitExtensions {
#[arg(long, short)]
force: bool,
/// One of `workflow`, `activity_wasm`, `activity_stub`, `webhook_endpoint`
component_type: ComponentType,
/// Path to the `wit` folder, containing the target world and possibly `deps` subfolder.
input_wit_directory: PathBuf,
/// Directory where folders and WIT files will be written to.
output_directory: PathBuf,
},
/// Generate Obelisk WIT files for given component type.
WitSupport {
/// One of `workflow`, `activity_wasm`, `activity_stub`, `webhook_endpoint`
component_type: ComponentType,
/// Directory where folders and WIT files will be written to.
output_directory: PathBuf,
},
/// Generate WIT dependency folder based on activities and workflows found in provided TOML configuration.
WitDeps {
/// Path to the TOML configuration.
config: PathBuf,
/// Directory where folders and WIT files will be written to.
output_directory: PathBuf,
/// Overwrite existing files.
#[arg(long, short)]
overwrite: bool,
},
Config {
/// Filename to write the TOML to, defaults to <stdout>.
output: Option<PathBuf>,
},
ExecutionId,
}
#[derive(Debug, clap::Subcommand)]
pub(crate) enum Server {
Run {
/// Clean the sqlite database directory
#[arg(long)]
clean_sqlite_directory: bool,
/// Clean the codegen and OCI cache directories
#[arg(long)]
clean_cache: bool,
/// Clean the codegen cache directory
#[arg(long)]
clean_codegen_cache: bool,
/// Path to the TOML configuration
#[arg(long, short)]
config: Option<PathBuf>,
},
/// Read the configuration, compile the components, verify their imports and exit
Verify {
/// Clean the codegen and OCI cache directories
#[arg(long)]
clean_cache: bool,
/// Clean the codegen cache
#[arg(long)]
clean_codegen_cache: bool,
/// Path to the TOML configuration
#[arg(long, short)]
config: Option<PathBuf>,
/// Do not verify existence of environment variables
#[arg(long, short)]
ignore_missing_env_vars: bool,
},
}
#[derive(Debug, clap::Args)]
pub(crate) struct Client {
/// Address of the obelisk server
#[arg(short, long, default_value = "http://127.0.0.1:5005")]
pub(crate) api_url: String,
#[command(subcommand)]
pub(crate) command: ClientSubcommand,
}
#[derive(Debug, clap::Subcommand)]
pub(crate) enum ClientSubcommand {
#[command(subcommand)]
Component(Component),
#[command(subcommand)]
Execution(Execution),
}
#[derive(Debug, clap::Subcommand)]
pub(crate) enum Component {
/// Parse WASM file and output its metadata.
Inspect {
/// One of `workflow`, `activity_wasm`, `activity_stub`, `webhook_endpoint`
#[arg(required(true))]
component_type: ComponentType,
/// Path to the WASM file
#[arg(required(true))]
path: PathBuf,
/// Show component imports
#[arg(short, long)]
imports: bool,
/// Show auto-generated export extensions
#[arg(short, long)]
extensions: bool,
/// Attempt to convert Core WASM Module to a WASM Component
#[arg(short, long)]
convert_core_module: bool,
},
/// List components.
List {
/// Show component imports
#[arg(short, long)]
imports: bool,
/// Show auto-generated export extensions
#[arg(short, long)]
extensions: bool,
},
/// Push a WASM file to an OCI registry.
Push {
/// WASM file to be pushed
#[arg(required(true))]
path: PathBuf,
/// OCI reference. Example: docker.io/repo/image:tag
#[arg(required(true))]
image_name: oci_client::Reference,
},
}
#[derive(Debug, clap::Subcommand)]
pub(crate) enum Execution {
/// Submit new execution and optionally follow its status stream until the it finishes.
Submit {
#[arg(short, long)]
execution_id: Option<ExecutionId>,
/// Function in the fully qualified format
#[arg(value_name = "function")]
ffqn: FunctionFqn,
/// Follow the stream of events until the execution finishes
#[arg(short, long)]
follow: bool,
/// Do not attempt to reconnect on connection error while following the status stream.
#[arg(long, requires = "follow")]
no_reconnect: bool,
/// Accepted Parameter Formats:
///
/// - JSON array string, e.g. '["first", "second", null, 1]'
///
/// - File reference prefixed with @, e.g. @file.json (file must contain a valid JSON array)
///
/// - Multiple arguments after --, e.g. -- '"first"' @secondparam.json null 1
///
/// - For functions with no parameters: [] (JSON array variant) or no arguments.
#[arg(name = "parameters")]
params: Vec<String>,
},
/// Write a return value or an execution error to an already created stubbed execution.
Stub(Stub),
/// Get the current state of an execution.
Get {
/// Follow the status stream until the execution finishes.
#[arg(short, long)]
follow: bool,
execution_id: ExecutionId,
/// Do not attempt to reconnect on connection error while following the status stream.
#[arg(long, requires = "follow")]
no_reconnect: bool,
},
Cancel(CancelCommand),
}
pub(crate) mod params {
use clap::error::ErrorKind;
use serde_json::Value;
pub(crate) fn parse_params(params: Vec<String>) -> Result<Vec<u8>, clap::Error> {
if params.is_empty() {
Ok("[]".to_string().into_bytes()) // no params, does not matter if `--` was present.
} else if params.len() == 1 && !dashdash() {
let mut params = params;
let json_array = params.pop().expect("checked that len == 1");
// Single JSON Array, or a `@`-prefixed file containing the array.
let json_array = if let Some(file_path) = json_array.strip_prefix('@') {
std::fs::read_to_string(file_path).map_err(|err| {
clap::Error::raw(
ErrorKind::Io,
format!(
"parameter parsing failed: failed to read file '{file_path}': {err}"
),
)
})?
} else {
json_array
};
let json_value = serde_json::from_str(&json_array).map_err(|err| {
clap::Error::raw(
ErrorKind::ValueValidation,
format!("Invalid JSON array for parameters: {err}"),
)
})?;
let Value::Array(_) = &json_value else {
return Err(clap::Error::raw(
ErrorKind::ValueValidation,
"Parameter provided as JSON must be a JSON array.",
));
};
Ok(json_value.to_string().into_bytes())
} else {
// Fallback to raw arguments. Each argument is interpreted as a JSON value or a file starting with `@` that contains the JSON.
let mut parsed_params: Vec<Value> = Vec::new();
for (idx, arg) in params.into_iter().enumerate() {
let arg = if let Some(file_path) = arg.strip_prefix('@') {
std::fs::read_to_string(file_path).map_err(|err| {
clap::Error::raw(
ErrorKind::Io,
format!(
"{}-th parameter parsing failed: failed to read file '{file_path}': {err}",
idx + 1
),
)
})?
} else {
arg
};
let json = serde_json::from_str(&arg).map_err(|err| {
clap::Error::raw(
ErrorKind::ValueValidation,
format!(
"cannot parse {}-th parameter `{arg}` as JSON - {err}",
idx + 1
),
)
})?;
parsed_params.push(json);
}
Ok(Value::Array(parsed_params).to_string().into_bytes())
}
}
fn dashdash() -> bool {
// Ambigous: Either the single JSON Array representing all parameters,
// OR `-- "first-and-only-json-param"`
let mut rev_arg_iter = std::env::args().rev();
rev_arg_iter.next().expect("last arg must be present");
let maybe_separator = rev_arg_iter.next().expect("last-1 arg must be present");
maybe_separator == "--"
}
}
#[derive(Debug, clap::Args)]
#[command()]
pub(crate) struct Stub {
/// Execution ID of the stub execution waiting for its return value.
#[arg(value_name = "EXECUTION_ID")]
pub(crate) execution_id: ExecutionIdDerived,
/// Stub a return value encoded as JSON
#[arg(value_name = "RETURN_VAL")]
pub(crate) return_value: String,
}
#[derive(Debug, clap::Args)]
#[command()]
pub(crate) struct CancelCommand {
#[arg(value_name = "ID")]
pub(crate) id: String,
}