summaryrefslogtreecommitdiffstats
path: root/src/commands/source.rs
blob: ef5b5c87fe7db98a0ed0364fbe4409da0c1d9d9b (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::io::Write;
use std::path::PathBuf;

use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
use clap::ArgMatches;
use tokio::stream::StreamExt;
use tokio::io::AsyncWriteExt;

use crate::config::*;
use crate::package::Package;
use crate::package::PackageName;
use crate::package::PackageVersionConstraint;
use crate::repository::Repository;
use crate::source::*;
use crate::util::progress::ProgressBars;

pub async fn source(matches: &ArgMatches, config: &Configuration, repo: Repository, progressbars: ProgressBars) -> Result<()> {
    match matches.subcommand() {
        Some(("verify", matches))       => verify(matches, config, repo).await,
        Some(("list-missing", matches)) => list_missing(matches, config, repo).await,
        Some(("url", matches))          => url(matches, config, repo).await,
        Some(("download", matches))     => download(matches, config, repo, progressbars).await,
        Some((other, _)) => return Err(anyhow!("Unknown subcommand: {}", other)),
        None             => return Err(anyhow!("No subcommand")),
    }
}

pub async fn verify(matches: &ArgMatches, config: &Configuration, repo: Repository) -> Result<()> {
    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);
    let pvers             = matches.value_of("package_version").map(String::from).map(PackageVersionConstraint::new).transpose()?;

    let packages = 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));

    let mut out = std::io::stdout();
    verify_impl(packages, &sc, &mut out).await
}

pub (in crate::commands) async fn verify_impl<'a, I>(packages: I, sc: &SourceCache, output: &mut Write) -> Result<()>
    where I: Iterator<Item = &'a Package> + 'a
{
    if let Err(_) = packages
        .map(|p| sc.sources_for(p).into_iter())
        .flatten()
        .map(|source| async move {
            if source.path().exists() {
                match source.verify_hash().await {
                    Ok(true)  => Ok(format!("Ok: {}", source.path().display())),
                    Ok(false) => Err(format!("Hash Mismatch: {}", source.path().display())),
                    Err(e)    => Err(format!("Hash verification failed: {}", e.to_string())), // TODO: make me nice
                }
            } else {
                Err(format!("Source missing: {}", source.path().display()))
            }
        })
        .collect::<futures::stream::FuturesUnordered<_>>()
        .collect::<Vec<std::result::Result<String, String>>>()
        .await
        .into_iter()
        .inspect(|r| { let _ = writeln!(output, "{}", match r { Ok(s) | Err(s) => s }); })
        .map(|r| r.map(|_| ()).map_err(|_| ()))
        .collect::<std::result::Result<(), ()>>()
    {
        Err(anyhow!("At least one package failed with source verification"))
    } else {
        Ok(())
    }
}

pub async fn list_missing(_: &ArgMatches, config: &Configuration, repo: Repository) -> Result<()> {
    let sc          = SourceCache::new(PathBuf::from(config.source_cache_root()));
    let out         = std::io::stdout();
    let mut outlock = out.lock();

    repo.packages()
        .map(|p| {
            for source in sc.sources_for(p) {
                if !source.exists() {
                    writeln!(outlock, "{} {} -> {}", p.name(), p.version(), source.path().display())?;
                }
            }

            Ok(())
        })
        .collect()
}

pub async fn url(matches: &ArgMatches, config: &Configuration, repo: Repository) -> Result<()> {
    let out         = std::io::stdout();
    let mut outlock = out.lock();

    let pname = matches.value_of("package_name").map(String::from).map(PackageName::from);
    let pvers = matches.value_of("package_version").map(String::from).map(PackageVersionConstraint::new).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| {
            p.sources()
                .iter()
                .map(|source| writeln!(outlock, "{} {} -> {}", p.name(), p.version(), source.url()).map_err(Error::from))
                .collect()
        })
        .collect()
}

pub async fn download(matches: &ArgMatches, config: &Configuration, repo: Repository, progressbars: ProgressBars) -> Result<()> {
    let force = matches.is_present("force");
    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(String::from).map(PackageVersionConstraint::new).transpose()?;
    let multi = indicatif::MultiProgress::new();

    let r = 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| {
            sc.sources_for(p)
                .into_iter()
                .map(|source| {
                    let bar = multi.add(progressbars.download_bar(source.url()));
                    async move {
                        if source.exists() && !force {
                            Err(anyhow!("Source exists: {}", source.path().display()))
                        } else {
                            if source.exists() {
                                let _ = source.remove_file().await?;
                            }

                            trace!("Starting download...");
                            let file = source.create().await?;
                            let mut file = tokio::io::BufWriter::new(file);
                            let response = reqwest::get(source.url().as_ref()).await?;
                            if let Some(len) = response.content_length() {
                                bar.set_length(len);
                            }
                            let mut stream = reqwest::get(source.url().as_ref()).await?.bytes_stream();
                            while let Some(bytes) = stream.next().await {
                                let bytes = bytes?;
                                file.write_all(bytes.as_ref()).await?;
                                bar.inc(bytes.len() as u64);
                            }

                            file.flush().await?;
                            bar.finish_with_message("Finished download");
                            Ok(())
                        }
                    }
                })
        })
        .flatten()
        .collect::<futures::stream::FuturesUnordered<_>>()
        .collect::<Result<()>>()
        .await;
    multi.join()?;
    r
}