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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use clap::{Parser, Subcommand, ValueEnum, ValueHint};
use clap_complete::engine::{ArgValueCompleter, CompletionCandidate};
use std::path::PathBuf;
use crate::cd::types::{CompilationProfile, ProgramConfig};
use crate::make_client;
/// Autocompletion for pipeline names by trying to fetch them from the server.
fn pipeline_names(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
let mut completions = vec![];
// We parse FELDERA_HOST and FELDERA_API_KEY from the environment
// using the `try_parse_from` method.
let cli = Cli::try_parse_from(["fda", "pipelines"]);
if let Ok(cli) = cli {
let client = make_client(cli.host, cli.auth, cli.timeout).unwrap();
let r = futures::executor::block_on(async {
client
.list_pipelines()
.send()
.await
.map(|r| r.into_inner())
.unwrap_or_else(|_| vec![])
});
let current = current.to_string_lossy();
for pipeline in r {
if pipeline.name.starts_with(current.as_ref()) {
completions.push(CompletionCandidate::new(pipeline.name));
}
}
}
completions
}
#[derive(Parser)]
#[command(
name = "fda",
about = "A CLI to interact with the Feldera REST API.",
version
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
/// The format in which the outputs from feldera should be displayed.
///
/// Note that this flag may have no effect on some commands in case
/// the requested output format is not supported for it.
#[arg(
long,
env = "FELDERA_OUTPUT_FORMAT",
global = true,
help_heading = "Global Options",
default_value = "text"
)]
pub format: OutputFormat,
/// The Feldera host to connect to.
#[arg(
long,
env = "FELDERA_HOST",
value_hint = ValueHint::Url,
global = true,
help_heading = "Global Options",
default_value_t = String::from("https://try.feldera.com")
)]
pub host: String,
/// Which API key to use for authentication.
///
/// The provided string should start with "apikey:" followed by the random characters.
///
/// If not specified, a request without authentication will be used.
#[arg(
long,
env = "FELDERA_API_KEY",
global = true,
hide_env_values = true,
help_heading = "Global Options"
)]
pub auth: Option<String>,
/// The client timeout for requests in seconds.
///
/// In almost all cases you should not need to set this value, but it can
/// be useful to limit the execution of certain commands (e.g., `query` or
/// `logs`).
///
/// By default, no timeout is set.
#[arg(
long,
env = "FELDERA_REQUEST_TIMEOUT",
global = true,
help_heading = "Global Options"
)]
pub timeout: Option<u64>,
}
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq)]
#[value(rename_all = "snake_case")]
pub enum OutputFormat {
/// Return the output in a human-readable text format.
Text,
/// Return the output in JSON format.
///
/// This usually corresponds to the exact response returned from the server.
Json,
}
#[derive(Subcommand)]
pub enum Commands {
/// List the available pipelines.
#[command(next_help_heading = "Pipeline Commands")]
Pipelines,
/// Interact with a pipeline.
///
/// If no sub-command is specified retrieves all configuration data for the pipeline.
#[command(flatten)]
Pipeline(PipelineAction),
/// Manage API keys.
Apikey {
#[command(subcommand)]
action: ApiKeyActions,
},
/// Debugging tools.
Debug {
#[command(subcommand)]
action: DebugActions,
},
}
#[derive(Subcommand)]
pub enum ApiKeyActions {
/// List available API keys
List,
/// Create a new API key
Create {
/// The name of the API key to create
name: String,
},
/// Delete an existing API key
#[clap(aliases = &["del"])]
Delete {
/// The name of the API key to delete
name: String,
},
}
#[derive(Subcommand)]
pub enum DebugActions {
/// Print a MessagePack file, such as `steps.bin` in a checkpoint directory,
/// to stdout.
MsgpCat {
/// The MessagePack file to read.
#[arg(value_hint = ValueHint::FilePath)]
path: PathBuf,
},
}
/// A list of possible configuration options.
#[derive(ValueEnum, Clone, Copy, Debug)]
#[value(rename_all = "snake_case")]
pub enum RuntimeConfigKey {
Workers,
Storage,
FaultTolerance,
CheckpointInterval,
CpuProfiler,
Tracing,
TracingEndpointJaeger,
MinBatchSizeRecords,
MaxBufferingDelayUsecs,
CpuCoresMin,
CpuCoresMax,
MemoryMbMin,
MemoryMbMax,
StorageMbMax,
StorageClass,
MinStorageBytes,
ClockResolutionUsecs,
}
#[derive(ValueEnum, Clone, Copy, Debug)]
#[value(rename_all = "snake_case")]
pub enum Profile {
Dev,
Unoptimized,
Optimized,
}
#[allow(clippy::from_over_into)]
impl Into<CompilationProfile> for Profile {
fn into(self) -> CompilationProfile {
match self {
Profile::Dev => CompilationProfile::Dev,
Profile::Unoptimized => CompilationProfile::Unoptimized,
Profile::Optimized => CompilationProfile::Optimized,
}
}
}
#[allow(clippy::from_over_into)]
impl Into<ProgramConfig> for Profile {
fn into(self) -> ProgramConfig {
ProgramConfig {
profile: Some(self.into()),
cache: true,
}
}
}
#[derive(Subcommand)]
pub enum PipelineAction {
/// Create a new pipeline.
Create {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// A path to a file containing the SQL code.
///
/// If no path is provided, the pipeline will be created with an empty program.
/// See the `stdin` flag for reading from stdin instead.
#[arg(value_hint = ValueHint::FilePath, conflicts_with = "stdin")]
program_path: Option<String>,
/// A path to a file containing the Rust UDF functions.
#[arg(short = 'u', long, value_hint = ValueHint::FilePath, conflicts_with = "stdin")]
udf_rs: Option<String>,
/// A path to the TOML file containing the dependencies for the UDF functions.
#[arg(short = 't', long, value_hint = ValueHint::FilePath, conflicts_with = "stdin")]
udf_toml: Option<String>,
/// The compilation profile to use.
#[arg(default_value = "optimized")]
profile: Profile,
/// Read the program code from stdin.
///
/// EXAMPLES:
///
/// * cat program.sql | fda create p1 -s
/// * echo "SELECT 1" | fda create p2 -s
/// * fda program get p2 | fda create p3 -s
#[arg(
verbatim_doc_comment,
short = 's',
long,
default_value_t = false,
conflicts_with = "program_path"
)]
stdin: bool,
},
/// Start a pipeline.
///
/// If the pipeline is compiling it will wait for the compilation to finish.
Start {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// Force the recompilation of the pipeline before starting.
///
/// This is useful for dev purposes in case the Feldera source-code has changed.
#[arg(long, short = 'r', default_value_t = false)]
recompile: bool,
/// Don't wait for pipeline to reach the status before returning.
#[arg(long, short = 'n', default_value_t = false)]
no_wait: bool,
},
/// Checkpoint a fault-tolerant pipeline.
Checkpoint {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// Pause a pipeline.
Pause {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// Don't wait for pipeline to reach the status before returning.
#[arg(long, short = 'n', default_value_t = false)]
no_wait: bool,
},
/// Shutdown a pipeline, then restart it.
///
/// This is a shortcut for calling `fda shutdown p1 && fda start p1`.
Restart {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// Force the recompilation of the pipeline before starting.
///
/// This is useful for dev purposes in case the Feldera source-code has changed.
#[arg(long, short = 'r', default_value_t = false)]
recompile: bool,
/// Don't wait for pipeline to reach the status before returning.
#[arg(long, short = 'n', default_value_t = false)]
no_wait: bool,
},
/// Shutdown a pipeline.
#[clap(aliases = &["stop"])]
Shutdown {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// Don't wait for pipeline to reach the status before returning.
#[arg(long, short = 'n', default_value_t = false)]
no_wait: bool,
},
/// Retrieve the entire state of a pipeline.
///
/// EXAMPLES:
///
/// - `fda status test | jq .program_info.schema`
///
/// - `fda status test | jq .program_info.input_connectors`
///
/// - `fda status test | jq .deployment_config`
Status {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// Retrieve the runtime statistics of a pipeline.
#[clap(aliases = &["statistics"])]
Stats {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// Retrieve the logs of a pipeline.
#[clap(aliases = &["log"])]
Logs {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// Watch the log endpoint and emit new pipeline log messages as they are written.
///
/// When `true`, the command will listen to pipeline logs until the pipeline terminates or
/// the command is interrupted by the user. When `false`, the command outputs pipeline logs
/// accumulated so far and exits.
#[arg(long, short = 'w', default_value_t = false)]
watch: bool,
},
/// Interact with the program of the pipeline.
///
/// If no sub-command is specified retrieves the program.
Program {
#[command(subcommand)]
action: ProgramAction,
},
/// Retrieve the runtime configuration of a pipeline.
#[clap(aliases = &["cfg"])]
Config {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// Update the runtime configuration of a pipeline.
#[clap(aliases = &["set-cfg"])]
SetConfig {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// The key of the configuration to update.
key: RuntimeConfigKey,
/// The new value for the configuration.
value: String,
},
/// Delete a pipeline.
#[clap(aliases = &["del"])]
Delete {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// Control an input connector belonging to a table of a pipeline.
Connector {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// The name of the table or view.
relation_name: String,
/// The name of the connector.
connector_name: String,
#[command(subcommand)]
action: ConnectorAction,
},
/// Obtains a heap profile for a pipeline.
///
/// By default, or with `--pprof`, this command retrieves the heap profile
/// to a temporary file and then displays it with `pprof`. With `--output`,
/// this command instead writes the heap profile to the specified file.
///
/// Get `pprof` from <https://github.com/google/pprof>. There is at least
/// one other program named `pprof` that is unrelated and will not work.
HeapProfile {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// The `pprof` command to run as a subprocess. The name of the `pprof`
/// file will be provided as an additional command-line argument.
#[arg(long, short = 'p', default_value = "pprof -http :")]
pprof: String,
/// The file to write the profile to.
#[arg(value_hint = ValueHint::FilePath, long, short = 'o')]
output: Option<PathBuf>,
},
/// Enter the ad-hoc SQL shell for a pipeline.
Shell {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// Start the pipeline before entering the shell.
#[arg(long, short = 's', default_value_t = false)]
start: bool,
},
/// Execute an ad-hoc query against a pipeline and return the result.
#[clap(aliases = &["exec"])]
Query {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// The SQL query to execute against the pipeline.
///
/// EXAMPLES:
///
/// * fda exec p1 "SELECT 1;"
#[arg(verbatim_doc_comment, conflicts_with = "stdin")]
sql: Option<String>,
/// Read the SQL query from stdin.
///
/// EXAMPLES:
///
/// * cat query.sql | fda exec p1 -s
/// * echo "SELECT 1" | fda exec p1 -s
#[arg(
verbatim_doc_comment,
short = 's',
long,
default_value_t = false,
conflicts_with = "sql"
)]
stdin: bool,
},
}
#[derive(Subcommand)]
pub enum ProgramAction {
/// Retrieve the program code.
///
/// By default, this returns the SQL code, but you can use the flags to retrieve
/// the Rust UDF code instead.
Get {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// Retrieve the Rust UDF code.
#[arg(short = 'u', long, default_value_t = false)]
udf_rs: bool,
/// Retrieve the TOML dependencies file for the UDF code.
#[arg(short = 't', long, default_value_t = false)]
udf_toml: bool,
},
/// Sets a new program.
Set {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// A path to a file containing the SQL code.
///
/// See the `stdin` flag for reading from stdin instead.
#[arg(value_hint = ValueHint::FilePath, conflicts_with = "stdin")]
program_path: Option<String>,
/// A path to a file containing the Rust UDF functions.
#[arg(short = 'u', long, value_hint = ValueHint::FilePath, conflicts_with = "stdin")]
udf_rs: Option<String>,
/// A path to the TOML file containing the dependencies for the UDF functions.
#[arg(short = 't', long, value_hint = ValueHint::FilePath, conflicts_with = "stdin")]
udf_toml: Option<String>,
/// Read the SQL program code from stdin.
///
/// EXAMPLES:
///
/// * cat program.sql | fda program set p1 -s
/// * echo "SELECT 1" | fda program set p1 -s
/// * fda program get p2 | fda program set p1 -s
#[arg(verbatim_doc_comment, short = 's', long, default_value_t = false)]
stdin: bool,
},
/// Retrieve the configuration of the program.
#[clap(aliases = &["cfg"])]
Config {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// Set the configuration of the program.
#[clap(aliases = &["set-cfg"])]
SetConfig {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// The updated configuration for the pipeline.
///
/// The profile accepts the following values:
/// `dev`, `unoptimized`, `optimized`
profile: Profile,
},
/// Retrieve the compilation status of the program.
Status {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
}
#[derive(Subcommand)]
pub enum ConnectorAction {
Start,
#[clap(aliases = &["stop"])]
Pause,
#[clap(aliases = &["status"])]
Stats,
}