atsh_lib/
lib.rs

1mod config;
2mod connection;
3mod storage;
4
5pub mod atsh {
6    use std::io::{Error, ErrorKind};
7    use std::path::Path;
8    use tracing::debug;
9
10    use crate::connection::Remotes;
11    use crate::storage::log::setup_logging;
12
13    type Result<T> = std::result::Result<T, Error>;
14
15    // export the objects to the outside
16    pub use crate::config::CONFIG;
17    pub use crate::connection::Remote;
18
19    pub fn initialize(work_dir: Option<impl AsRef<Path>>) -> Result<()> {
20        if let Some(p) = work_dir {
21            CONFIG.set_work_dir(p)?;
22        }
23        let w = CONFIG.get_work_dir();
24        setup_logging(w)?;
25        debug!("success initialize at {:?}", w);
26        Ok(())
27    }
28
29    pub fn add(
30        user: &str,
31        password: &str,
32        ip: &str,
33        port: u16,
34        name: &Option<impl AsRef<str>>,
35        note: &Option<impl AsRef<str>>,
36    ) -> Result<usize> {
37        Remotes::add(user, password, ip, port, name, note)
38    }
39
40    pub fn add_remote(remote: &Remote) -> Result<usize> {
41        add(
42            &remote.user,
43            &remote.password,
44            &remote.ip,
45            remote.port,
46            &remote.name,
47            &remote.note,
48        )
49    }
50
51    pub fn remove(index: &Vec<usize>) -> Result<usize> {
52        Remotes::delete(index)
53    }
54
55    pub fn get(index: usize) -> Result<Option<Remote>> {
56        Remotes::get(index)
57    }
58
59    pub fn try_get(index: usize) -> Result<Remote> {
60        Remotes::try_get(index)
61    }
62
63    pub fn get_all() -> Result<Vec<Remote>> {
64        let remotes = Remotes::get_all()?;
65        Ok(remotes.0)
66    }
67
68    pub fn pprint(all: bool) -> Result<()> {
69        let remotes = Remotes::get_all()?;
70        remotes.pprint(all);
71        Ok(())
72    }
73
74    // pub fn list(all: bool) -> Result<()> {
75    //     if all {
76    //         Remotes::list_all()
77    //     } else {
78    //         Remotes::list()
79    //     }
80    // }
81
82    // auth params means try auth against the server
83    pub fn login(index: usize, auth: bool) -> Result<()> {
84        let remote = Remotes::try_get(index)?;
85        remote.login(auth)
86    }
87
88    #[deprecated(
89        since = "0.1.2",
90        note = "This function is not clearly expressed; use `upload/download` instead."
91    )]
92    pub fn copy(index: usize, path: &str) -> Result<()> {
93        let paths = path.split('=').collect::<Vec<&str>>();
94        if paths.len() != 2 {
95            return Err(Error::new(
96                ErrorKind::InvalidInput,
97                "path format error, like `from=to`",
98            ));
99        }
100        let remote = Remotes::try_get(index)?;
101        if std::path::PathBuf::from(paths[0]).exists() {
102            remote.upload(paths[0], paths[1])
103        } else {
104            remote.download(paths[0], paths[1])
105        }
106    }
107
108    pub fn upload(index: usize, path: &Vec<impl AsRef<str>>) -> Result<()> {
109        if path.len() != 2 {
110            return Err(Error::new(
111                ErrorKind::InvalidInput,
112                "path format error, like `upload -p /local/path /remote/path`",
113            ));
114        }
115        let (local, remote) = (path[0].as_ref(), path[1].as_ref());
116        if !Path::new(local).exists() {
117            return Err(Error::new(ErrorKind::NotFound, "the upload file not found"));
118        }
119
120        Remotes::try_get(index)?.upload(local, remote)
121    }
122
123    pub fn download(index: usize, path: &Vec<impl AsRef<str>>) -> Result<()> {
124        if path.len() != 2 {
125            return Err(Error::new(
126                ErrorKind::InvalidInput,
127                "path format error, like `upload -p /remote/path /local/path`",
128            ));
129        }
130        let (remote, local) = (path[0].as_ref(), path[1].as_ref());
131
132        Remotes::try_get(index)?.download(remote, local)
133    }
134}