summaryrefslogtreecommitdiffstats
path: root/src/commands/env_of.rs
blob: c763a23bc241b358821c70fd3a9f7d25c815994a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use std::convert::TryFrom;

use anyhow::Result;
use clap::ArgMatches;
use log::trace;

use crate::package::PackageName;
use crate::package::PackageVersionConstraint;
use crate::repository::Repository;

/// Implementation of the "env_of" subcommand
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(PackageVersionConstraint::try_from)
            .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))
        .try_for_each(|pkg| {
            if let Some(hm) = pkg.environment() {
                for (key, value) in hm {
                    writeln!(stdout, "{} = '{}'", key, value)?;
                }
            } else {
                writeln!(stdout, "No environment")?;
            }

            Ok(())
        })
}