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 {
Commands::Cd { member } => {
todo!();
Commands::Dir { member } => {
vcs.dir(&member).expect("Error changing directory");
}
Commands::Checkout {
reference,

@ -26,6 +26,7 @@ pub struct Vcs {
work_dir: PathBuf,
profiles: Vec<String>,
projects: Vec<Project>,
members: Vec<String>,
}
impl Vcs {
@ -59,12 +60,15 @@ impl Vcs {
let workspace = Self::get_workspace_toml(&toml_path)?;
let members = workspace
let members: Vec<String> = 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<Value>,
members: &Vec<String>,
profiles: &HashMap<String, HashMap<String, String>>,
) -> Result<Vec<Project>, 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"))?

@ -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 {

Loading…
Cancel
Save