From d1592e83a56fccaab60e466f0da77f2159985349 Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Tue, 21 Dec 2021 20:35:40 -0500 Subject: [PATCH] Add category feature --- src/config.rs | 4 ++- src/lib.rs | 72 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3c63823..2e3ab55 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,13 +18,15 @@ pub struct Config { pub struct Repository { pub name: String, pub url: String, + pub category: Option, } impl Repository { - pub fn new(name: String, url: String) -> Repository { + pub fn new(name: String, url: String, category: Option) -> Repository { Repository { name, url, + category, } } } diff --git a/src/lib.rs b/src/lib.rs index ce3388f..f66ce6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub struct State { cmd: Option, url: Option, name: Option, + category: Option, pub config: String, } @@ -45,6 +46,7 @@ impl State { cmd: None, url: None, name: None, + category: None, config: format!("{}/.config/shepherd/config.toml", env::var("HOME").unwrap()), }; @@ -79,7 +81,19 @@ impl State { } else if let None = state.cmd { // add command if x == "add" { - let name = args.next(); + let next = args.next(); + let name: Option; + match next { + Some(ref x) => match &x[..] { + "--category" => { + state.category = args.next(); + name = args.next(); + } + _ => name = next, + }, + None => name = None, + } + eprintln!("Category: {:?}", name); match name { Some(x) => { state.name = Some(x); @@ -150,6 +164,10 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box> { Some(x) => x, _ => String::new(), }, + match state.category { + Some(x) => Some(x), + _ => None, + }, ); if let None = config.repositories.iter().find(|x| **x == repo) { config.repositories.push(repo); @@ -161,7 +179,7 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box> { } Some(Cmd::List) => { // Get the largest entry by name - let width: usize = match config + let name_width: usize = match config .repositories .iter() .max_by(|x, y| x.name.len().cmp(&y.name.len())) @@ -169,9 +187,33 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box> { Some(x) => x.name.len(), None => 0, }; - println!("{:width$} URL", "Name", width = width); + let url_width: usize = match config + .repositories + .iter() + .max_by(|x, y| x.url.len().cmp(&y.url.len())) + { + Some(x) => x.url.len(), + None => 0, + }; + println!( + "{:name$} {:url$} Category", + "Name", + "URL", + name = name_width, + url = url_width + ); for repo in config.repositories.iter() { - println!("{:width$} {}", repo.name, repo.url, width = width); + println!( + "{name:name_w$} {url:url_w$} {cat}", + name = repo.name, + name_w = name_width, + url = repo.url, + url_w = url_width, + cat = match &repo.category { + Some(x) => x, + None => "", + }, + ); } } Some(Cmd::Fetch) => { @@ -194,23 +236,29 @@ fn fetch_repos(config: Config) { // Loop through the repositories for repo in config.repositories.iter() { + // Get the full path to the repository + let path: String = match &repo.category { + Some(x) => format!("{}/{}/", config.source_dir, x), + None => config.source_dir.clone(), + }; + let fullpath: String = format!("{}/{}", path, repo.name); // Check to see if the folder is already cloned, if so, just fetch - if std::path::Path::new(&format!("{}/{}", config.source_dir, repo.name)).is_dir() { + if std::path::Path::new(&fullpath).is_dir() { println!("=== FETCHING {} ===", repo.name); Command::new("git") - .args([ - "-C", - &format!("{}/{}", config.source_dir, repo.name), - "fetch", - "--all", - ]) + .args(["-C", &fullpath, "fetch", "--all"]) .status() .unwrap(); } else { println!("=== {} doesn't exist locally ===", repo.name); println!("=== CLONING {} ===", repo.name); + match fs::create_dir_all(&path) { + Err(e) => eprintln!("{}", e), + _ => {} + } + println!("{}", path); Command::new("git") - .args(["-C", &config.source_dir, "clone", &repo.url, &repo.name]) + .args(["-C", &path, "clone", &repo.url, &repo.name]) .status() .unwrap(); }