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
use clap::{Parser, Subcommand, ValueEnum, ValueHint};
use clap_complete::engine::{ArgValueCompleter, CompletionCandidate};
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.")]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
/// The format in which the output should be displayed.
#[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 change this value.
/// It can be helpful to increase this if you want to evaluate long-running
/// ad-hoc queries in the shell.
#[arg(
long,
env = "FELDERA_REQUEST_TIMEOUT",
global = true,
help_heading = "Global Options",
default_value_t = 120
)]
pub timeout: u64,
}
#[derive(ValueEnum, Clone, Copy, Debug)]
#[value(rename_all = "snake_case")]
pub enum OutputFormat {
Text,
}
#[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,
},
}
#[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,
},
}
/// A list of possible configuration options.
#[derive(ValueEnum, Clone, Copy, Debug)]
#[value(rename_all = "snake_case")]
pub enum RuntimeConfigKey {
Workers,
Storage,
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()),
}
}
}
#[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.
///
/// See the `stdin` flag for reading from stdin instead.
#[arg(value_hint = ValueHint::FilePath)]
program_path: 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 - dev
/// * fda program p2 | fda create p3 -s -
#[arg(verbatim_doc_comment, short = 's', long, default_value_t = false)]
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,
},
/// Pause a pipeline.
Pause {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// 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,
},
/// Shutdown a pipeline.
#[clap(aliases = &["stop"])]
Shutdown {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
},
/// 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,
},
/// Interact with the program of the pipeline.
///
/// If no sub-command is specified retrieves the program.
Program {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
#[command(subcommand)]
action: Option<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 endpoint of a pipeline.
Endpoint {
/// The name of the pipeline.
#[arg(value_hint = ValueHint::Other, add = ArgValueCompleter::new(pipeline_names))]
name: String,
/// The name of the pipeline endpoint.
endpoint_name: String,
#[command(subcommand)]
action: EndpointAction,
},
/// 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 {
/// Set a new SQL program.
Set {
/// A path to a file containing the SQL code.
///
/// See the `stdin` flag for reading from stdin instead.
#[arg(value_hint = ValueHint::FilePath)]
program_path: String,
/// Read the program code from stdin.
///
/// EXAMPLES:
///
/// * cat program.sql | fda program p1 set -s -
/// * echo "SELECT 1" | fda program p1 set -s -
/// * fda program p2 | fda program p1 set -s -
#[arg(verbatim_doc_comment, short = 's', long, default_value_t = false)]
stdin: bool,
},
/// Retrieve the configuration of the program.
#[clap(aliases = &["cfg"])]
Config,
/// Set the configuration of the program.
#[clap(aliases = &["set-cfg"])]
SetConfig {
/// 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,
}
#[derive(Subcommand)]
pub enum EndpointAction {
Start,
Pause,
}