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
use serde::{Deserialize, Serialize};
pub mod output;
mod common;
#[cfg(feature = "target")]
pub mod tls;
pub use common::*;
pub use crate::output::Terminal;
pub const COMMAND_TEMPLATE: &str = "{about}
{usage}
{all-args}
";
#[macro_export]
macro_rules! t_print {
($out:expr,$($arg:tt)*) => ( $out.print(&format!($($arg)*)))
}
#[macro_export]
macro_rules! t_println {
($out:expr,$($arg:tt)*) => ( $out.println(&format!($($arg)*)))
}
#[macro_export]
macro_rules! t_print_cli_err {
($out:expr,$x:expr) => {
t_println!($out, "\x1B[1;31merror:\x1B[0m {}", $x);
};
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FluvioExtensionMetadata {
pub command: String,
pub description: String,
pub version: String,
}
pub struct PrintTerminal {}
impl PrintTerminal {
pub fn new() -> Self {
Self {}
}
}
impl Default for PrintTerminal {
fn default() -> Self {
Self::new()
}
}
impl Terminal for PrintTerminal {
fn print(&self, msg: &str) {
print!("{}", msg);
}
fn println(&self, msg: &str) {
println!("{}", msg);
}
}
#[cfg(feature = "target")]
pub mod target {
use std::io::{ErrorKind, Error as IoError};
use std::convert::TryInto;
use structopt::StructOpt;
use fluvio::FluvioConfig;
use fluvio::FluvioError;
use fluvio::Fluvio;
use fluvio::config::ConfigFile;
use crate::tls::TlsClientOpt;
#[derive(thiserror::Error, Debug)]
pub enum TargetError {
#[error(transparent)]
IoError(#[from] IoError),
#[error("Fluvio client error")]
ClientError(#[from] FluvioError),
#[error("Invalid argument: {0}")]
InvalidArg(String),
#[error("Unknown error: {0}")]
Other(String),
}
impl TargetError {
pub fn invalid_arg<M: Into<String>>(reason: M) -> Self {
Self::InvalidArg(reason.into())
}
}
#[derive(Debug, StructOpt, Default)]
pub struct ClusterTarget {
#[structopt(short = "c", long, value_name = "host:port")]
pub cluster: Option<String>,
#[structopt(flatten)]
pub tls: TlsClientOpt,
#[structopt(short = "P", long, value_name = "profile")]
pub profile: Option<String>,
}
impl ClusterTarget {
pub async fn connect(self) -> Result<Fluvio, TargetError> {
let fluvio_config = self.load()?;
Fluvio::connect_with_config(&fluvio_config)
.await
.map_err(|err| err.into())
}
pub fn load(self) -> Result<FluvioConfig, TargetError> {
let tls = self.tls.try_into()?;
use fluvio::config::TlsPolicy::*;
match (self.profile, self.cluster) {
(Some(_profile), Some(_cluster)) => Err(TargetError::invalid_arg(
"cluster addr is not valid when profile is used",
)),
(Some(profile), _) => {
if let Anonymous | Verified(_) = tls {
return Err(TargetError::invalid_arg(
"tls is not valid when profile is is used",
));
}
let config_file = ConfigFile::load(None)?;
let cluster = config_file
.config()
.cluster_with_profile(&profile)
.ok_or_else(|| {
IoError::new(ErrorKind::Other, "Cluster not found for profile")
})?;
Ok(cluster.clone())
}
(None, Some(cluster)) => {
let cluster = FluvioConfig::new(cluster).with_tls(tls);
Ok(cluster)
}
(None, None) => {
if let Anonymous | Verified(_) = tls {
return Err(TargetError::invalid_arg(
"tls is only valid if cluster addr is used",
));
}
let config_file = ConfigFile::load(None)?;
let cluster = config_file.config().current_cluster()?;
Ok(cluster.clone())
}
}
}
}
}