Skip to main content

asimov_cli/commands/
source.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::BoxError;
4use asimov_module::ModuleName;
5use clientele::{StandardOptions, crates::clap::Subcommand, options::sort::SortKeys};
6
7#[derive(Debug, Subcommand)]
8pub enum SourceCommand {
9    /// Fetch a resource from a URL, utilizing enabled modules.
10    #[clap(aliases = ["extract", "get", "import", "ingest", "parse"])]
11    Fetch {
12        #[clap(flatten)]
13        args: fetch::SourceFetchArgs,
14    },
15
16    /// List resources from a collection URL, utilizing enabled modules.
17    #[clap(aliases = ["dir", "ls"])]
18    List {
19        /// The collection URL(s) to examine.
20        urls: Vec<String>,
21
22        /// The specific module to use.
23        #[clap(long, short = 'M')]
24        module: Option<ModuleName>,
25
26        /// Sort resources by the specified keys. (Prefix a key with `-` for descending order.)
27        #[clap(long, aliases = ["sort-by", "order", "order-by"], value_name = "[+|-]KEY,...", allow_hyphen_values = true)]
28        sort: Option<SortKeys>,
29
30        /// The index offset of the first output.
31        #[clap(value_name = "INDEX", long, default_value = "0")]
32        offset: Option<usize>,
33
34        /// The maximum count of outputs [default: none].
35        #[arg(value_name = "COUNT", short = 'n', long)]
36        limit: Option<usize>,
37
38        /// The output format.
39        #[arg(value_name = "FORMAT", short = 'o', long)]
40        output: Option<String>, // TODO: OutputFormat, default_value = "jsonl"
41    },
42
43    /// Read a resource specified by a URL, utilizing enabled modules
44    Read {
45        #[clap(long, short = 'M')]
46        module: Option<ModuleName>,
47
48        urls: Vec<String>,
49    },
50
51    /// Manage snapshots stored on disk
52    #[cfg(feature = "source-snap")]
53    Snap {
54        #[clap(subcommand)]
55        command: Option<SnapCommand>,
56
57        #[clap(flatten)]
58        args: SnapSaveArgs,
59    },
60}
61
62impl Default for SourceCommand {
63    fn default() -> Self {
64        SourceCommand::Fetch {
65            args: fetch::SourceFetchArgs::default(),
66        }
67    }
68}
69
70impl SourceCommand {
71    pub async fn run(self, flags: &StandardOptions) -> Result<(), BoxError> {
72        use SourceCommand::*;
73        match self {
74            Fetch { args } => fetch(args, flags).await,
75
76            List {
77                urls,
78                module,
79                sort,
80                offset,
81                limit,
82                output,
83            } => list(urls, module, sort, offset, limit, output, flags).await,
84
85            Read { module, urls } => read(urls, module, flags).await,
86
87            #[cfg(feature = "source-snap")]
88            Snap { command, args } => {
89                command
90                    .unwrap_or(SnapCommand::Save { args })
91                    .run(flags)
92                    .await
93            },
94        }
95    }
96}
97
98#[cfg(false)]
99pub mod describe;
100
101mod fetch;
102pub use fetch::*;
103
104mod list;
105pub use list::list;
106
107mod read;
108pub use read::*;
109
110mod snap;
111pub use snap::*;