From d38e03b14003de0e66af7ef99c61e4149a0c1867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Katona?= Date: Sun, 31 Jul 2022 21:13:52 -0700 Subject: [PATCH] add Dir command --- src/main.rs | 4 ++-- src/vcs.rs | 34 ++++++++++++++++++++++++++-------- src/vcs/cli.rs | 6 +++--- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index ca69d5f..309822d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,8 +23,8 @@ fn main() { }; match args.command { - Commands::Cd { member } => { - todo!(); + Commands::Dir { member } => { + vcs.dir(&member).expect("Error changing directory"); } Commands::Checkout { reference, diff --git a/src/vcs.rs b/src/vcs.rs index bf323b0..f019154 100644 --- a/src/vcs.rs +++ b/src/vcs.rs @@ -26,6 +26,7 @@ pub struct Vcs { work_dir: PathBuf, profiles: Vec, projects: Vec, + members: Vec, } impl Vcs { @@ -59,12 +60,15 @@ impl Vcs { let workspace = Self::get_workspace_toml(&toml_path)?; - let members = workspace + let members: Vec = workspace .get("members") .ok_or_else(|| Error::cargo_error("Cargo.toml missing [members] array"))? .as_array() .ok_or_else(|| Error::cargo_error("Cargo.toml [members] is not an array"))? - .to_owned(); + .to_owned() + .iter() + .map(|v| v.as_str().unwrap().to_owned()) // TODO: handle the unlikely case + .collect(); let vcs_path = work_dir.join("Cargo_vcs.toml"); let profiles_map = if vcs_path.exists() { @@ -86,6 +90,7 @@ impl Vcs { work_dir, profiles, projects, + members, }) } @@ -279,6 +284,22 @@ impl Vcs { }) } + pub fn dir(&self, member: &str) -> Result<(), Error> { + if let Some(repo_postfix) = self.members.iter().find(|repo_path| { + if let Some(p) = Path::new(repo_path).components().last() { + p.as_os_str().eq_ignore_ascii_case(member) + } else { + false + } + }) { + let repo_path = self.work_dir.join(repo_postfix); + println!("{}", repo_path.display()); + Ok(()) + } else { + Err(Error::project_error("Member not found")) + } + } + pub fn checkout_ref( &mut self, ref_name: &str, @@ -408,17 +429,14 @@ impl Vcs { fn process_repos( work_dir: &Path, - members: &Vec, + members: &Vec, profiles: &HashMap>, ) -> Result, Error> { let mut projects = Vec::new(); for member in members { - let repo_postfix = member - .as_str() - .ok_or_else(|| Error::project_error("Repo not a string"))?; - let repo_path = work_dir.join(repo_postfix); + let repo_path = work_dir.join(member); - let repo_basename = Path::new(repo_postfix) + let repo_basename = Path::new(member) .components() .last() .ok_or_else(|| Error::project_error("Repo basename not found"))? diff --git a/src/vcs/cli.rs b/src/vcs/cli.rs index 00acb0b..71b55ce 100644 --- a/src/vcs/cli.rs +++ b/src/vcs/cli.rs @@ -15,9 +15,9 @@ pub struct Cli { #[derive(Debug, Parser)] pub enum Commands { - /// Change directory into a specific member folder - Cd { - /// Workspace member name to change directory to + /// Output absolute directory path of given workspace member root + Dir { + /// Workspace member name to show directory for member: String, }, Clone {