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
use std::sync::Arc;
use structopt::clap::{AppSettings, Shell};
use structopt::StructOpt;
use tracing::debug;
use fluvio_future::task::run_block_on;
use crate::COMMAND_TEMPLATE;
use crate::CliError;
use super::consume::process_consume_log;
use super::produce::process_produce_record;
use super::topic::process_topic;
use super::spu::*;
use super::custom::*;
use super::group::*;
use super::profile::process_profile;
use super::cluster::process_cluster;
use super::consume::ConsumeLogOpt;
use super::produce::ProduceLogOpt;
use super::topic::TopicOpt;
use super::profile::ProfileCommand;
use super::cluster::ClusterCommands;
use super::partition::PartitionOpt;
#[cfg(feature = "cluster_components")]
use super::run::{process_run, RunOpt};
#[derive(Debug, StructOpt)]
#[structopt(
about = "Fluvio Command Line Interface",
name = "fluvio",
template = COMMAND_TEMPLATE,
global_settings = &[AppSettings::VersionlessSubcommands, AppSettings::DeriveDisplayOrder, AppSettings::DisableVersion]
)]
enum Root {
#[structopt(
no_version,
name = "consume",
template = COMMAND_TEMPLATE,
about = "Reads messages from a topic/partition"
)]
Consume(ConsumeLogOpt),
#[structopt(
name = "produce",
template = COMMAND_TEMPLATE,
about = "Writes messages to a topic/partition"
)]
Produce(ProduceLogOpt),
#[structopt(
name = "spu",
template = COMMAND_TEMPLATE,
about = "SPU operations"
)]
SPU(SpuOpt),
#[structopt(
name = "spu-group",
template = COMMAND_TEMPLATE,
about = "SPU group operations"
)]
SPUGroup(SpuGroupOpt),
#[structopt(
name = "custom-spu",
template = COMMAND_TEMPLATE,
about = "Custom SPU operations"
)]
CustomSPU(CustomSpuOpt),
#[structopt(
name = "topic",
template = COMMAND_TEMPLATE,
about = "Topic operations"
)]
Topic(TopicOpt),
#[structopt(
name = "partition",
template = COMMAND_TEMPLATE,
about = "Partition operations"
)]
Partition(PartitionOpt),
#[structopt(
name = "profile",
template = COMMAND_TEMPLATE,
about = "Profile operations"
)]
Profile(ProfileCommand),
#[structopt(
name = "cluster",
template = COMMAND_TEMPLATE,
about = "Cluster operations"
)]
Cluster(ClusterCommands),
#[cfg(feature = "cluster_components")]
#[structopt(about = "Run cluster component")]
Run(RunOpt),
#[structopt(
name = "version",
about = "Prints the current fluvio version information"
)]
Version(VersionCmd),
#[structopt(
name = "completions",
about = "Generate command-line completions for Fluvio",
settings = &[AppSettings::Hidden]
)]
Completions(CompletionShell),
#[structopt(external_subcommand)]
External(Vec<String>),
}
pub fn run_cli(args: &[String]) -> eyre::Result<String> {
run_block_on(async move {
let terminal = Arc::new(PrintTerminal::new());
let root_args: Root = Root::from_iter(args);
let output = match root_args {
Root::Consume(consume) => process_consume_log(terminal.clone(), consume).await?,
Root::Produce(produce) => process_produce_record(terminal.clone(), produce).await?,
Root::SPU(spu) => process_spu(terminal.clone(), spu).await?,
Root::SPUGroup(spu_group) => process_spu_group(terminal.clone(), spu_group).await?,
Root::CustomSPU(custom_spu) => process_custom_spu(terminal.clone(), custom_spu).await?,
Root::Topic(topic) => process_topic(terminal.clone(), topic).await?,
Root::Partition(partition) => partition.process_partition(terminal.clone()).await?,
Root::Profile(profile) => process_profile(terminal.clone(), profile).await?,
Root::Cluster(cluster) => process_cluster(terminal.clone(), cluster).await?,
#[cfg(feature = "cluster_components")]
Root::Run(opt) => process_run(opt)?,
Root::Version(_) => process_version_cmd()?,
Root::Completions(shell) => process_completions_cmd(shell)?,
Root::External(args) => process_external_subcommand(args)?,
};
Ok(output)
})
}
use crate::Terminal;
struct PrintTerminal {}
impl PrintTerminal {
fn new() -> Self {
Self {}
}
}
impl Terminal for PrintTerminal {
fn print(&self, msg: &str) {
print!("{}", msg);
}
fn println(&self, msg: &str) {
println!("{}", msg);
}
}
#[derive(Debug, StructOpt)]
struct VersionCmd {}
fn process_version_cmd() -> Result<String, CliError> {
println!("Fluvio version : {}", crate::VERSION);
println!("Git Commit : {}", env!("GIT_HASH"));
if let Some(os_info) = option_env!("UNAME") {
println!("OS Details : {}", os_info);
}
println!("Rustc Version : {}", env!("RUSTC_VERSION"));
Ok("".to_owned())
}
#[derive(Debug, StructOpt)]
struct CompletionOpt {
#[structopt(long, default_value = "fluvio")]
name: String,
}
#[derive(Debug, StructOpt)]
enum CompletionShell {
#[structopt(name = "bash")]
Bash(CompletionOpt),
#[structopt(name = "fish")]
Fish(CompletionOpt),
}
fn process_completions_cmd(shell: CompletionShell) -> Result<String, CliError> {
let mut app: structopt::clap::App = Root::clap();
match shell {
CompletionShell::Bash(opt) => {
app.gen_completions_to(opt.name, Shell::Bash, &mut std::io::stdout());
}
CompletionShell::Fish(opt) => {
app.gen_completions_to(opt.name, Shell::Fish, &mut std::io::stdout());
}
}
Ok("".to_string())
}
fn process_external_subcommand(mut args: Vec<String>) -> Result<String, CliError> {
use std::process::Command;
use which::{CanonicalPath, Error as WhichError};
let cmd = args.remove(0);
let external_subcommand = format!("fluvio-{}", cmd);
let subcommand_path = match CanonicalPath::new(&external_subcommand) {
Ok(path) => path,
Err(WhichError::CannotFindBinaryPath) => {
println!(
"Unable to find plugin '{}'. Make sure it is executable and in your PATH.",
&external_subcommand
);
std::process::exit(1);
}
other => other?,
};
let args_string = args.join(" ");
debug!(
"Launching external subcommand: {} {}",
subcommand_path.as_path().display(),
&args_string
);
let status = Command::new(subcommand_path.as_path())
.args(&args)
.status()?;
if let Some(code) = status.code() {
std::process::exit(code);
}
Ok("".to_string())
}