From 853680633f56c29832198c85937ab39d9eafee72 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 8 Apr 2021 20:24:59 +0200 Subject: Add subcommand to get path to source file Signed-off-by: Matthias Beyer --- src/cli.rs | 18 ++++++++++++++++++ src/commands/source.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index 1473164..c43cc0e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -691,6 +691,24 @@ pub fn cli<'a>() -> App<'a> { .about("Overwrite existing cache entry") ) ) + .subcommand(App::new("of") + .version(crate_version!()) + .about("Get the pathes of the sources of a package") + .arg(Arg::new("package_name") + .required(false) + .multiple(false) + .index(1) + .value_name("PKG") + .about("Get the source file pathes for this package") + ) + .arg(Arg::new("package_version") + .required(false) + .multiple(false) + .index(2) + .value_name("VERSION") + .about("Get the source file pathes for the package in this version") + ) + ) ) .subcommand(App::new("release") diff --git a/src/commands/source.rs b/src/commands/source.rs index 7f4a1bb..0267673 100644 --- a/src/commands/source.rs +++ b/src/commands/source.rs @@ -42,6 +42,7 @@ pub async fn source( Some(("list-missing", matches)) => list_missing(matches, config, repo).await, Some(("url", matches)) => url(matches, repo).await, Some(("download", matches)) => download(matches, config, repo, progressbars).await, + Some(("of", matches)) => of(matches, config, repo).await, Some((other, _)) => return Err(anyhow!("Unknown subcommand: {}", other)), None => return Err(anyhow!("No subcommand")), } @@ -306,3 +307,48 @@ pub async fn download( let (r, _) = tokio::join!(r, multibar_block); r } + +async fn of( + matches: &ArgMatches, + config: &Configuration, + repo: Repository, +) -> Result<()> { + let cache = PathBuf::from(config.source_cache_root()); + let sc = SourceCache::new(cache); + let pname = matches + .value_of("package_name") + .map(String::from) + .map(PackageName::from); + let pvers = matches + .value_of("package_version") + .map(PackageVersionConstraint::try_from) + .transpose()?; + + repo.packages() + .filter(|p| pname.as_ref().map(|n| p.name() == n).unwrap_or(true)) + .filter(|p| { + pvers + .as_ref() + .map(|v| v.matches(p.version())) + .unwrap_or(true) + }) + .map(|p| { + let pathes = sc.sources_for(p) + .into_iter() + .map(|source| source.path()) + .collect::>(); + + (p, pathes) + }) + .fold(Ok(std::io::stdout()), |out, (package, pathes)| { + out.and_then(|mut out| { + writeln!(out, "{} {}", package.name(), package.version())?; + for path in pathes { + writeln!(out, "\t{}", path.display())?; + } + + Ok(out) + }) + }) + .map(|_| ()) +} -- cgit v1.2.3