Compare commits

...

1 Commits

Author SHA1 Message Date
John Tur
1ef4551c9c Simplify shell commands for WSL and SSH
Co-authored-by: Lukas Wirth <me@lukaswirth.dev>
2025-12-10 12:53:58 -05:00
3 changed files with 34 additions and 35 deletions

View File

@@ -31,8 +31,7 @@ use tempfile::TempDir;
use util::{
paths::{PathStyle, RemotePathBuf},
rel_path::RelPath,
shell::{Shell, ShellKind},
shell_builder::ShellBuilder,
shell::ShellKind,
};
pub(crate) struct SshRemoteConnection {
@@ -1363,8 +1362,6 @@ fn build_command(
} else {
write!(exec, "{ssh_shell} -l")?;
};
let (command, command_args) = ShellBuilder::new(&Shell::Program(ssh_shell.to_owned()), false)
.build(Some(exec.clone()), &[]);
let mut args = Vec::new();
args.extend(ssh_args);
@@ -1375,8 +1372,7 @@ fn build_command(
}
args.push("-t".into());
args.push(command);
args.extend(command_args);
args.push(exec);
Ok(CommandTemplate {
program: "ssh".into(),
@@ -1416,9 +1412,6 @@ mod tests {
"-p",
"2222",
"-t",
"/bin/fish",
"-i",
"-c",
"cd \"$HOME/work\" && exec env INPUT_VA=val remote_program arg1 arg2"
]
);
@@ -1451,9 +1444,6 @@ mod tests {
"-L",
"1:foo:2",
"-t",
"/bin/fish",
"-i",
"-c",
"cd && exec env INPUT_VA=val /bin/fish -l"
]
);

View File

@@ -14,7 +14,6 @@ use semver::Version;
use smol::{fs, process};
use std::{
ffi::OsStr,
fmt::Write as _,
path::{Path, PathBuf},
process::Stdio,
sync::Arc,
@@ -423,39 +422,31 @@ impl RemoteConnection for WslRemoteConnection {
bail!("WSL shares the network interface with the host system");
}
let shell_kind = self.shell_kind;
let working_dir = working_dir
.map(|working_dir| RemotePathBuf::new(working_dir, PathStyle::Posix).to_string())
.unwrap_or("~".to_string());
let mut exec = String::from("exec env ");
let mut shell_builder_args = Vec::new();
shell_builder_args.push("env".to_owned());
for (k, v) in env.iter() {
write!(
exec,
"{}={} ",
k,
shell_kind.try_quote(v).context("shell quoting")?
)?;
shell_builder_args.push(format!("{}={}", k, v));
}
if let Some(program) = program {
write!(
exec,
"{}",
shell_kind
.try_quote_prefix_aware(&program)
.context("shell quoting")?
)?;
for arg in args {
let arg = shell_kind.try_quote(&arg).context("shell quoting")?;
write!(exec, " {}", &arg)?;
}
shell_builder_args.push(program);
shell_builder_args.extend(args.into_iter().cloned())
} else {
write!(&mut exec, "{} -l", self.shell)?;
let (command, args) =
ShellBuilder::new(&Shell::Program(self.shell.clone()), false).build_login_shell();
shell_builder_args.push(command);
shell_builder_args.extend(args);
}
let (command, args) =
ShellBuilder::new(&Shell::Program(self.shell.clone()), false).build(Some(exec), &[]);
let (command, args) = ShellBuilder::new(
&Shell::Program(self.shell.clone()),
/* not important as we do not supploy `Shell::System` */ false,
)
.build(Some("exec".to_owned()), &[]);
let mut wsl_args = if let Some(user) = &self.connection_options.user {
vec![

View File

@@ -73,6 +73,24 @@ impl ShellBuilder {
self
}
pub fn build_login_shell(self) -> (String, Vec<String>) {
(
self.program,
match self.kind {
ShellKind::Fish
| ShellKind::Csh
| ShellKind::Tcsh
| ShellKind::Rc
| ShellKind::Xonsh
| ShellKind::Elvish
| ShellKind::Nushell
| ShellKind::Posix
| ShellKind::Pwsh => vec!["-l".to_owned()],
ShellKind::PowerShell | ShellKind::Cmd => vec![],
},
)
}
/// Returns the program and arguments to run this task in a shell.
pub fn build(
mut self,