diff options
| -rw-r--r-- | src/config.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 15 | ||||
| -rw-r--r-- | src/utils.rs | 19 |
3 files changed, 33 insertions, 3 deletions
diff --git a/src/config.rs b/src/config.rs index 4de9021..39b0377 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,7 @@ pub struct Config { #[derive(Deserialize)] pub struct ProjectConfig { - pub name: String, + pub name: Option<String>, pub path: Option<String>, pub url: String, pub description: Option<String>, diff --git a/src/main.rs b/src/main.rs index 2007e39..18912d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod config; +mod utils; use std::{ env, @@ -65,7 +66,12 @@ fn init() { path.push(Path::new( project.path.as_ref().unwrap_or(&config.default_path), )); - path.push(project.name); + + match project.name { + Some(name) => path.push(name), + None => path.push(utils::get_repo_name(&project.url)) + }; + if path.exists() { println!("{:#?} already exists, skipping.", path); continue; @@ -94,7 +100,12 @@ fn update() { path.push(Path::new( project.path.as_ref().unwrap_or(&config.default_path), )); - path.push(project.name); + + match project.name { + Some(name) => path.push(name), + None => path.push(utils::get_repo_name(&project.url)) + }; + if !path.exists() { println!("{:#?} doesn't exist, skipping.", &path); continue; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..6e737aa --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,19 @@ +pub fn get_repo_name(url: &str) -> &str { + let start = url.rfind('/').unwrap_or_default() + 1; + if url.ends_with(".git") + { + return &url[start..url.len() - 4]; + } + + &url[start..] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn get_repo_name_test() { + assert_eq!(get_repo_name("https://github.com/Bond-009/git-mirror.git"), "git-mirror"); + } +} |
