fast_scp/
utils.rs

1use dirs_next::home_dir;
2use std::path::PathBuf;
3
4use crate::error::ScpError;
5
6pub fn with_retry<T, F>(f: F, max_retries: u32) -> anyhow::Result<T, ScpError>
7where
8    F: Fn() -> anyhow::Result<T, ScpError>,
9{
10    let mut retries = 0;
11    loop {
12        match f() {
13            Ok(x) => return Ok(x),
14            Err(e) => {
15                if retries >= max_retries {
16                    return Err(e);
17                }
18
19                retries += 1;
20            }
21        }
22    }
23}
24
25pub fn get_private_key_path(private_key: &Option<PathBuf>) -> anyhow::Result<PathBuf, ScpError> {
26    match private_key {
27                Some(path) => Ok(PathBuf::from(path)),
28                None => Ok(home_dir()
29                    .ok_or(
30                        ScpError::Io(
31                            std::io::Error::new(
32                                std::io::ErrorKind::Other,
33                                "Could not find home directory, please provide the private key path using the --private-key-path <key> flag",
34                            ),
35                        ),
36                    )?
37                    .join(".ssh/id_rsa")),
38            }
39}