add Dir command

master
Aleš Katona 2 years ago
parent 01b179e93b
commit d38e03b140
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -23,8 +23,8 @@ fn main() {
}; };
match args.command { match args.command {
Commands::Cd { member } => { Commands::Dir { member } => {
todo!(); vcs.dir(&member).expect("Error changing directory");
} }
Commands::Checkout { Commands::Checkout {
reference, reference,

@ -26,6 +26,7 @@ pub struct Vcs {
work_dir: PathBuf, work_dir: PathBuf,
profiles: Vec<String>, profiles: Vec<String>,
projects: Vec<Project>, projects: Vec<Project>,
members: Vec<String>,
} }
impl Vcs { impl Vcs {
@ -59,12 +60,15 @@ impl Vcs {
let workspace = Self::get_workspace_toml(&toml_path)?; let workspace = Self::get_workspace_toml(&toml_path)?;
let members = workspace let members: Vec<String> = workspace
.get("members") .get("members")
.ok_or_else(|| Error::cargo_error("Cargo.toml missing [members] array"))? .ok_or_else(|| Error::cargo_error("Cargo.toml missing [members] array"))?
.as_array() .as_array()
.ok_or_else(|| Error::cargo_error("Cargo.toml [members] is not an 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 vcs_path = work_dir.join("Cargo_vcs.toml");
let profiles_map = if vcs_path.exists() { let profiles_map = if vcs_path.exists() {
@ -86,6 +90,7 @@ impl Vcs {
work_dir, work_dir,
profiles, profiles,
projects, 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( pub fn checkout_ref(
&mut self, &mut self,
ref_name: &str, ref_name: &str,
@ -408,17 +429,14 @@ impl Vcs {
fn process_repos( fn process_repos(
work_dir: &Path, work_dir: &Path,
members: &Vec<Value>, members: &Vec<String>,
profiles: &HashMap<String, HashMap<String, String>>, profiles: &HashMap<String, HashMap<String, String>>,
) -> Result<Vec<Project>, Error> { ) -> Result<Vec<Project>, Error> {
let mut projects = Vec::new(); let mut projects = Vec::new();
for member in members { for member in members {
let repo_postfix = member let repo_path = work_dir.join(member);
.as_str()
.ok_or_else(|| Error::project_error("Repo not a string"))?;
let repo_path = work_dir.join(repo_postfix);
let repo_basename = Path::new(repo_postfix) let repo_basename = Path::new(member)
.components() .components()
.last() .last()
.ok_or_else(|| Error::project_error("Repo basename not found"))? .ok_or_else(|| Error::project_error("Repo basename not found"))?

@ -15,9 +15,9 @@ pub struct Cli {
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub enum Commands { pub enum Commands {
/// Change directory into a specific member folder /// Output absolute directory path of given workspace member root
Cd { Dir {
/// Workspace member name to change directory to /// Workspace member name to show directory for
member: String, member: String,
}, },
Clone { Clone {

Loading…
Cancel
Save