summaryrefslogtreecommitdiffstats
path: root/src/commands/source.rs
blob: 9cb1f48bf78fdc8a8b2f56ae12547657eb3fde6e (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
use std::io::Write;
use std::path::PathBuf;

use anyhow::Result;
use anyhow::anyhow;
use clap_v3::ArgMatches;

use crate::config::*;
use crate::package::PackageName;
use crate::repository::Repository;
use crate::source::*;

pub async fn source<'a>(matches: &ArgMatches, config: &Configuration<'a>, repo: Repository) -> Result<()> {
    match matches.subcommand() {
        ("verify", Some(matches))     => verify(matches, config, repo).await,
        (other, _) => return Err(anyhow!("Unknown subcommand: {}", other)),
    }
}

pub async fn verify<'a>(matches: &ArgMatches, config: &Configuration<'a>, repo: Repository) -> Result<()> {
    use tokio::stream::StreamExt;

    let source_cache_root = PathBuf::from(config.source_cache_root());
    let sc                = SourceCache::new(source_cache_root);
    let pname             = matches.value_of("package_name").map(String::from).map(PackageName::from);

    repo.packages()
        .filter(|p| pname.as_ref().map(|n| p.name() == n).unwrap_or(true))
        .map(|p| {
            let source = sc.source_for(p);
            async move {
                let out = std::io::stdout();
                if source.exists() {
                    if source.verify_hash().await? {
                        writeln!(out.lock(), "Ok: {}", source.path().display())?;
                    } else {
                        writeln!(out.lock(), "Hash Mismatch: {}", source.path().display())?;
                    }
                } else {
                    writeln!(out.lock(), "Source missing: {}", source.path().display())?;
                }

                Ok(())
            }
        })
        .collect::<futures::stream::FuturesUnordered<_>>()
        .collect::<Result<()>>()
        .await
}