1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use super::*;
impl SwarmSSH {
pub fn upload_task<C, P>(&self, content: C, remote_path: P) -> QResult<UploadTask>
where
P: AsRef<Path>,
ContentResolver: TryFrom<C, Error = QError>,
{
let content = ContentResolver::try_from(content)?.content;
Ok(UploadTask { content, target: remote_path.as_ref().to_path_buf(), permission: 0o644, session: &self.session })
}
}
impl<'s> UploadTask<'s> {
pub fn get_permission(&self) -> i32 {
self.permission
}
pub fn set_permission(&mut self, permission: i32) {
self.permission = permission;
}
pub fn with_permission(mut self, permission: i32) -> Self {
self.permission = permission;
self
}
pub fn get_remote_path(&self) -> &Path {
&self.target
}
pub fn set_remote_path<P: AsRef<Path>>(&mut self, remote_path: P) {
self.target = remote_path.as_ref().to_path_buf();
}
pub fn with_remote_path<P: AsRef<Path>>(mut self, remote_path: P) -> Self {
self.target = remote_path.as_ref().to_path_buf();
self
}
pub async fn execute(self) -> QResult<()> {
let mut scp = self.session.scp_send(&self.target, self.permission, self.content.len() as u64, None)?;
scp.write(&self.content)?;
Ok(())
}
}
impl TryFrom<&Path> for ContentResolver {
type Error = QError;
fn try_from(path: &Path) -> QResult<Self> {
let mut file = File::open(path)?;
let mut content = Vec::new();
file.read_to_end(&mut content)?;
Ok(Self { content })
}
}
impl TryFrom<&PathBuf> for ContentResolver {
type Error = QError;
fn try_from(path: &PathBuf) -> QResult<Self> {
ContentResolver::try_from(path.as_path())
}
}
impl TryFrom<&[u8]> for ContentResolver {
type Error = QError;
fn try_from(data: &[u8]) -> QResult<Self> {
Ok(Self { content: data.to_vec() })
}
}
impl TryFrom<&str> for ContentResolver {
type Error = QError;
fn try_from(data: &str) -> QResult<Self> {
Ok(Self { content: data.as_bytes().to_vec() })
}
}