asimov_cli/commands/
fetch.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{commands::External, shared::build_resolver, StandardOptions, SysexitsError};
4use color_print::ceprintln;
5use miette::Result;
6
7pub fn fetch(urls: &Vec<String>, flags: &StandardOptions) -> Result<(), SysexitsError> {
8    let resolver = build_resolver("fetcher").map_err(|e| {
9        ceprintln!("<s,r>error:</> failed to build a resolver: {e}");
10        SysexitsError::EX_UNAVAILABLE
11    })?;
12
13    for url in urls {
14        if flags.verbose > 1 {
15            ceprintln!("<s><c>»</></> Fetching `{}`...", url);
16        }
17
18        let modules = resolver.resolve(url)?;
19
20        match modules.first() {
21            Some(module) => {
22                let subcommand = format!("{}-fetcher", module.name);
23
24                let cmd = External {
25                    is_debug: flags.debug,
26                    pipe_output: false,
27                };
28
29                let code = cmd
30                    .execute(&subcommand, &[url.to_owned()])
31                    .map(|result| result.code)?;
32                if code.is_failure() {
33                    return Err(code);
34                }
35            }
36            None => {
37                ceprintln!(
38                    "<s,r>error:</> failed to find a module to fetch the URL: `{}`",
39                    url
40                );
41                return Err(SysexitsError::EX_SOFTWARE);
42            }
43        }
44
45        if flags.verbose > 0 {
46            ceprintln!("<s><g>✓</></> Fetched `{}`.", url);
47        }
48    }
49
50    Ok(())
51}