Fix upload and display of files

This commit is contained in:
Leonard Steppy 2024-12-13 00:46:35 +01:00
parent dc9fb045f5
commit 4f75581c8b
3 changed files with 25 additions and 30 deletions

View File

@ -9,15 +9,6 @@ pub struct ServerActions<'a> {
pub actions: Vec<FileAction>
}
impl <'a> ServerActions<'a> {
pub fn new(server: &'a Server) -> Self {
Self {
server,
actions: vec![],
}
}
}
impl Display for ServerActions<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: ", self.server.ssh_name)?;
@ -37,10 +28,10 @@ pub struct FileAction {
impl Display for FileAction {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.kind {
Action::Add => write!(f, "+ adding {:?}", self.file),
Action::Replace => write!(f, "~ replacing {:?}", self.file),
Action::Delete => write!(f, "- deleting {:?}", self.file),
Action::Rename { new_name } => write!(f, "* renaming {:?} -> {:?}", self.file, new_name),
Action::Add => write!(f, "+ adding {}", self.file.to_string_lossy()),
Action::Replace => write!(f, "~ replacing {}", self.file.to_string_lossy()),
Action::Delete => write!(f, "- deleting {}", self.file.to_string_lossy()),
Action::Rename { new_name } => write!(f, "* renaming {} -> {}", self.file.to_string_lossy(), new_name.to_string_lossy()),
}
}
}

View File

@ -113,6 +113,7 @@ impl FileMatcher {
}
}
//TODO return kind of match (partial | full) and consider replacing on full match in delete policy
pub fn matches(&self, file_name: &str) -> bool {
file_name.starts_with(&self.name)
&& self

View File

@ -9,6 +9,7 @@ use lazy_regex::{lazy_regex, Lazy, Regex};
use server::{Server, ServerReference};
use std::cell::LazyCell;
use std::env;
use std::ffi::OsString;
use std::hash::Hash;
use std::io::Write;
use std::iter::once;
@ -46,13 +47,13 @@ enum Command {
/// The file to upload
file: PathBuf,
/// How to handle older versions of the file
#[arg(short = 'a', long, default_value = "delete", default_missing_value = "archive", num_args = 0..1)]
#[arg(short = 'a', long, default_value = "delete", default_missing_value = "archive", num_args = 0..=1)]
old_version_policy: OldVersionPolicy,
/// The directory where to upload to, relative to the server directory
#[arg(short = 'p', long, default_value = "plugins")]
upload_directory: PathBuf,
/// Skip the confirmation dialog
#[arg(long, default_value = "false", default_missing_value = "true", num_args = 0..1)]
#[arg(long, default_value = "false")]
no_confirm: bool,
/// The prefix of the name of older versions of the file, which should be replaced or deleted
#[arg(short, long)]
@ -135,24 +136,22 @@ fn main() -> Result<(), String> {
file_matcher = file_matcher.and_extension(extension);
}
let add_action_iter = once(FileAction {
file: file.clone(),
let file_name = file_name_info.to_full_file_name();
let add_action = FileAction {
file: PathBuf::from(&file_name),
kind: Action::Add,
});
};
let mut files = output.lines();
match old_version_policy {
OldVersionPolicy::Ignore => {
let file_name = file_name_info.to_full_file_name();
vec![if files.any(|file| file == file_name) {
FileAction {
file: PathBuf::from(file_name),
file: PathBuf::from(&file_name),
kind: Action::Replace,
}
} else {
FileAction {
file: file.clone(),
kind: Action::Add,
}
add_action
}]
}
OldVersionPolicy::Archive => files
@ -163,7 +162,7 @@ fn main() -> Result<(), String> {
new_name: format!("{file}{}", file.chars().last().unwrap_or('1')).into(),
},
})
.chain(add_action_iter)
.chain(once(add_action))
.collect(),
OldVersionPolicy::Delete => files
.filter(|file| file_matcher.matches(file))
@ -171,7 +170,7 @@ fn main() -> Result<(), String> {
file: PathBuf::from(file),
kind: Action::Delete,
})
.chain(add_action_iter)
.chain(once(add_action))
.collect(),
}
},
@ -206,12 +205,16 @@ fn main() -> Result<(), String> {
for file_action in server_actions.actions {
match file_action.kind {
Action::Add | Action::Replace => {
let mut destination = OsString::from(&server.ssh_name);
destination.push(":");
destination.push(&server.server_directory_path);
if !server.server_directory_path.to_string_lossy().ends_with("/") {
destination.push("/");
}
destination.push(&upload_directory);
ShellCmd::new("scp")
.arg(file.clone())
.arg(format!(
"{}:{:?}/{upload_directory:?}",
server.ssh_name, server.server_directory_path
))
.arg(destination)
.spawn()
.map_err(|e| format!("failed to upload file: {e}"))?
.wait()