Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Use drivers to create help #11

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};

use clap::Parser;
use clap::{builder::StyledStr, Parser};
use lazy_static::lazy_static;
use tracing::*;

Expand Down Expand Up @@ -33,6 +33,8 @@ struct Args {
num_args = 1..,
value_delimiter = ' ',
value_parser = endpoints_parser,
help = "Space-separated list of endpoints.",
long_help = build_endpoints_help(),
)]
endpoints: Vec<Arc<dyn drivers::Driver>>,

Expand All @@ -52,6 +54,48 @@ struct Args {
streamreq_disable: bool,
}

#[instrument(level = "trace")]
fn build_endpoints_help() -> String {
let mut help = vec!["Space-separated list of endpoints.".to_string()];
let endpoints = drivers::endpoints()
.iter()
.map(|endpoint| {
let info = &endpoint.driver_ext;
let name = info.name();
let help_schemas = info
.valid_schemes()
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(",");

let help_legacy = info
.cli_example_legacy()
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n\t\t ");

let help_url = info
.cli_example_url()
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n\t\t ");

vec![
format!("{name}\t {help_schemas}").to_string(),
format!("\t legacy: {help_legacy}").to_string(),
format!("\t url: {help_url}\n").to_string(),
]
.join("\n")
})
.collect::<Vec<String>>();

help.extend(endpoints);
help.join("\n")
}

#[instrument(level = "debug")]
fn endpoints_parser(entry: &str) -> Result<Arc<dyn drivers::Driver>, String> {
drivers::create_driver_from_entry(entry)
Expand Down
16 changes: 16 additions & 0 deletions src/drivers/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ impl DriverInfo for FakeSinkInfo {
]
}

fn cli_example_legacy(&self) -> Vec<String> {
vec![]
}

fn cli_example_url(&self) -> Vec<String> {
vec![]
}

fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
None
}
Expand Down Expand Up @@ -184,6 +192,14 @@ impl DriverInfo for FakeSourceInfo {
]
}

fn cli_example_legacy(&self) -> Vec<String> {
vec![]
}

fn cli_example_url(&self) -> Vec<String> {
vec![]
}

fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
None
}
Expand Down
20 changes: 19 additions & 1 deletion src/drivers/file/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,27 @@ impl DriverInfo for FileClientInfo {

fn valid_schemes(&self) -> Vec<String> {
vec![
"filec".to_string(),
"fileclient".to_string(),
"filewriter".to_string(),
"filec".to_string(),
]
}

fn cli_example_legacy(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
vec![
format!("{first_schema}:<FILE>"),
format!("{first_schema}:/tmp/potato.tlog"),
]
}

fn cli_example_url(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
vec![
format!("{first_schema}://<FILE>").to_string(),
url::Url::parse(&format!("{first_schema}:///tmp/potato.tlog"))
.unwrap()
.to_string(),
]
}

Expand Down
18 changes: 18 additions & 0 deletions src/drivers/file/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,24 @@ impl DriverInfo for FileServerInfo {
]
}

fn cli_example_legacy(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
vec![
format!("{first_schema}:<FILE>"),
format!("{first_schema}:/tmp/potato.tlog"),
]
}

fn cli_example_url(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
vec![
format!("{first_schema}://<FILE>").to_string(),
url::Url::parse(&format!("{first_schema}:///tmp/potato.tlog"))
.unwrap()
.to_string(),
]
}

