Skip to content

Commit 6057429

Browse files
committed
Apply suggestions of code review.
Signed-off-by: Aalekh Patel <aalekh.gwpeck.7998@icloud.com>
1 parent b778ea9 commit 6057429

File tree

2 files changed

+106
-18
lines changed

2 files changed

+106
-18
lines changed

src/command.rs

Lines changed: 105 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,127 @@ macro_rules! delegate {
5353
/// Primarily a way to allow `std::process::Command` to be turned directly into an `openssh::Command`.
5454
pub trait OverSsh {
5555
/// Given a session, return a command that can be executed over that session.
56+
///
57+
/// **Note**: The command to be executed on the remote machine does not include
58+
/// any environment variables or the current directory. They must be
59+
/// set explicitly, if desired.
5660
fn over_session<'session>(&self, session: &'session Session) -> crate::Command<'session>;
5761
}
5862

5963
impl OverSsh for std::process::Command {
6064
/// Given a session, convert a `std::process::Command` into an `openssh::Command`
6165
/// that can be executed over that session.
66+
/// **Note**: The command to be executed on the remote machine does not include
67+
/// any environment variables or the current directory. They must be
68+
/// set explicitly, if desired.
6269
/// ```no_run
63-
/// use std::process::Command;
64-
/// use openssh::{Session, KnownHosts, OverSsh};
65-
66-
/// let session = Session::connect_mux("me@ssh.example.com", KnownHosts::Strict).await?;
67-
/// let ls =
68-
/// Command::new("ls")
69-
/// .arg("-l")
70-
/// .arg("-a")
71-
/// .arg("-h")
72-
/// .over_session(&session)
73-
/// .output()
74-
/// .await?;
75-
///
76-
/// assert!(String::from_utf8(ls.stdout).unwrap().contains("total"));
70+
/// # #[tokio::main(flavor = "current_thread")]
71+
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
72+
/// use std::process::Command;
73+
/// use openssh::{Session, KnownHosts, OverSsh};
74+
75+
/// let session = Session::connect_mux("me@ssh.example.com", KnownHosts::Strict).await?;
76+
/// let ls =
77+
/// Command::new("ls")
78+
/// .arg("-l")
79+
/// .arg("-a")
80+
/// .arg("-h")
81+
/// .over_session(&session)
82+
/// .output()
83+
/// .await?;
7784
///
85+
/// assert!(String::from_utf8(ls.stdout).unwrap().contains("total"));
86+
/// # Ok(())
87+
/// }
7888
///
7989
/// ```
8090
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
81-
82-
let mut command = session.raw_command(self.get_program());
91+
let mut command = session.command(self.get_program().to_string_lossy());
8392
command.raw_args(self.get_args());
8493
command
8594
}
8695
}
8796

97+
98+
impl OverSsh for tokio::process::Command {
99+
/// Given a session, convert a `tokio::process::Command` into an `openssh::Command`
100+
/// that can be executed over that session.
101+
///
102+
/// **Note**: The command to be executed on the remote machine does not include
103+
/// any environment variables or the current directory. They must be
104+
/// set explicitly, if desired.
105+
/// ```no_run
106+
/// # #[tokio::main(flavor = "current_thread")]
107+
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
108+
/// use tokio::process::Command;
109+
/// use openssh::{Session, KnownHosts, OverSsh};
110+
111+
/// let session = Session::connect_mux("me@ssh.example.com", KnownHosts::Strict).await?;
112+
/// let ls =
113+
/// Command::new("ls")
114+
/// .arg("-l")
115+
/// .arg("-a")
116+
/// .arg("-h")
117+
/// .over_session(&session)
118+
/// .output()
119+
/// .await?;
120+
///
121+
/// assert!(String::from_utf8(ls.stdout).unwrap().contains("total"));
122+
/// # Ok(())
123+
/// # }
124+
///
125+
/// ```
126+
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
127+
self.as_std().over_session(session)
128+
}
129+
}
130+
131+
132+
impl<S> OverSsh for &S
133+
where
134+
S: OverSsh
135+
{
136+
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
137+
<S as OverSsh>::over_session(self, session)
138+
}
139+
}
140+
141+
impl<S> OverSsh for &mut S
142+
where
143+
S: OverSsh
144+
{
145+
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
146+
<S as OverSsh>::over_session(self, session)
147+
}
148+
}
149+
150+
impl<S> OverSsh for Box<S>
151+
where
152+
S: OverSsh
153+
{
154+
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
155+
<S as OverSsh>::over_session(self, session)
156+
}
157+
}
158+
159+
impl<S> OverSsh for std::rc::Rc<S>
160+
where
161+
S: OverSsh
162+
{
163+
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
164+
<S as OverSsh>::over_session(self, session)
165+
}
166+
}
167+
168+
impl<S> OverSsh for std::sync::Arc<S>
169+
where
170+
S: OverSsh
171+
{
172+
fn over_session<'session>(&self, session: &'session Session) -> Command<'session> {
173+
<S as OverSsh>::over_session(self, session)
174+
}
175+
}
176+
88177
/// A remote process builder, providing fine-grained control over how a new remote process should
89178
/// be spawned.
90179
///

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ mod builder;
167167
pub use builder::{KnownHosts, SessionBuilder};
168168

169169
mod command;
170-
pub use command::Command;
171-
pub use command::OverSsh;
170+
pub use command::{Command, OverSsh};
172171

173172
mod child;
174173
pub use child::RemoteChild;

0 commit comments

Comments
 (0)