Allow ssh connection for setting up zed (#12063)

Co-Authored-By: Mikayla <mikayla@zed.dev>



Release Notes:

- Magic `ssh` login feature for remote development

---------

Co-authored-by: Mikayla <mikayla@zed.dev>
Co-authored-by: Nate Butler <iamnbutler@gmail.com>
This commit is contained in:
Conrad Irwin 2024-05-21 22:39:16 -06:00 committed by GitHub
parent 3382e79ef9
commit e5b9e2044e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 1242 additions and 785 deletions

View file

@ -19,6 +19,38 @@ pub use vscode_format::VsCodeTaskFile;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TaskId(pub String);
/// TerminalWorkDir describes where a task should be run
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TerminalWorkDir {
/// Local is on this machine
Local(PathBuf),
/// SSH runs the terminal over ssh
Ssh {
/// The command to run to connect
ssh_command: String,
/// The path on the remote server
path: Option<String>,
},
}
impl TerminalWorkDir {
/// is_local
pub fn is_local(&self) -> bool {
match self {
TerminalWorkDir::Local(_) => true,
TerminalWorkDir::Ssh { .. } => false,
}
}
/// local_path
pub fn local_path(&self) -> Option<PathBuf> {
match self {
TerminalWorkDir::Local(path) => Some(path.clone()),
TerminalWorkDir::Ssh { .. } => None,
}
}
}
/// Contains all information needed by Zed to spawn a new terminal tab for the given task.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpawnInTerminal {
@ -36,7 +68,7 @@ pub struct SpawnInTerminal {
/// A human-readable label, containing command and all of its arguments, joined and substituted.
pub command_label: String,
/// Current working directory to spawn the command into.
pub cwd: Option<PathBuf>,
pub cwd: Option<TerminalWorkDir>,
/// Env overrides for the command, will be appended to the terminal's environment from the settings.
pub env: HashMap<String, String>,
/// Whether to use a new terminal tab or reuse the existing one to spawn the process.

View file

@ -8,7 +8,8 @@ use sha2::{Digest, Sha256};
use util::{truncate_and_remove_front, ResultExt};
use crate::{
ResolvedTask, SpawnInTerminal, TaskContext, TaskId, VariableName, ZED_VARIABLE_NAME_PREFIX,
ResolvedTask, SpawnInTerminal, TaskContext, TaskId, TerminalWorkDir, VariableName,
ZED_VARIABLE_NAME_PREFIX,
};
/// A template definition of a Zed task to run.
@ -112,12 +113,14 @@ impl TaskTemplate {
&variable_names,
&mut substituted_variables,
)?;
Some(substitured_cwd)
Some(TerminalWorkDir::Local(PathBuf::from(substitured_cwd)))
}
None => None,
}
.map(PathBuf::from)
.or(cx.cwd.clone());
.or(cx
.cwd
.as_ref()
.map(|cwd| TerminalWorkDir::Local(cwd.clone())));
let human_readable_label = substitute_all_template_variables_in_str(
&self.label,
&truncated_variables,
@ -379,8 +382,10 @@ mod tests {
task_variables: TaskVariables::default(),
};
assert_eq!(
resolved_task(&task_without_cwd, &cx).cwd.as_deref(),
Some(context_cwd.as_path()),
resolved_task(&task_without_cwd, &cx)
.cwd
.and_then(|cwd| cwd.local_path()),
Some(context_cwd.clone()),
"TaskContext's cwd should be taken on resolve if task's cwd is None"
);
@ -394,8 +399,10 @@ mod tests {
task_variables: TaskVariables::default(),
};
assert_eq!(
resolved_task(&task_with_cwd, &cx).cwd.as_deref(),
Some(task_cwd.as_path()),
resolved_task(&task_with_cwd, &cx)
.cwd
.and_then(|cwd| cwd.local_path()),
Some(task_cwd.clone()),
"TaskTemplate's cwd should be taken on resolve if TaskContext's cwd is None"
);
@ -404,8 +411,10 @@ mod tests {
task_variables: TaskVariables::default(),
};
assert_eq!(
resolved_task(&task_with_cwd, &cx).cwd.as_deref(),
Some(task_cwd.as_path()),
resolved_task(&task_with_cwd, &cx)
.cwd
.and_then(|cwd| cwd.local_path()),
Some(task_cwd.clone()),
"TaskTemplate's cwd should be taken on resolve if TaskContext's cwd is not None"
);
}