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    #[cfg(feature = "module-new")]
91    New {
92        /// The module's short name, e.g. `widget` for `asimov-widget-module`
93        name: String,
94
95        /// The target directory to create the module in.
96        /// Defaults to `./asimov-<name>-module`.
97        #[arg(long)]
98        dir: Option<String>,
99
100        /// Scaffold a program of this kind, e.g. `fetcher` for
101        /// `asimov-widget-fetcher`. May be repeated to scaffold several
102        /// programs at once.
103        #[arg(long, default_value = "emitter")]
104        program: Vec<String>,
105
106        /// A short summary of what this module does.
107        /// Defaults to a summary derived from the module's name.
108        #[arg(long)]
109        summary: Option<String>,
110    },
111
112    /// Resolve a given URL to modules which can handle it
113    Resolve {
114        /// The URL to resolve
115        url: String,
116    },
117
118    /// Uninstall a currently installed module
119    Uninstall {
120        /// The names of the modules to uninstall
121        names: Vec<String>,
122    },
123
124    /// Upgrade currently installed modules
125    ///
126    /// By default upgrades all installed modules.
127    #[clap(alias = "update")]
128    Upgrade {
129        /// The names of the modules to upgrade
130        names: Vec<String>,
131
132        /// Optionally upgrade to a specific version instead of latest
133        #[arg(long)]
134        version: Option<String>,
135
136        /// Optionally specify desired model size to download for module.
137        /// Only affects modules which require models.
138        #[arg(long)]
139        model_size: Option<String>,
140    },
141}
142
143impl ModuleCommand {
144    pub async fn run(&self, flags: &StandardOptions) -> Result<(), Box<dyn Error>> {
145        use ModuleCommand::*;
146        match self {
147            Browse { name } => browse(name, flags).await,
148            Config { name, unset, args } => config(name, *unset, &args, flags).await,
149            Disable { names } => disable(names, flags).await,
150            Enable { names } => enable(names, flags).await,
151            #[cfg(feature = "unstable")]
152            Find { name } => find(name, flags).await,
153            #[cfg(feature = "unstable")]
154            Inspect { name } => inspect(name, flags).await,
155            Install {
156                names,
157                version,
158                model_size,
159            } => install(names, version, model_size, flags).await,
160            Link { name } => link(name, flags).await,
161            List { output } => list(output.as_deref().unwrap_or(&"cli"), flags).await,
162            #[cfg(feature = "module-new")]
163            New {
164                name,
165                dir,
166                program,
167                summary,
168            } => new(name, dir.as_deref(), program, summary.as_deref(), flags).await,
169            Resolve { url } => resolve(url, flags).await,
170            Uninstall { names } => uninstall(names, flags).await,
171            Upgrade {
172                names,
173                version,
174                model_size,
175            } => upgrade(names, version, model_size, flags).await,
176        }
177    }
178}
179
180// From asimov-module-cli:
181const CONFIG_USAGE: &str = r#"
182    config <module>                     # Interactive configuration
183    config <module> <key>               # Show value for key
184    config <module> [<key> <value>]...  # Set key(s) to value(s)
185"#;
186
187mod browse;
188pub use browse::*;
189
190mod config;
191pub use config::*;
192
193mod disable;
194pub use disable::*;
195
196mod enable;
197pub use enable::*;
198
199mod find;
200pub use find::*;
201
202mod inspect;
203pub use inspect::*;
204
205mod install;
206pub use install::*;
207
208mod link;
209pub use link::*;
210
211mod list;
212pub use list::*;
213
214#[cfg(feature = "module-new")]
215mod new;
216#[cfg(feature = "module-new")]
217pub use new::*;
218
219mod resolve;
220pub use resolve::*;
221
222mod uninstall;
223pub use uninstall::*;
224
225mod upgrade;
226pub use upgrade::*;