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

View File

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