use crate::prelude::*;
use serde_json::json;
use std::{env, fs, io, path::Path};
pub struct Task {
remote_path: String,
local_path: String,
}
impl Task {
pub fn new(remote_path: String, local_path: String) -> Self {
Self {
remote_path,
local_path,
}
}
}
impl GenericTask<String> for Task {
fn prepare(&self, host: Host) -> Result<String> {
let local_path = Path::new(&self.local_path);
if local_path.is_absolute() {
return Err(Box::new(Error::IsAbsolute(
"Local path should be a relative path, not absolute".to_string(),
)));
}
let cwd = env::current_dir()?;
let fullpath = cwd.join(host.id.to_string()).join(local_path);
let fulldir = fullpath.parent().unwrap();
fs::create_dir_all(fulldir)?;
Ok(String::from(fullpath.to_string_lossy()))
}
fn apply(&self, host: Host, local_path: String) -> TaskResult {
let sess = host.get_session()?;
let sftp = sess.sftp()?;
let mut remote_file = sftp.open(Path::new(&self.remote_path))?;
let mut local_file = fs::File::create(&local_path)?;
let size = io::copy(&mut remote_file, &mut local_file)?;
Ok(json!({
"file_path": local_path,
"file_size": size,
}))
}
}