fn url_from_legacy(
&self,
legacy_entry: crate::drivers::DriverDescriptionLegacy,
Expand Down
12 changes: 12 additions & 0 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ where
pub trait DriverInfo: Sync + Send {
fn name(&self) -> &str;
fn valid_schemes(&self) -> Vec<String>;
// CLI helpers
fn cli_example_legacy(&self) -> Vec<String>;
fn cli_example_url(&self) -> Vec<String>;

fn create_endpoint_from_url(&self, url: &Url) -> Option<Arc<dyn Driver>>;

fn default_scheme(&self) -> String {
Expand Down Expand Up @@ -297,6 +301,14 @@ mod tests {
vec!["exampledriver".to_string()]
}

fn cli_example_legacy(&self) -> Vec<String> {
vec![]
}

fn cli_example_url(&self) -> Vec<String> {
vec![]
}

fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
None
}
Expand Down
22 changes: 22 additions & 0 deletions src/drivers/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ impl DriverInfo for SerialInfo {
vec!["serial".to_string()]
}

fn cli_example_legacy(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
vec![
format!("{first_schema}:<PORT>:<BAUDRATE>"),
format!("{first_schema}:/dev/ttyACM0:115200"),
format!("{first_schema}:COM:57600"),
]
}

fn cli_example_url(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
vec![
format!("{first_schema}://<PORT>?baudrate=<BAUDRATE?>").to_string(),
url::Url::parse(&format!("{first_schema}:///dev/ttyACM0?baudrate=115200"))
.unwrap()
.to_string(),
url::Url::parse(&format!("{first_schema}://COM1?baudrate=57600"))
.unwrap()
.to_string(),
]
}

fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
let port_name = url.path().to_string();
let baud_rate = url
Expand Down
24 changes: 24 additions & 0 deletions src/drivers/tcp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ impl DriverInfo for TcpClientInfo {
vec!["tcpc".to_string(), "tcpclient".to_string()]
}

fn cli_example_legacy(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}:<IP>:<PORT>"),
format!("{first_schema}:0.0.0.0:14550"),
format!("{second_schema}:127.0.0.1:14660"),
]
}

fn cli_example_url(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}://<IP>:<PORT>").to_string(),
url::Url::parse(&format!("{first_schema}://0.0.0.0:14550"))
.unwrap()
.to_string(),
url::Url::parse(&format!("{second_schema}://127.0.0.1:14660"))
.unwrap()
.to_string(),
]
}

fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
let host = url.host_str().unwrap();
let port = url.port().unwrap();
Expand Down
25 changes: 25 additions & 0 deletions src/drivers/tcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ impl DriverInfo for TcpServerInfo {
vec!["tcps".to_string(), "tcpserver".to_string()]
}

fn cli_example_legacy(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}:<IP?>:<PORT>"),
format!("{first_schema}:14550"),
format!("{first_schema}:0.0.0.0:14550"),
format!("{second_schema}:127.0.0.1:14660"),
]
}

fn cli_example_url(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}://<IP>:<PORT>").to_string(),
url::Url::parse(&format!("{first_schema}://0.0.0.0:14550"))
.unwrap()
.to_string(),
url::Url::parse(&format!("{second_schema}://127.0.0.1:14660"))
.unwrap()
.to_string(),
]
}

fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
let host = url.host_str().unwrap();
let port = url.port().unwrap();
Expand Down
28 changes: 26 additions & 2 deletions src/drivers/udp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,38 @@ impl Driver for UdpClient {
pub struct UdpClientInfo;
impl DriverInfo for UdpClientInfo {
fn name(&self) -> &str {
"UdpServer"
"UdpClient"
}

fn valid_schemes(&self) -> Vec<String> {
vec![
"udpc".to_string(),
"udpclient".to_string(),
"udpout".to_string(),
"udpc".to_string(),
]
}

fn cli_example_legacy(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}:<IP>:<PORT>"),
format!("{first_schema}:0.0.0.0:14550"),
format!("{second_schema}:192.168.2.1:14660"),
]
}

fn cli_example_url(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}://<IP>:<PORT>").to_string(),
url::Url::parse(&format!("{first_schema}://0.0.0.0:14550"))
.unwrap()
.to_string(),
url::Url::parse(&format!("{second_schema}://192.168.2.1:14660"))
.unwrap()
.to_string(),
]
}

Expand Down
26 changes: 25 additions & 1 deletion src/drivers/udp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,33 @@ impl DriverInfo for UdpServerInfo {

fn valid_schemes(&self) -> Vec<String> {
vec![
"udps".to_string(),
"udpserver".to_string(),
"udpin".to_string(),
"udps".to_string(),
]
}

fn cli_example_legacy(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}:<IP>:<PORT>"),
format!("{first_schema}:0.0.0.0:14550"),
format!("{second_schema}:127.0.0.1:14660"),
]
}

fn cli_example_url(&self) -> Vec<String> {
let first_schema = &self.valid_schemes()[0];
let second_schema = &self.valid_schemes()[1];
vec![
format!("{first_schema}://<IP>:<PORT>").to_string(),
url::Url::parse(&format!("{first_schema}://0.0.0.0:14550"))
.unwrap()
.to_string(),
url::Url::parse(&format!("{second_schema}://127.0.0.1:14660"))
.unwrap()
.to_string(),
]
}

Expand Down
Loading