summaryrefslogtreecommitdiffstats
path: root/src/commands/find_pkg.rs
blob: e2f7b776b3c74f66e33edd770b71b9bf5959c1cb (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// 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 anyhow::Context;
use anyhow::Result;
use clap::ArgMatches;
use log::trace;

use crate::config::Configuration;
use crate::package::PackageVersionConstraint;
use crate::repository::Repository;

/// Implementation of the "find_pkg" subcommand
pub async fn find_pkg(
    matches: &ArgMatches,
    config: &Configuration,
    repo: Repository,
) -> Result<()> {
    use std::io::Write;

    let package_name_regex = crate::commands::util::mk_package_name_regex({
        matches.value_of("package_name_regex").unwrap() // safe by clap
    })?;

    let package_version_constraint = matches
        .value_of("package_version_constraint")
        .map(String::from)
        .map(PackageVersionConstraint::new)
        .transpose()
        .context("Parsing package version constraint")
        .context("A valid package version constraint looks like this: '=1.0.0'")?;

    let iter = repo
        .packages()
        .filter(|p| package_name_regex.captures(p.name()).is_some())
        .filter(|p| {
            package_version_constraint
                .as_ref()
                .map(|v| v.matches(p.version()))
                .unwrap_or(true)
        })
        .inspect(|pkg| trace!("Found package: {:?}", pkg));

    let out = std::io::stdout();
    let mut outlock = out.lock();
    if matches.is_present("terse") {
        for p in iter {
            writeln!(outlock, "{} {}", p.name(), p.version())?;
        }
        Ok(())
    } else {
        let flags = crate::ui::PackagePrintFlags {
            print_all: matches.is_present("show_all"),
            print_runtime_deps: crate::commands::util::getbool(
                matches,
                "dependency_type",
                crate::cli::IDENT_DEPENDENCY_TYPE_RUNTIME,
            ),
            print_build_deps: crate::commands::util::getbool(
                matches,
                "dependency_type",
                crate::cli::IDENT_DEPENDENCY_TYPE_BUILD,
            ),
            print_sources: matches.is_present("show_sources"),
            print_dependencies: matches.is_present("show_dependencies"),
            print_patches: matches.is_present("show_patches"),
            print_env: matches.is_present("show_env"),
            print_flags: matches.is_present("show_flags"),
            print_allowed_images: matches.is_present("show_allowed_images"),
            print_denied_images: matches.is_present("show_denied_images"),
            print_phases: matches.is_present("show_phases"),
            print_script: matches.is_present("show_script"),
            script_line_numbers: !matches.is_present("no_script_line_numbers"),
            script_highlighting: !matches.is_present("no_script_highlight"),
        };

        let format = config.package_print_format();
        crate::ui::print_packages(&mut outlock, format, iter, config, &flags)
    }
}