D2: engine commands for the remote browser, compare and sync; save before upload
deploy.browse, readRemote, writeRemote, renameRemote, mkdirRemote, deleteRemote (confirm: true required), compare, sync (confirmDelete required for deletions). deploy.upload saves dirty buffers under its paths first (saveFirst, default true) without queueing uploads on save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a8ad74755c
commit
7601ce12f6
5 changed files with 673 additions and 11 deletions
|
|
@ -29,6 +29,7 @@ use std::sync::mpsc;
|
||||||
use std::sync::{Arc, Mutex, PoisonError};
|
use std::sync::{Arc, Mutex, PoisonError};
|
||||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
use lutra_deploy::browse::{self, Target};
|
||||||
use lutra_deploy::known_hosts::{self, HostKey};
|
use lutra_deploy::known_hosts::{self, HostKey};
|
||||||
use lutra_deploy::secrets::account;
|
use lutra_deploy::secrets::account;
|
||||||
use lutra_deploy::{
|
use lutra_deploy::{
|
||||||
|
|
@ -75,6 +76,7 @@ pub fn register(registry: &mut Registry) -> Result<(), EngineError> {
|
||||||
registry.register(Box::new(CancelJob))?;
|
registry.register(Box::new(CancelJob))?;
|
||||||
registry.register(Box::new(Log))?;
|
registry.register(Box::new(Log))?;
|
||||||
registry.register(Box::new(ClearLog))?;
|
registry.register(Box::new(ClearLog))?;
|
||||||
|
super::deploy_remote::register(registry)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -171,6 +173,9 @@ pub struct DeployState {
|
||||||
next_id: u64,
|
next_id: u64,
|
||||||
/// Uploads on save waiting for the slot, in order, without duplicates.
|
/// Uploads on save waiting for the slot, in order, without duplicates.
|
||||||
queue: VecDeque<Queued>,
|
queue: VecDeque<Queued>,
|
||||||
|
/// Set while `deploy.upload` saves buffers first, so those saves queue
|
||||||
|
/// no upload on save of their own.
|
||||||
|
saving: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DeployState {
|
impl Default for DeployState {
|
||||||
|
|
@ -182,10 +187,18 @@ impl Default for DeployState {
|
||||||
log: VecDeque::new(),
|
log: VecDeque::new(),
|
||||||
next_id: 1,
|
next_id: 1,
|
||||||
queue: VecDeque::new(),
|
queue: VecDeque::new(),
|
||||||
|
saving: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
/// Whether no deployment job runs.
|
||||||
|
pub fn deploy_idle(&self) -> bool {
|
||||||
|
self.deploy.active.is_none()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl DeployState {
|
impl DeployState {
|
||||||
/// Uses `store` for passwords and passphrases instead of the keychain.
|
/// Uses `store` for passwords and passphrases instead of the keychain.
|
||||||
pub fn set_secret_store(&mut self, store: Arc<dyn SecretStore>) {
|
pub fn set_secret_store(&mut self, store: Arc<dyn SecretStore>) {
|
||||||
|
|
@ -315,13 +328,29 @@ fn drain(state: &mut State) {
|
||||||
|
|
||||||
/// What a job does.
|
/// What a job does.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Work {
|
pub(super) enum Work {
|
||||||
/// Test the connection.
|
/// Test the connection.
|
||||||
Test,
|
Test,
|
||||||
/// Upload these paths.
|
/// Upload these paths.
|
||||||
Upload(Vec<String>),
|
Upload(Vec<String>),
|
||||||
/// Download these paths.
|
/// Download these paths.
|
||||||
Download(Vec<String>),
|
Download(Vec<String>),
|
||||||
|
/// List a browser folder.
|
||||||
|
Browse(String),
|
||||||
|
/// Read a file as text.
|
||||||
|
Read(Target),
|
||||||
|
/// Write a browser file.
|
||||||
|
Write(String, String),
|
||||||
|
/// Rename a browser path.
|
||||||
|
Rename(String, String),
|
||||||
|
/// Create a browser folder.
|
||||||
|
Mkdir(String),
|
||||||
|
/// Delete a browser path (confirmed).
|
||||||
|
Delete(String),
|
||||||
|
/// Compare project paths with the server.
|
||||||
|
Compare(Vec<String>),
|
||||||
|
/// Upload these files and delete those (confirmed) on the server.
|
||||||
|
Sync(Vec<String>, Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Work {
|
impl Work {
|
||||||
|
|
@ -331,6 +360,13 @@ impl Work {
|
||||||
Self::Test => "test",
|
Self::Test => "test",
|
||||||
Self::Upload(_) => "upload",
|
Self::Upload(_) => "upload",
|
||||||
Self::Download(_) => "download",
|
Self::Download(_) => "download",
|
||||||
|
Self::Browse(_) | Self::Read(_) => "browse",
|
||||||
|
Self::Write(..) => "save",
|
||||||
|
Self::Rename(..) => "rename",
|
||||||
|
Self::Mkdir(_) => "mkdir",
|
||||||
|
Self::Delete(_) => "delete",
|
||||||
|
Self::Compare(_) => "compare",
|
||||||
|
Self::Sync(..) => "sync",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -431,7 +467,7 @@ fn spawn(
|
||||||
/// Starts a job for a command and returns its pending outcome. The thread
|
/// Starts a job for a command and returns its pending outcome. The thread
|
||||||
/// ignores a failed wake-up send: it only means the outcome was dropped
|
/// ignores a failed wake-up send: it only means the outcome was dropped
|
||||||
/// because the host stopped waiting.
|
/// because the host stopped waiting.
|
||||||
fn start(
|
pub(super) fn start(
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
command: &'static str,
|
command: &'static str,
|
||||||
server_id: Option<&str>,
|
server_id: Option<&str>,
|
||||||
|
|
@ -444,11 +480,7 @@ fn start(
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
let filled = Arc::clone(&slot);
|
let filled = Arc::clone(&slot);
|
||||||
spawn(state, work, job, None, move |job, work| {
|
spawn(state, work, job, None, move |job, work| {
|
||||||
let value = match work {
|
let value = run_work(command, job, work);
|
||||||
Work::Test => test_value(job::test_connection(job)),
|
|
||||||
Work::Upload(paths) => summary_value(job::upload(job, paths)),
|
|
||||||
Work::Download(paths) => summary_value(job::download(job, paths)),
|
|
||||||
};
|
|
||||||
*filled.lock().unwrap_or_else(PoisonError::into_inner) = Some(value);
|
*filled.lock().unwrap_or_else(PoisonError::into_inner) = Some(value);
|
||||||
let _ = tx.send(Ok(Value::Null));
|
let _ = tx.send(Ok(Value::Null));
|
||||||
})?;
|
})?;
|
||||||
|
|
@ -464,6 +496,44 @@ fn start(
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Runs `work` on the job's thread and turns the result into the
|
||||||
|
/// command's value; the caller's mistakes (bad paths) become
|
||||||
|
/// `invalid_params` for `command`.
|
||||||
|
fn run_work(command: &'static str, job: &Job, work: &Work) -> Result<Value, EngineError> {
|
||||||
|
let fail = |e: DeployError| engine_error(command, e);
|
||||||
|
match work {
|
||||||
|
Work::Test => test_value(job::test_connection(job)),
|
||||||
|
Work::Upload(paths) => summary_value(job::upload(job, paths)),
|
||||||
|
Work::Download(paths) => summary_value(job::download(job, paths)),
|
||||||
|
Work::Browse(rel) => {
|
||||||
|
let entries: Vec<Value> = browse::list(job, rel)
|
||||||
|
.map_err(fail)?
|
||||||
|
.into_iter()
|
||||||
|
.map(|e| json!({"name": e.name, "isDir": e.is_dir, "size": e.size}))
|
||||||
|
.collect();
|
||||||
|
Ok(json!({"path": rel, "entries": entries}))
|
||||||
|
}
|
||||||
|
Work::Read(target) => {
|
||||||
|
let read = browse::read(job, target).map_err(fail)?;
|
||||||
|
Ok(json!({"text": read.text, "binary": read.binary, "tooLarge": read.too_large}))
|
||||||
|
}
|
||||||
|
Work::Write(rel, text) => browse::write(job, rel, text.as_bytes())
|
||||||
|
.map(|()| json!({}))
|
||||||
|
.map_err(fail),
|
||||||
|
Work::Rename(from, to) => browse::rename(job, from, to)
|
||||||
|
.map(|()| json!({}))
|
||||||
|
.map_err(fail),
|
||||||
|
Work::Mkdir(rel) => browse::mkdir(job, rel).map(|()| json!({})).map_err(fail),
|
||||||
|
Work::Delete(rel) => browse::delete(job, rel)
|
||||||
|
.map(|n| json!({"deleted": n}))
|
||||||
|
.map_err(fail),
|
||||||
|
Work::Compare(paths) => job::compare(job, paths)
|
||||||
|
.map(|d| json!({"differences": d}))
|
||||||
|
.map_err(fail),
|
||||||
|
Work::Sync(upload, delete) => summary_value(job::sync(job, upload, delete)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An upload or download summary as the command's value.
|
/// An upload or download summary as the command's value.
|
||||||
fn summary_value(result: Result<lutra_deploy::Summary, DeployError>) -> Result<Value, EngineError> {
|
fn summary_value(result: Result<lutra_deploy::Summary, DeployError>) -> Result<Value, EngineError> {
|
||||||
result
|
result
|
||||||
|
|
@ -563,7 +633,7 @@ fn start_queued(state: &mut State) {
|
||||||
/// `file.saveAll`. A configuration that cannot be read is logged and
|
/// `file.saveAll`. A configuration that cannot be read is logged and
|
||||||
/// otherwise ignored, so saving never fails because of deployment.
|
/// otherwise ignored, so saving never fails because of deployment.
|
||||||
pub fn saved(state: &mut State, path: &str) {
|
pub fn saved(state: &mut State, path: &str) {
|
||||||
if state.project.is_none() || state.config.is_none() {
|
if state.project.is_none() || state.config.is_none() || state.deploy.saving {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let Ok((_, deployment)) = load(state, "file.save") else {
|
let Ok((_, deployment)) = load(state, "file.save") else {
|
||||||
|
|
@ -1052,7 +1122,7 @@ impl Command for TrustHostKey {
|
||||||
|
|
||||||
/// Params of `deploy.upload` and `deploy.download`.
|
/// Params of `deploy.upload` and `deploy.download`.
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
||||||
struct TransferParams {
|
struct TransferParams {
|
||||||
/// Project-relative (or absolute, inside the project) files or
|
/// Project-relative (or absolute, inside the project) files or
|
||||||
/// folders; `""` is the whole project.
|
/// folders; `""` is the whole project.
|
||||||
|
|
@ -1060,10 +1130,52 @@ struct TransferParams {
|
||||||
/// The server's id; the default server when absent.
|
/// The server's id; the default server when absent.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
id: Option<String>,
|
id: Option<String>,
|
||||||
|
/// Upload only: save the dirty buffers under `paths` first (default
|
||||||
|
/// true). Ignored by download.
|
||||||
|
#[serde(default)]
|
||||||
|
save_first: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Saves every dirty buffer whose file lies under one of the
|
||||||
|
/// project-relative `paths` (`""` is the whole project), without queueing
|
||||||
|
/// uploads on save for them.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// The first save that fails; the upload does not start then.
|
||||||
|
fn save_under(state: &mut State, paths: &[String]) -> Result<(), EngineError> {
|
||||||
|
let ids: Vec<_> = state
|
||||||
|
.buffers
|
||||||
|
.dirty_ids()
|
||||||
|
.into_iter()
|
||||||
|
.filter(|id| {
|
||||||
|
state.buffers.get(*id).is_some_and(|open| {
|
||||||
|
let rel = open.path.to_string_lossy().replace('\\', "/");
|
||||||
|
paths.iter().any(|p| {
|
||||||
|
p.is_empty()
|
||||||
|
|| rel == *p
|
||||||
|
|| rel
|
||||||
|
.strip_prefix(p.as_str())
|
||||||
|
.is_some_and(|rest| rest.starts_with('/'))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
state.deploy.saving = true;
|
||||||
|
let mut result = Ok(());
|
||||||
|
for id in ids {
|
||||||
|
if let Err(e) = super::file::save_buffer(state, "deploy.upload", id) {
|
||||||
|
result = Err(EngineError::Failed(format!(
|
||||||
|
"saving before the upload failed, so nothing was uploaded: {e}"
|
||||||
|
)));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state.deploy.saving = false;
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turns client paths into project-relative ones.
|
/// Turns client paths into project-relative ones.
|
||||||
fn relative_paths(
|
pub(super) fn relative_paths(
|
||||||
state: &State,
|
state: &State,
|
||||||
command: &str,
|
command: &str,
|
||||||
given: &[String],
|
given: &[String],
|
||||||
|
|
@ -1097,7 +1209,7 @@ impl Command for Upload {
|
||||||
label: "Deployment: Upload to Server",
|
label: "Deployment: Upload to Server",
|
||||||
menu_path: None,
|
menu_path: None,
|
||||||
default_shortcut: None,
|
default_shortcut: None,
|
||||||
params: "{ paths: [project-relative file or folder, \"\" for the whole project], id?: server id (default server when absent) }; returns { files, bytes, skipped, failed }; progress as deployProgress, lines as deployLog; deploy.cancel stops it",
|
params: "{ paths: [project-relative file or folder, \"\" for the whole project], id?: server id (default server when absent), saveFirst?: boolean (default true: save the dirty buffers under paths first) }; returns { files, bytes, skipped, failed, deleted }; progress as deployProgress, lines as deployLog; deploy.cancel stops it",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1109,6 +1221,9 @@ impl Command for Upload {
|
||||||
let command = self.spec().id;
|
let command = self.spec().id;
|
||||||
let p: TransferParams = parse(command, params)?;
|
let p: TransferParams = parse(command, params)?;
|
||||||
let paths = relative_paths(state, command, &p.paths)?;
|
let paths = relative_paths(state, command, &p.paths)?;
|
||||||
|
if state.deploy.active.is_none() && p.save_first.unwrap_or(true) {
|
||||||
|
save_under(state, &paths)?;
|
||||||
|
}
|
||||||
start(state, command, p.id.as_deref(), Work::Upload(paths))
|
start(state, command, p.id.as_deref(), Work::Upload(paths))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
413
crates/engine/src/cmd/deploy_remote.rs
Normal file
413
crates/engine/src/cmd/deploy_remote.rs
Normal file
|
|
@ -0,0 +1,413 @@
|
||||||
|
//! Remote browser, compare and sync commands (C9 D2). They share the job
|
||||||
|
//! slot of [`super::deploy`]: each runs as a pending outcome on a
|
||||||
|
//! `lutra-deploy` thread, one at a time.
|
||||||
|
//!
|
||||||
|
//! **Browser paths** are relative to the server's root folder with `/`
|
||||||
|
//! (`""` is the root); absolute paths and `..` are `invalid_params`.
|
||||||
|
//!
|
||||||
|
//! **Nothing is deleted on the server without an explicit confirmation:**
|
||||||
|
//! `deploy.deleteRemote` needs `confirm: true`, and `deploy.sync` needs
|
||||||
|
//! `confirmDelete: true` whenever its `delete` list is not empty. Without
|
||||||
|
//! it they fail with `invalid_params` before connecting.
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use super::deploy::{Work, relative_paths, start};
|
||||||
|
use super::params::parse;
|
||||||
|
use crate::{Command, CommandSpec, EngineError, Outcome, Registry, State};
|
||||||
|
use lutra_deploy::browse::Target;
|
||||||
|
|
||||||
|
/// Longest text `deploy.writeRemote` accepts.
|
||||||
|
const MAX_WRITE: usize = 4 * 1024 * 1024;
|
||||||
|
|
||||||
|
/// Registers the remote `deploy.*` commands.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`EngineError::Internal`] if an id is taken.
|
||||||
|
pub fn register(registry: &mut Registry) -> Result<(), EngineError> {
|
||||||
|
registry.register(Box::new(Browse))?;
|
||||||
|
registry.register(Box::new(ReadRemote))?;
|
||||||
|
registry.register(Box::new(WriteRemote))?;
|
||||||
|
registry.register(Box::new(RenameRemote))?;
|
||||||
|
registry.register(Box::new(MkdirRemote))?;
|
||||||
|
registry.register(Box::new(DeleteRemote))?;
|
||||||
|
registry.register(Box::new(Compare))?;
|
||||||
|
registry.register(Box::new(Sync))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ids of this module's commands.
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) const COMMAND_IDS: &[&str] = &[
|
||||||
|
"deploy.browse",
|
||||||
|
"deploy.compare",
|
||||||
|
"deploy.deleteRemote",
|
||||||
|
"deploy.mkdirRemote",
|
||||||
|
"deploy.readRemote",
|
||||||
|
"deploy.renameRemote",
|
||||||
|
"deploy.sync",
|
||||||
|
"deploy.writeRemote",
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Whether a job may start: a project and a free slot.
|
||||||
|
fn idle(state: &State) -> bool {
|
||||||
|
state.has_project() && state.deploy_idle()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params naming a browser path.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
struct PathParams {
|
||||||
|
/// The browser path.
|
||||||
|
#[serde(default)]
|
||||||
|
path: String,
|
||||||
|
/// The server's id; the default server when absent.
|
||||||
|
#[serde(default)]
|
||||||
|
id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.browse`.
|
||||||
|
struct Browse;
|
||||||
|
|
||||||
|
impl Command for Browse {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.browse",
|
||||||
|
label: "Deployment: List Server Folder",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ path?: folder relative to the server's root (\"\" for the root), id?: server id }; returns { path, entries: [{ name, isDir, size? }] } folders first",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let p: PathParams = parse(self.spec().id, params)?;
|
||||||
|
start(state, self.spec().id, p.id.as_deref(), Work::Browse(p.path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params of `deploy.readRemote`.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
||||||
|
struct ReadParams {
|
||||||
|
/// A project-relative path, mapped to the server.
|
||||||
|
#[serde(default)]
|
||||||
|
path: Option<String>,
|
||||||
|
/// A browser path, relative to the server's root.
|
||||||
|
#[serde(default)]
|
||||||
|
remote_path: Option<String>,
|
||||||
|
/// The server's id; the default server when absent.
|
||||||
|
#[serde(default)]
|
||||||
|
id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.readRemote`.
|
||||||
|
struct ReadRemote;
|
||||||
|
|
||||||
|
impl Command for ReadRemote {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.readRemote",
|
||||||
|
label: "Deployment: Read Server File",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ path: project-relative file (mapped) | remotePath: file relative to the server's root, id?: server id }; returns { text: string|null (null when missing), binary, tooLarge } (at most 4 MiB)",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let command = self.spec().id;
|
||||||
|
let p: ReadParams = parse(command, params)?;
|
||||||
|
let target = match (p.path, p.remote_path) {
|
||||||
|
(Some(path), None) => {
|
||||||
|
let rel = relative_paths(state, command, &[path])?;
|
||||||
|
Target::Mapped(rel.into_iter().next().unwrap_or_default())
|
||||||
|
}
|
||||||
|
(None, Some(remote)) => Target::Browser(remote),
|
||||||
|
_ => {
|
||||||
|
return Err(EngineError::invalid_params(
|
||||||
|
command,
|
||||||
|
"give exactly one of path and remotePath",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
start(state, command, p.id.as_deref(), Work::Read(target))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params of `deploy.writeRemote`.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
||||||
|
struct WriteParams {
|
||||||
|
/// The browser path of the file.
|
||||||
|
remote_path: String,
|
||||||
|
/// Its new text.
|
||||||
|
text: String,
|
||||||
|
/// The server's id; the default server when absent.
|
||||||
|
#[serde(default)]
|
||||||
|
id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.writeRemote`.
|
||||||
|
struct WriteRemote;
|
||||||
|
|
||||||
|
impl Command for WriteRemote {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.writeRemote",
|
||||||
|
label: "Deployment: Save Server File",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ remotePath: file relative to the server's root, text: at most 4 MiB, id?: server id }; writes the file on the server (creating its folder); returns {}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let command = self.spec().id;
|
||||||
|
let p: WriteParams = parse(command, params)?;
|
||||||
|
if p.text.len() > MAX_WRITE || p.remote_path.trim().is_empty() {
|
||||||
|
return Err(EngineError::invalid_params(
|
||||||
|
command,
|
||||||
|
"remotePath must name a file and text may hold at most 4 MiB",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
start(
|
||||||
|
state,
|
||||||
|
command,
|
||||||
|
p.id.as_deref(),
|
||||||
|
Work::Write(p.remote_path, p.text),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params of `deploy.renameRemote`.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
struct RenameParams {
|
||||||
|
/// The browser path to rename.
|
||||||
|
from: String,
|
||||||
|
/// Its new browser path.
|
||||||
|
to: String,
|
||||||
|
/// The server's id; the default server when absent.
|
||||||
|
#[serde(default)]
|
||||||
|
id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.renameRemote`.
|
||||||
|
struct RenameRemote;
|
||||||
|
|
||||||
|
impl Command for RenameRemote {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.renameRemote",
|
||||||
|
label: "Deployment: Rename on Server",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ from, to: paths relative to the server's root, id?: server id }; returns {}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let command = self.spec().id;
|
||||||
|
let p: RenameParams = parse(command, params)?;
|
||||||
|
if p.from.trim().is_empty() || p.to.trim().is_empty() {
|
||||||
|
return Err(EngineError::invalid_params(
|
||||||
|
command,
|
||||||
|
"from and to must name paths",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
start(state, command, p.id.as_deref(), Work::Rename(p.from, p.to))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.mkdirRemote`.
|
||||||
|
struct MkdirRemote;
|
||||||
|
|
||||||
|
impl Command for MkdirRemote {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.mkdirRemote",
|
||||||
|
label: "Deployment: New Folder on Server",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ path: new folder relative to the server's root, id?: server id }; creates it and its parents; returns {}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let command = self.spec().id;
|
||||||
|
let p: PathParams = parse(command, params)?;
|
||||||
|
if p.path.trim().is_empty() {
|
||||||
|
return Err(EngineError::invalid_params(
|
||||||
|
command,
|
||||||
|
"path must name the new folder",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
start(state, command, p.id.as_deref(), Work::Mkdir(p.path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params of `deploy.deleteRemote`.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
struct DeleteParams {
|
||||||
|
/// The browser path to delete.
|
||||||
|
path: String,
|
||||||
|
/// The user confirmed the deletion.
|
||||||
|
#[serde(default)]
|
||||||
|
confirm: bool,
|
||||||
|
/// The server's id; the default server when absent.
|
||||||
|
#[serde(default)]
|
||||||
|
id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.deleteRemote`.
|
||||||
|
struct DeleteRemote;
|
||||||
|
|
||||||
|
impl Command for DeleteRemote {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.deleteRemote",
|
||||||
|
label: "Deployment: Delete on Server",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ path: file or folder relative to the server's root (never the root), confirm: true (required: the user agreed), id?: server id }; a folder goes with everything inside; returns { deleted: count }",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let command = self.spec().id;
|
||||||
|
let p: DeleteParams = parse(command, params)?;
|
||||||
|
if !p.confirm {
|
||||||
|
return Err(EngineError::invalid_params(
|
||||||
|
command,
|
||||||
|
"deleting on the server needs confirm: true after the user agreed",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
start(state, command, p.id.as_deref(), Work::Delete(p.path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params of `deploy.compare`.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
struct CompareParams {
|
||||||
|
/// Project-relative files or folders; `""` is the whole project.
|
||||||
|
paths: Vec<String>,
|
||||||
|
/// The server's id; the default server when absent.
|
||||||
|
#[serde(default)]
|
||||||
|
id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.compare`.
|
||||||
|
struct Compare;
|
||||||
|
|
||||||
|
impl Command for Compare {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.compare",
|
||||||
|
label: "Deployment: Compare with Server",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ paths: [project-relative file or folder, \"\" for the whole project], id?: server id }; returns { differences: [{ path, status: localOnly|remoteOnly|different, localSize?, remoteSize? }] } sorted by path; equal files are left out",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let command = self.spec().id;
|
||||||
|
let p: CompareParams = parse(command, params)?;
|
||||||
|
let paths = relative_paths(state, command, &p.paths)?;
|
||||||
|
start(state, command, p.id.as_deref(), Work::Compare(paths))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Params of `deploy.sync`.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
||||||
|
struct SyncParams {
|
||||||
|
/// Project-relative files to upload.
|
||||||
|
#[serde(default)]
|
||||||
|
upload: Vec<String>,
|
||||||
|
/// Project-relative files to delete on the server.
|
||||||
|
#[serde(default)]
|
||||||
|
delete: Vec<String>,
|
||||||
|
/// The user confirmed the deletions.
|
||||||
|
#[serde(default)]
|
||||||
|
confirm_delete: bool,
|
||||||
|
/// The server's id; the default server when absent.
|
||||||
|
#[serde(default)]
|
||||||
|
id: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `deploy.sync`.
|
||||||
|
struct Sync;
|
||||||
|
|
||||||
|
impl Command for Sync {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "deploy.sync",
|
||||||
|
label: "Deployment: Sync with Server",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "{ upload?: [project-relative files], delete?: [project-relative files to delete on the server], confirmDelete: true (required when delete is not empty), id?: server id }; returns { files, bytes, skipped, failed, deleted }",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self, state: &State) -> bool {
|
||||||
|
idle(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let command = self.spec().id;
|
||||||
|
let p: SyncParams = parse(command, params)?;
|
||||||
|
if !p.delete.is_empty() && !p.confirm_delete {
|
||||||
|
return Err(EngineError::invalid_params(
|
||||||
|
command,
|
||||||
|
"deleting on the server needs confirmDelete: true after the user agreed",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if p.upload.is_empty() && p.delete.is_empty() {
|
||||||
|
return Err(EngineError::invalid_params(command, "nothing to sync"));
|
||||||
|
}
|
||||||
|
let upload = if p.upload.is_empty() {
|
||||||
|
Vec::new()
|
||||||
|
} else {
|
||||||
|
relative_paths(state, command, &p.upload)?
|
||||||
|
};
|
||||||
|
let delete = if p.delete.is_empty() {
|
||||||
|
Vec::new()
|
||||||
|
} else {
|
||||||
|
relative_paths(state, command, &p.delete)?
|
||||||
|
};
|
||||||
|
start(state, command, p.id.as_deref(), Work::Sync(upload, delete))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -112,6 +112,19 @@ fn save(state: &mut State, command: &str, id: BufferId) -> Result<u64, EngineErr
|
||||||
Ok(version)
|
Ok(version)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Writes buffer `id` to disk like `file.save`, for other commands (such as
|
||||||
|
/// `deploy.upload`, which saves before it uploads).
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Like `file.save`.
|
||||||
|
pub(super) fn save_buffer(
|
||||||
|
state: &mut State,
|
||||||
|
command: &str,
|
||||||
|
id: BufferId,
|
||||||
|
) -> Result<u64, EngineError> {
|
||||||
|
save(state, command, id)
|
||||||
|
}
|
||||||
|
|
||||||
/// Params of `file.save`.
|
/// Params of `file.save`.
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
mod app;
|
mod app;
|
||||||
mod buffer;
|
mod buffer;
|
||||||
pub mod deploy;
|
pub mod deploy;
|
||||||
|
mod deploy_remote;
|
||||||
mod file;
|
mod file;
|
||||||
pub mod git;
|
pub mod git;
|
||||||
pub mod git_blame;
|
pub mod git_blame;
|
||||||
|
|
@ -98,6 +99,7 @@ pub(crate) const BUILTIN_COMMAND_IDS: &[&[&str]] = &[
|
||||||
app::COMMAND_IDS,
|
app::COMMAND_IDS,
|
||||||
buffer::COMMAND_IDS,
|
buffer::COMMAND_IDS,
|
||||||
deploy::COMMAND_IDS,
|
deploy::COMMAND_IDS,
|
||||||
|
deploy_remote::COMMAND_IDS,
|
||||||
file::COMMAND_IDS,
|
file::COMMAND_IDS,
|
||||||
git::COMMAND_IDS,
|
git::COMMAND_IDS,
|
||||||
git_blame::COMMAND_IDS,
|
git_blame::COMMAND_IDS,
|
||||||
|
|
|
||||||
|
|
@ -384,3 +384,122 @@ fn bad_params_and_no_project() {
|
||||||
Err(EngineError::Disabled(_))
|
Err(EngineError::Disabled(_))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remote_browser_compare_sync_and_save_first() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let remote = tmp.path().join("remote");
|
||||||
|
std::fs::create_dir_all(remote.join("www/public")).unwrap();
|
||||||
|
std::fs::write(remote.join("www/public/old.php"), "<?php // old").unwrap();
|
||||||
|
let ftp = FtpServer::start(&remote, "deploy", PASSWORD).unwrap();
|
||||||
|
let host = Host::new(tmp.path());
|
||||||
|
let proj = project(tmp.path());
|
||||||
|
host.run("project.open", json!({"path": proj})).unwrap();
|
||||||
|
host.run(
|
||||||
|
"deploy.saveServer",
|
||||||
|
json!({"server": {
|
||||||
|
"name": "Shared", "kind": "ftp", "host": "127.0.0.1", "port": ftp.addr.port(),
|
||||||
|
"user": "deploy", "root": "/www", "excluded": ["composer.json"],
|
||||||
|
}, "secret": PASSWORD}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let listed = host
|
||||||
|
.run("deploy.browse", json!({"path": "public"}))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(listed["entries"][0]["name"], "old.php");
|
||||||
|
assert_eq!(listed["entries"][0]["isDir"], false);
|
||||||
|
let bad = host
|
||||||
|
.run("deploy.browse", json!({"path": "../.."}))
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(bad.code(), "invalid_params", "{bad}");
|
||||||
|
|
||||||
|
host.run(
|
||||||
|
"deploy.writeRemote",
|
||||||
|
json!({"remotePath": "notes/n.txt", "text": "hi"}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
std::fs::read_to_string(remote.join("www/notes/n.txt")).unwrap(),
|
||||||
|
"hi"
|
||||||
|
);
|
||||||
|
let read = host
|
||||||
|
.run("deploy.readRemote", json!({"remotePath": "notes/n.txt"}))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(read["text"], "hi");
|
||||||
|
let missing = host
|
||||||
|
.run("deploy.readRemote", json!({"path": "public/index.php"}))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(missing["text"], Value::Null);
|
||||||
|
host.run(
|
||||||
|
"deploy.renameRemote",
|
||||||
|
json!({"from": "notes/n.txt", "to": "notes/m.txt"}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
host.run("deploy.mkdirRemote", json!({"path": "empty/dir"}))
|
||||||
|
.unwrap();
|
||||||
|
assert!(remote.join("www/empty/dir").is_dir());
|
||||||
|
let refused = host
|
||||||
|
.run("deploy.deleteRemote", json!({"path": "notes"}))
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(refused.code(), "invalid_params");
|
||||||
|
assert!(
|
||||||
|
remote.join("www/notes/m.txt").exists(),
|
||||||
|
"nothing is deleted without confirm"
|
||||||
|
);
|
||||||
|
let deleted = host
|
||||||
|
.run(
|
||||||
|
"deploy.deleteRemote",
|
||||||
|
json!({"path": "notes", "confirm": true}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(deleted["deleted"], 2);
|
||||||
|
|
||||||
|
let diffs = host
|
||||||
|
.run("deploy.compare", json!({"paths": ["public"]}))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
diffs["differences"],
|
||||||
|
json!([
|
||||||
|
{"path": "public/index.php", "status": "localOnly", "localSize": 13},
|
||||||
|
{"path": "public/old.php", "status": "remoteOnly", "remoteSize": 12},
|
||||||
|
])
|
||||||
|
);
|
||||||
|
let unconfirmed = host
|
||||||
|
.run(
|
||||||
|
"deploy.sync",
|
||||||
|
json!({"upload": ["public/index.php"], "delete": ["public/old.php"]}),
|
||||||
|
)
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(unconfirmed.code(), "invalid_params");
|
||||||
|
assert!(remote.join("www/public/old.php").exists());
|
||||||
|
let synced = host
|
||||||
|
.run(
|
||||||
|
"deploy.sync",
|
||||||
|
json!({"upload": ["public/index.php"], "delete": ["public/old.php"], "confirmDelete": true}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
(synced["files"].clone(), synced["deleted"].clone()),
|
||||||
|
(json!(1), json!(1))
|
||||||
|
);
|
||||||
|
assert!(!remote.join("www/public/old.php").exists());
|
||||||
|
|
||||||
|
let opened = host
|
||||||
|
.run("file.open", json!({"path": "public/index.php"}))
|
||||||
|
.unwrap();
|
||||||
|
let id = opened["id"].as_u64().unwrap();
|
||||||
|
host.run(
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "// unsaved\n"}]}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
host.run("deploy.upload", json!({"paths": ["public"]}))
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
std::fs::read_to_string(remote.join("www/public/index.php")).unwrap(),
|
||||||
|
"// unsaved\n<?php echo 1;",
|
||||||
|
"the upload saved the buffer first"
|
||||||
|
);
|
||||||
|
assert!(!host.transcript().contains(PASSWORD));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue