repos/
util.rs

1use std::fs;
2use std::path::Path;
3
4use url::Url;
5
6/// Validate repository url.
7///
8/// Notes:
9/// * Relative URLs without base (scp-like syntax) are not supported.
10///
11///   e.g. `[user@]host.xz:path/to/repo.git` or `[user@]host.xz:~/path/to/repo.git`
12pub fn validate_repo_url(url: &str) -> Result<(), String> {
13    match Url::parse(url) {
14        Ok(_parsed) => Ok(()),
15        Err(err) => return Err(format!("{}", err.to_string())),
16    }
17}
18
19/// Get repository server (host[:port]) from repository url.
20pub fn repo_server_from_url(url: &str) -> Result<String, String> {
21    let parsed = match Url::parse(url) {
22        Ok(parsed) => parsed,
23        Err(err) => return Err(format!("{}", err.to_string())),
24    };
25    let host = parsed.host_str().unwrap();
26    let port = parsed.port();
27    // URL scheme is ignored.
28    let server = match port {
29        None => host.to_owned(),
30        _ => host.to_owned() + ":" + &port.unwrap().to_string(),
31    };
32    Ok(server.to_string())
33}
34
35/// Convert repository url to relative repository directory.
36pub fn repo_url_to_relpath(url: &str) -> Result<String, String> {
37    let parsed = match Url::parse(url) {
38        Ok(parsed) => parsed,
39        Err(err) => return Err(format!("{}", err.to_string())),
40    };
41    let host = parsed.host_str().unwrap();
42    let port = parsed.port();
43    let path = parsed.path();
44    let relpath = match port {
45        None => host.to_owned() + path,
46        _ => host.to_owned() + ":" + &port.unwrap().to_string() + path,
47    };
48    // TODO bare repository needs ".git" suffix?
49    Ok(relpath.trim_right_matches(".git").to_string())
50}
51
52/// Delete repository relative path.
53pub fn delete_repo_relpath(relpath: &Path) {
54    let local_relpath = relpath.to_str().unwrap();
55    if relpath.is_dir() {
56        // Delete repo directory
57        println!(
58            "Found repository directory '{}'. Try to delete it...",
59            &local_relpath
60        );
61        match fs::remove_dir_all(relpath) {
62            Ok(_) => {
63                println!(
64                    "Local repository directory '{}' is deleted.",
65                    &local_relpath
66                );
67                println!("Please manually delete repository from metadata file.");
68            }
69            Err(err) => println!(
70                "Failed to delete repository directory '{}' because of: {}",
71                &local_relpath,
72                err.to_string()
73            ),
74        }
75    } else if relpath.exists() {
76        // Delete it whatever.
77        println!(
78            "The repository path '{}' is not a directory. Try to delete it whatever...",
79            &local_relpath
80        );
81        match fs::remove_file(relpath) {
82            Ok(_) => {
83                println!("Local repository path '{}' is deleted.", &local_relpath);
84                println!("Please manually delete repository from metadata file.");
85            }
86            Err(err) => println!(
87                "Failed to delete repository path '{}' because of: {}",
88                &local_relpath,
89                err.to_string()
90            ),
91        }
92    } else {
93        // Repo directory does not exist.
94        println!(
95            "The repository directory '{}' does not exists.",
96            &local_relpath
97        );
98    }
99}
100
101/// Generate http proxy url.
102pub fn gen_proxy_url(scheme: &str, host: &str, port: u16) -> String {
103    let proxy_url = scheme.to_owned() + "://" + host + ":" + &port.to_string();
104    proxy_url.to_string()
105}
106
107/// Generate alternative url for vcs.
108pub fn gen_alternative_url(vcs: &str, url: &str) -> Option<String> {
109    if vcs == "git" {
110        let alternative_url = if url.ends_with(".git") {
111            url.trim_right_matches(".git").to_string()
112        } else {
113            url.to_string() + ".git"
114        };
115        Some(alternative_url)
116    } else {
117        // Other vcs unsupported now.
118        None
119    }
120}