From 667e17d0752c8fbe9a937dae6e361f60c0a4b39f Mon Sep 17 00:00:00 2001 From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr> Date: Wed, 15 May 2024 10:49:54 +0200 Subject: [PATCH] src/remote.rs: add a function to check remote paths --- src/remote.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/remote.rs b/src/remote.rs index 79fcf76..ff2119e 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -138,11 +138,46 @@ pub fn show() -> () { display_remotes(dic_remote); } +/// Turn a remote URL into an absolute path if located in the current +/// filesystem +/// +/// # Arguments +/// - `url`: The URL to canonicalize or not +fn add_url(url: String) -> String { + let tmp = url.split(":").collect::<Vec<&str>>(); + if tmp.len() == 1 { + let path = PathBuf::from(&url); + if !path.is_dir() { + eprintln!("{}: The local path {} is not a directory !", "error".red(), &url.yellow()); + exit(1); + } + path.canonicalize().unwrap().to_str().unwrap().to_string() + } else if tmp.len() == 2 && tmp.get(0).unwrap() == &"file" { + let path = PathBuf::from(tmp.get(1).unwrap()); + if !path.is_dir() { + eprintln!("{}: The local path {} is not a directory !", "error".red(), &url.yellow()); + exit(1); + } + path.canonicalize().unwrap().to_str().unwrap().to_string() + } else if tmp.len() == 2 { + let tmp_url = tmp.get(1).unwrap(); + if !tmp_url.starts_with("/") { + eprintln!("{}: The remote path {} must be absolute !", "error".red(), &url.yellow()); + exit(1); + } + url + } else { + eprintln!("{}: The path {} is not well defined !", "error".red(), &url.yellow()); + exit(1); + } +} + + /// Function that update the current gblk config file for the project folder /// # Arguments /// - `key` : The remote name to update /// - `value`: The value of the key to update -/// - `global: A bolean indicating whether to update the global or the local configuration +/// - `global`: A bolean indicating whether to update the global or the local configuration pub(crate) fn update_config(key: &str, value: &str, global: bool) -> () { let mut my_config = parse_config(global); let mut remotes = match my_config.gblk_remote { @@ -152,14 +187,14 @@ pub(crate) fn update_config(key: &str, value: &str, global: bool) -> () { let mut updated = false; for r in remotes.iter_mut() { if r.name == key { - r.url = value.to_owned(); + r.url = add_url(value.to_owned()); updated = true; } } if !updated { remotes.push(RemoteConfig { name: key.to_owned(), - url: value.to_owned(), + url: add_url(value.to_owned()), }) } my_config.gblk_remote = Some(remotes); -- GitLab