Skip to content
Snippets Groups Projects
Commit 667e17d0 authored by nfontrod's avatar nfontrod
Browse files

src/remote.rs: add a function to check remote paths

parent be2a92ba
No related branches found
No related tags found
No related merge requests found
...@@ -138,11 +138,46 @@ pub fn show() -> () { ...@@ -138,11 +138,46 @@ pub fn show() -> () {
display_remotes(dic_remote); 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 /// Function that update the current gblk config file for the project folder
/// # Arguments /// # Arguments
/// - `key` : The remote name to update /// - `key` : The remote name to update
/// - `value`: The value of the key 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) -> () { pub(crate) fn update_config(key: &str, value: &str, global: bool) -> () {
let mut my_config = parse_config(global); let mut my_config = parse_config(global);
let mut remotes = match my_config.gblk_remote { let mut remotes = match my_config.gblk_remote {
...@@ -152,14 +187,14 @@ pub(crate) fn update_config(key: &str, value: &str, global: bool) -> () { ...@@ -152,14 +187,14 @@ pub(crate) fn update_config(key: &str, value: &str, global: bool) -> () {
let mut updated = false; let mut updated = false;
for r in remotes.iter_mut() { for r in remotes.iter_mut() {
if r.name == key { if r.name == key {
r.url = value.to_owned(); r.url = add_url(value.to_owned());
updated = true; updated = true;
} }
} }
if !updated { if !updated {
remotes.push(RemoteConfig { remotes.push(RemoteConfig {
name: key.to_owned(), name: key.to_owned(),
url: value.to_owned(), url: add_url(value.to_owned()),
}) })
} }
my_config.gblk_remote = Some(remotes); my_config.gblk_remote = Some(remotes);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment