aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-10-22 06:46:29 -0400
committerJesse Luehrs <doy@tozt.net>2019-10-22 06:46:29 -0400
commit036f51ad6deddaf149685e6fd4945b1ed30543e3 (patch)
treebb2d24faf3eeb1b00ef295739d103768d9e8aa80
parentd6472faae56121d53f87c8ccffe40cf0a3d815dc (diff)
downloadteleterm-036f51ad6deddaf149685e6fd4945b1ed30543e3.tar.gz
teleterm-036f51ad6deddaf149685e6fd4945b1ed30543e3.zip
remove some duplication
-rw-r--r--src/cmd.rs5
-rw-r--r--src/cmd/play.rs10
-rw-r--r--src/cmd/record.rs12
-rw-r--r--src/cmd/server.rs27
-rw-r--r--src/cmd/stream.rs34
-rw-r--r--src/cmd/watch.rs34
-rw-r--r--src/config.rs4
7 files changed, 70 insertions, 56 deletions
diff --git a/src/cmd.rs b/src/cmd.rs
index 8861fb9..d101c81 100644
--- a/src/cmd.rs
+++ b/src/cmd.rs
@@ -96,7 +96,10 @@ pub fn run(matches: &clap::ArgMatches<'_>) -> Result<()> {
let mut cmd_config = (chosen_cmd.config)(config)?;
cmd_config.merge_args(chosen_submatches)?;
log::debug!("{:?}", cmd_config);
- cmd_config.run()
+ tokio::run(cmd_config.run().map_err(|e| {
+ log::error!("{}", e);
+ }));
+ Ok(())
}
fn program_name() -> Result<String> {
diff --git a/src/cmd/play.rs b/src/cmd/play.rs
index 510e7f3..2f2f4ba 100644
--- a/src/cmd/play.rs
+++ b/src/cmd/play.rs
@@ -15,12 +15,10 @@ impl crate::config::Config for Config {
self.ttyrec.merge_args(matches)
}
- fn run(&self) -> Result<()> {
- let fut = PlaySession::new(&self.ttyrec.filename);
- tokio::run(fut.map_err(|e| {
- log::error!("{}", e);
- }));
- Ok(())
+ fn run(
+ &self,
+ ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send> {
+ Box::new(PlaySession::new(&self.ttyrec.filename))
}
}
diff --git a/src/cmd/record.rs b/src/cmd/record.rs
index 746e6a9..44d01f3 100644
--- a/src/cmd/record.rs
+++ b/src/cmd/record.rs
@@ -20,17 +20,15 @@ impl crate::config::Config for Config {
Ok(())
}
- fn run(&self) -> Result<()> {
- let fut = RecordSession::new(
+ fn run(
+ &self,
+ ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send> {
+ Box::new(RecordSession::new(
&self.ttyrec.filename,
self.command.buffer_size,
&self.command.command,
&self.command.args,
- );
- tokio::run(fut.map_err(|e| {
- log::error!("{}", e);
- }));
- Ok(())
+ ))
}
}
diff --git a/src/cmd/server.rs b/src/cmd/server.rs
index 43c6618..bd1b4f4 100644
--- a/src/cmd/server.rs
+++ b/src/cmd/server.rs
@@ -25,10 +25,12 @@ impl crate::config::Config for Config {
self.server.merge_args(matches)
}
- fn run(&self) -> Result<()> {
+ fn run(
+ &self,
+ ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send> {
let (acceptor, server) =
if let Some(tls_identity_file) = &self.server.tls_identity_file {
- create_server_tls(
+ match create_server_tls(
self.server.listen_address,
self.server.buffer_size,
self.server.read_timeout,
@@ -37,9 +39,12 @@ impl crate::config::Config for Config {
self.oauth_configs.clone(),
self.server.uid,
self.server.gid,
- )?
+ ) {
+ Ok(futs) => futs,
+ Err(e) => return Box::new(futures::future::err(e)),
+ }
} else {
- create_server(
+ match create_server(
self.server.listen_address,
self.server.buffer_size,
self.server.read_timeout,
@@ -47,18 +52,18 @@ impl crate::config::Config for Config {
self.oauth_configs.clone(),
self.server.uid,
self.server.gid,
- )?
+ ) {
+ Ok(futs) => futs,
+ Err(e) => return Box::new(futures::future::err(e)),
+ }
};
- tokio::run(futures::future::lazy(move || {
+ Box::new(futures::future::lazy(move || {
tokio::spawn(server.map_err(|e| {
log::error!("{}", e);
}));
- acceptor.map_err(|e| {
- log::error!("{}", e);
- })
- }));
- Ok(())
+ acceptor
+ }))
}
}
diff --git a/src/cmd/stream.rs b/src/cmd/stream.rs
index ed9b980..0b60925 100644
--- a/src/cmd/stream.rs
+++ b/src/cmd/stream.rs
@@ -20,17 +20,20 @@ impl crate::config::Config for Config {
Ok(())
}
- fn run(&self) -> Result<()> {
- let host = self.client.host().to_string();
- let address = *self.client.addr();
+ fn run(
+ &self,
+ ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send> {
let auth = match self.client.auth {
crate::protocol::AuthType::Plain => {
let username = self
.client
.username
.clone()
- .context(crate::error::CouldntFindUsername)?;
- crate::protocol::Auth::plain(&username)
+ .context(crate::error::CouldntFindUsername);
+ match username {
+ Ok(username) => crate::protocol::Auth::plain(&username),
+ Err(e) => return Box::new(futures::future::err(e)),
+ }
}
crate::protocol::AuthType::RecurseCenter => {
let id = crate::oauth::load_client_auth_id(self.client.auth);
@@ -39,11 +42,16 @@ impl crate::config::Config for Config {
)
}
};
- let fut: Box<
- dyn futures::future::Future<Item = (), Error = Error> + Send,
- > = if self.client.tls {
- let connector = native_tls::TlsConnector::new()
- .context(crate::error::CreateConnector)?;
+
+ let host = self.client.host().to_string();
+ let address = *self.client.addr();
+ if self.client.tls {
+ let connector = match native_tls::TlsConnector::new()
+ .context(crate::error::CreateConnector)
+ {
+ Ok(connector) => connector,
+ Err(e) => return Box::new(futures::future::err(e)),
+ };
let connect: crate::client::Connector<_> = Box::new(move || {
let host = host.clone();
let connector = connector.clone();
@@ -80,11 +88,7 @@ impl crate::config::Config for Config {
self.command.buffer_size,
&auth,
))
- };
- tokio::run(fut.map_err(|e| {
- log::error!("{}", e);
- }));
- Ok(())
+ }
}
}
diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs
index f9fc7c3..f73457e 100644
--- a/src/cmd/watch.rs
+++ b/src/cmd/watch.rs
@@ -15,17 +15,20 @@ impl crate::config::Config for Config {
self.client.merge_args(matches)
}
- fn run(&self) -> Result<()> {
- let host = self.client.host().to_string();
- let address = *self.client.addr();
+ fn run(
+ &self,
+ ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send> {
let auth = match self.client.auth {
crate::protocol::AuthType::Plain => {
let username = self
.client
.username
.clone()
- .context(crate::error::CouldntFindUsername)?;
- crate::protocol::Auth::plain(&username)
+ .context(crate::error::CouldntFindUsername);
+ match username {
+ Ok(username) => crate::protocol::Auth::plain(&username),
+ Err(e) => return Box::new(futures::future::err(e)),
+ }
}
crate::protocol::AuthType::RecurseCenter => {
let id = crate::oauth::load_client_auth_id(self.client.auth);
@@ -34,11 +37,16 @@ impl crate::config::Config for Config {
)
}
};
- let fut: Box<
- dyn futures::future::Future<Item = (), Error = Error> + Send,
- > = if self.client.tls {
- let connector = native_tls::TlsConnector::new()
- .context(crate::error::CreateConnector)?;
+
+ let host = self.client.host().to_string();
+ let address = *self.client.addr();
+ if self.client.tls {
+ let connector = match native_tls::TlsConnector::new()
+ .context(crate::error::CreateConnector)
+ {
+ Ok(connector) => connector,
+ Err(e) => return Box::new(futures::future::err(e)),
+ };
let make_connector: Box<
dyn Fn() -> crate::client::Connector<_> + Send,
> = Box::new(move || {
@@ -74,11 +82,7 @@ impl crate::config::Config for Config {
})
});
Box::new(WatchSession::new(make_connector, &auth))
- };
- tokio::run(fut.map_err(|e| {
- log::error!("{}", e);
- }));
- Ok(())
+ }
}
}
diff --git a/src/config.rs b/src/config.rs
index c626afe..6a9c2c0 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -35,7 +35,9 @@ pub trait Config: std::fmt::Debug {
&mut self,
matches: &clap::ArgMatches<'a>,
) -> Result<()>;
- fn run(&self) -> Result<()>;
+ fn run(
+ &self,
+ ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send>;
}
pub fn config(