|
| 1 | +use crate::escape::escape; |
| 2 | + |
1 | 3 | use super::stdio::TryFromChildIo;
|
2 | 4 | use super::RemoteChild;
|
3 | 5 | use super::Stdio;
|
@@ -49,6 +51,130 @@ macro_rules! delegate {
|
49 | 51 | }};
|
50 | 52 | }
|
51 | 53 |
|
| 54 | +/// If a command is `OverSsh` then it can be executed over an SSH session. |
| 55 | +/// |
| 56 | +/// Primarily a way to allow `std::process::Command` to be turned directly into an `openssh::Command`. |
| 57 | +pub trait OverSsh { |
| 58 | + /// Given an ssh session, return a command that can be executed over that ssh session. |
| 59 | + /// |
| 60 | + /// ### Notes |
| 61 | + /// |
| 62 | + /// The command to be executed on the remote machine should not explicitly |
| 63 | + /// set environment variables or the current working directory. It errors if the source command |
| 64 | + /// has environment variables or a current working directory set, since `openssh` doesn't (yet) have |
| 65 | + /// a method to set environment variables and `ssh` doesn't support setting a current working directory |
| 66 | + /// outside of `bash/dash/zsh` (which is not always available). |
| 67 | + /// |
| 68 | + /// ### Examples |
| 69 | + /// |
| 70 | + /// 1. Consider the implementation of `OverSsh` for `std::process::Command`. Let's build a |
| 71 | + /// `ls -l -a -h` command and execute it over an SSH session. |
| 72 | + /// |
| 73 | + /// ```no_run |
| 74 | + /// # #[tokio::main(flavor = "current_thread")] |
| 75 | + /// async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 76 | + /// use std::process::Command; |
| 77 | + /// use openssh::{Session, KnownHosts, OverSsh}; |
| 78 | + /// |
| 79 | + /// let session = Session::connect_mux("[email protected]", KnownHosts::Strict).await?; |
| 80 | + /// let ls = |
| 81 | + /// Command::new("ls") |
| 82 | + /// .arg("-l") |
| 83 | + /// .arg("-a") |
| 84 | + /// .arg("-h") |
| 85 | + /// .over_ssh(&session)? |
| 86 | + /// .output() |
| 87 | + /// .await?; |
| 88 | + /// |
| 89 | + /// assert!(String::from_utf8(ls.stdout).unwrap().contains("total")); |
| 90 | + /// # Ok(()) |
| 91 | + /// } |
| 92 | + /// |
| 93 | + /// ``` |
| 94 | + /// 2. Building a command with environment variables or a current working directory set will |
| 95 | + /// results in an error. |
| 96 | + /// |
| 97 | + /// ```no_run |
| 98 | + /// # #[tokio::main(flavor = "current_thread")] |
| 99 | + /// async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 100 | + /// use std::process::Command; |
| 101 | + /// use openssh::{Session, KnownHosts, OverSsh}; |
| 102 | + /// |
| 103 | + /// let session = Session::connect_mux("[email protected]", KnownHosts::Strict).await?; |
| 104 | + /// let echo = |
| 105 | + /// Command::new("echo") |
| 106 | + /// .env("MY_ENV_VAR", "foo") |
| 107 | + /// .arg("$MY_ENV_VAR") |
| 108 | + /// .over_ssh(&session); |
| 109 | + /// assert!(matches!(echo, Err(openssh::Error::CommandHasEnv))); |
| 110 | + /// |
| 111 | + /// # Ok(()) |
| 112 | + /// } |
| 113 | + /// |
| 114 | + /// ``` |
| 115 | + fn over_ssh<'session>( |
| 116 | + &self, |
| 117 | + session: &'session Session, |
| 118 | + ) -> Result<crate::Command<'session>, crate::Error>; |
| 119 | +} |
| 120 | + |
| 121 | +impl OverSsh for std::process::Command { |
| 122 | + fn over_ssh<'session>( |
| 123 | + &self, |
| 124 | + session: &'session Session, |
| 125 | + ) -> Result<Command<'session>, crate::Error> { |
| 126 | + // I'd really like `!self.get_envs().is_empty()` here, but that's |
| 127 | + // behind a `exact_size_is_empty` feature flag. |
| 128 | + if self.get_envs().len() > 0 { |
| 129 | + return Err(crate::Error::CommandHasEnv); |
| 130 | + } |
| 131 | + |
| 132 | + if self.get_current_dir().is_some() { |
| 133 | + return Err(crate::Error::CommandHasCwd); |
| 134 | + } |
| 135 | + |
| 136 | + let program_escaped: Cow<'_, OsStr> = escape(self.get_program()); |
| 137 | + let mut command = session.raw_command(program_escaped); |
| 138 | + |
| 139 | + let args = self.get_args().map(escape); |
| 140 | + command.raw_args(args); |
| 141 | + Ok(command) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl OverSsh for tokio::process::Command { |
| 146 | + fn over_ssh<'session>( |
| 147 | + &self, |
| 148 | + session: &'session Session, |
| 149 | + ) -> Result<Command<'session>, crate::Error> { |
| 150 | + self.as_std().over_ssh(session) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl<S> OverSsh for &S |
| 155 | +where |
| 156 | + S: OverSsh, |
| 157 | +{ |
| 158 | + fn over_ssh<'session>( |
| 159 | + &self, |
| 160 | + session: &'session Session, |
| 161 | + ) -> Result<Command<'session>, crate::Error> { |
| 162 | + <S as OverSsh>::over_ssh(self, session) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl<S> OverSsh for &mut S |
| 167 | +where |
| 168 | + S: OverSsh, |
| 169 | +{ |
| 170 | + fn over_ssh<'session>( |
| 171 | + &self, |
| 172 | + session: &'session Session, |
| 173 | + ) -> Result<Command<'session>, crate::Error> { |
| 174 | + <S as OverSsh>::over_ssh(self, session) |
| 175 | + } |
| 176 | +} |
| 177 | + |
52 | 178 | /// A remote process builder, providing fine-grained control over how a new remote process should
|
53 | 179 | /// be spawned.
|
54 | 180 | ///
|
|
0 commit comments