Skip to main content

asimov_cli/commands/
module.rs

1// This is free and unencumbered software released into the public domain.
2
3use clientele::{StandardOptions, crates::clap::Subcommand};
4use core::error::Error;
5use std::{string::String, vec::Vec};
6
7#[derive(Debug, Subcommand)]
8pub enum ModuleCommand {
9    /// Open the module's package page in a web browser
10    #[clap(alias = "open")]
11    Browse {
12        /// The name of the module to browse
13        name: String,
14    },
15
16    /// Configure an installed module
17    #[clap(override_usage = CONFIG_USAGE)]
18    Config {
19        /// The name of the module to configure
20        name: String,
21
22        /// Unset configured variable(s). By default all when no arguments provided.
23        #[arg(short = 'u', long, default_value = "false")]
24        unset: bool,
25
26        /// A single configuration variable to read, or key-value pair(s) to be set.
27        #[clap(trailing_var_arg = true)]
28        args: Vec<String>,
29    },
30
31    /// Disable modules
32    Disable {
33        /// The names of the modules to disable
34        names: Vec<String>,
35    },
36
37    /// Enable modules
38    Enable {
39        /// The names of the modules to enable
40        names: Vec<String>,
41    },
42
43    /// TBD
44    #[cfg(feature = "unstable")]
45    #[clap(alias = "which")]
46    Find {
47        /// The name of the module to find
48        name: String,
49    },
50
51    /// TBD
52    #[cfg(feature = "unstable")]
53    #[clap(alias = "show")]
54    Inspect {
55        /// The name of the module to inspect
56        name: String,
57    },
58
59    /// Install an available module locally
60    Install {
61        /// The names of the modules to install
62        names: Vec<String>,
63
64        /// Optionally install a specific version instead of latest
65        #[arg(long)]
66        version: Option<String>,
67
68        /// Optionally specify desired model size to download for module.
69        /// Only affects modules which require models.
70        #[arg(long)]
71        model_size: Option<String>,
72    },
73
74    /// Print the module's package link
75    #[clap(alias = "url")]
76    Link {
77        /// The name of the module to link to
78        name: String,
79    },
80
81    /// List all available and/or installed modules
82    #[clap(alias = "ls")]
83    List {
84        /// Set the output format [default: cli] [possible values: cli, jsonl]
85        #[arg(value_name = "FORMAT", short = 'o', long)]
86        output: Option<String>,
87    },
88
89    /// Scaffold a new module
90    New {
91        /// The module's short name, e.g. `widget` for `asimov-widget-module`
92        name: String,
93
94        /// The target directory to create the module in.
95        /// Defaults to `./asimov-<name>-module`.
96        #[arg(long)]
97        dir: Option<String>,
98
99        /// Scaffold a program of this kind, e.g. `fetcher` for
100        /// `asimov-widget-fetcher`. May be repeated to scaffold several
101        /// programs at once.
102        #[arg(long, default_value = "emitter")]
103        program: Vec<String>,
104
105        /// A short summary of what this module does.
106        /// Defaults to a summary derived from the module's name.
107        #[arg(long)]
108        summary: Option<String>,
109    },
110
111    /// Resolve a given URL to modules which can handle it
112    Resolve {
113        /// The URL to resolve
114        url: String,
115    },
116
117    /// Uninstall a currently installed module
118    Uninstall {
119        /// The names of the modules to uninstall
120        names: Vec<String>,
121    },
122
123    /// Upgrade currently installed modules
124    ///
125    /// By default upgrades all installed modules.
126    #[clap(alias = "update")]
127    Upgrade {
128        /// The names of the modules to upgrade
129        names: Vec<String>,
130
131        /// Optionally upgrade to a specific version instead of latest
132        #[arg(long)]
133        version: Option<String>,
134
135        /// Optionally specify desired model size to download for module.
136        /// Only affects modules which require models.
137        #[arg(long)]
138        model_size: Option<String>,
139    },
140}
141
142impl ModuleCommand {
143    pub async fn run(&self, flags: &StandardOptions) -> Result<(), Box<dyn Error>> {
144        use ModuleCommand::*;
145        match self {
146            Browse { name } => browse(name, flags).await,
147            Config { name, unset, args } => config(name, *unset, &args, flags).await,
148            Disable { names } => disable(names, flags).await,
149            Enable { names } => enable(names, flags).await,
150            #[cfg(feature = "unstable")]
151            Find { name } => find(name, flags).await,
152            #[cfg(feature = "unstable")]
153            Inspect { name } => inspect(name, flags).await,
154            Install {
155                names,
156                version,
157                model_size,
158            } => install(names, version, model_size, flags).await,
159            Link { name } => link(name, flags).await,
160            List { output } => list(output.as_deref().unwrap_or(&"cli"), flags).await,
161            New {
162                name,
163                dir,
164                program,
165                summary,
166            } => new(name, dir.as_deref(), program, summary.as_deref(), flags).await,
167            Resolve { url } => resolve(url, flags).await,
168            Uninstall { names } => uninstall(names, flags).await,
169            Upgrade {
170                names,
171                version,
172                model_size,
173            } => upgrade(names, version, model_size, flags).await,
174        }
175    }
176}
177
178// From asimov-module-cli:
179const CONFIG_USAGE: &str = r#"
180    config <module>                     # Interactive configuration
181    config <module> <key>               # Show value for key
182    config <module> [<key> <value>]...  # Set key(s) to value(s)
183"#;
184
185mod browse;
186pub use browse::*;
187
188mod config;
189pub use config::*;
190
191mod disable;
192pub use disable::*;
193
194mod enable;
195pub use enable::*;
196
197mod find;
198pub use find::*;
199
200mod inspect;
201pub use inspect::*;
202
203mod install;
204pub use install::*;
205
206mod link;
207pub use link::*;
208
209mod list;
210pub use list::*;
211
212mod new;
213pub use new::*;
214
215mod resolve;
216pub use resolve::*;
217
218mod uninstall;
219pub use uninstall::*;
220
221mod upgrade;
222pub use upgrade::*;