From 2acac26f6cc378eec5b0b5a10d39239499d92ba3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 9 Nov 2020 16:42:26 +0100 Subject: Add subcommand: env-of Signed-off-by: Matthias Beyer --- src/cli.rs | 18 ++++++++++++++++++ src/commands/env_of.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 3 +++ src/main.rs | 5 +++++ src/package/version.rs | 1 + 5 files changed, 69 insertions(+) create mode 100644 src/commands/env_of.rs (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index 3470503..1bba3d3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -189,6 +189,24 @@ pub fn cli<'a>() -> App<'a> { .help("The name of the package") ) ) + .subcommand(App::new("env-of") + .alias("env") + .about("Show the ENV configured for a package") + .arg(Arg::with_name("package_name") + .required(true) + .multiple(false) + .index(1) + .value_name("PACKAGE_NAME") + .help("The name of the package") + ) + .arg(Arg::with_name("package_version_constraint") + .required(false) + .multiple(false) + .index(2) + .value_name("VERSION_CONSTRAINT") + .help("A version constraint to search for (optional)") + ) + ) } diff --git a/src/commands/env_of.rs b/src/commands/env_of.rs new file mode 100644 index 0000000..4ce5161 --- /dev/null +++ b/src/commands/env_of.rs @@ -0,0 +1,42 @@ +use anyhow::Error; +use anyhow::Context; +use anyhow::Result; +use clap_v3::ArgMatches; + +use crate::package::PackageName; +use crate::package::PackageVersionConstraint; +use crate::repository::Repository; + +pub async fn env_of(matches: &ArgMatches, repo: Repository) -> Result<()> { + use filters::filter::Filter; + use std::io::Write; + + let package_filter = { + let name = matches.value_of("package_name").map(String::from).map(PackageName::from).unwrap(); + let constraint = matches.value_of("package_version_constraint").map(String::from).map(PackageVersionConstraint::new).unwrap()?; + trace!("Checking for package with name = {} and version = {:?}", name, constraint); + + crate::util::filters::build_package_filter_by_name(name) + .and(crate::util::filters::build_package_filter_by_version_constraint(constraint)) + }; + + let mut stdout = std::io::stdout(); + repo.packages() + .filter(|package| package_filter.filter(package)) + .inspect(|pkg| trace!("Found package: {:?}", pkg)) + .map(|pkg| { + if let Some(hm) = pkg.environment() { + for (key, value) in hm { + writeln!(stdout, "{} = '{}'", key, value)?; + } + } else { + writeln!(stdout, "No environment")?; + } + + Ok(()) + }) + .collect::>() +} + + + diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d44c6f1..a745f8a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,9 @@ mod build; pub use build::build; +mod env_of; +pub use env_of::env_of; + mod dependencies_of; pub use dependencies_of::dependencies_of; diff --git a/src/main.rs b/src/main.rs index 7edd932..de25f13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,6 +86,11 @@ async fn main() -> Result<()> { ("versions-of", Some(matches)) => { let repo = load_repo()?; crate::commands::versions_of(matches, repo).await? + }, + + ("env-of", Some(matches)) => { + let repo = load_repo()?; + crate::commands::env_of(matches, repo).await? } (other, _) => return Err(anyhow!("Unknown subcommand: {}", other)), diff --git a/src/package/version.rs b/src/package/version.rs index f6f7d2c..8cc2c84 100644 --- a/src/package/version.rs +++ b/src/package/version.rs @@ -1,5 +1,6 @@ use std::ops::Deref; +use anyhow::Error; use anyhow::Result; use pom::parser::Parser as PomParser; use serde::Deserialize; -- cgit v1.2.3