Skip to content

Commit a340328

Browse files
committed
Impl Session::subsystem
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 7a48da6 commit a340328

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/changelog.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::*;
66
/// ## Added
77
/// - Implement [`openssh_sftp_client::Writer`] for `ChildStdin`
88
/// if feature `openssh-sftp-client` is enabled.
9+
/// - [`Session::subsystem`]
910
#[doc(hidden)]
1011
pub mod unreleased {}
1112

src/session.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,61 @@ impl Session {
166166
)
167167
}
168168

169+
/// Constructs a new [`Command`] for launching subsystem `program` on the remote
170+
/// host.
171+
///
172+
/// Unlike [`command`](Session::command), this method does not shell-escape `program`, so it may be evaluated in
173+
/// unforeseen ways by the remote shell.
174+
///
175+
/// The returned `Command` is a builder, with the following default configuration:
176+
///
177+
/// * No arguments to the program
178+
/// * Empty stdin and dsicard stdout/stderr for `spawn` or `status`, but create output pipes for
179+
/// `output`
180+
///
181+
/// Builder methods are provided to change these defaults and otherwise configure the process.
182+
///
183+
/// ## Sftp subsystem
184+
///
185+
/// To use sftp subsystem, it is recommended to enable feature `openssh-sftp-client`,
186+
/// then use the following code to construct a sftp instance:
187+
///
188+
/// ```rust,no_run
189+
/// # use std::error::Error;
190+
/// # #[cfg(feature = "native-mux")]
191+
/// # #[tokio::main]
192+
/// # async fn main() -> Result<(), Box<dyn Error>> {
193+
///
194+
/// use openssh::{Session, KnownHosts, Stdio};
195+
/// use openssh_sftp_client::highlevel::Sftp;
196+
///
197+
/// let session = Session::connect_mux("me@ssh.example.com", KnownHosts::Strict).await?;
198+
///
199+
/// let mut child = session
200+
/// .subsystem("sftp")
201+
/// .stdin(Stdio::piped())
202+
/// .stdout(Stdio::piped())
203+
/// .spawn()
204+
/// .await?;
205+
///
206+
/// Sftp::new(
207+
/// child.stdin().take().unwrap(),
208+
/// child.stdout().take().unwrap(),
209+
/// Default::default(),
210+
/// )
211+
/// .await?
212+
/// .close()
213+
/// .await?;
214+
///
215+
/// # Ok(()) }
216+
/// ```
217+
pub fn subsystem<S: AsRef<OsStr>>(&self, program: S) -> Command<'_> {
218+
Command::new(
219+
self,
220+
delegate!(&self.0, imp, { imp.subsystem(program).into() }),
221+
)
222+
}
223+
169224
/// Constructs a new [`Command`] that runs the provided shell command on the remote host.
170225
///
171226
/// The provided command is passed as a single, escaped argument to `sh -c`, and from that

0 commit comments

Comments
 (0)