#![deny(unsafe_code)]
use asimov_module_cli::commands;
use clientele::{
StandardOptions,
SysexitsError::{self, *},
crates::clap::{Parser, Subcommand},
};
#[derive(Debug, Parser)]
#[command(name = "asimov-module", long_about)]
#[command(arg_required_else_help = true)]
struct Options {
#[clap(flatten)]
flags: StandardOptions,
#[clap(subcommand)]
command: Option<Command>,
}
#[derive(Debug, Subcommand)]
enum Command {
#[clap(alias = "open")]
Browse {
name: String,
},
#[clap(override_usage = CONFIG_USAGE)]
Config {
name: String,
#[clap(trailing_var_arg = true)]
args: Vec<String>,
},
Disable {
names: Vec<String>,
},
Enable {
names: Vec<String>,
},
#[cfg(feature = "unstable")]
#[clap(alias = "which")]
Find {
name: String,
},
#[cfg(feature = "unstable")]
#[clap(alias = "show")]
Inspect {
name: String,
},
Install {
names: Vec<String>,
},
#[clap(alias = "url")]
Link {
name: String,
},
#[clap(alias = "ls")]
List {
#[arg(value_name = "FORMAT", short = 'o', long)]
output: Option<String>,
},
Resolve {
url: String,
},
Uninstall {
names: Vec<String>,
},
#[clap(alias = "update")]
Upgrade {
names: Vec<String>,
},
}
pub fn main() -> SysexitsError {
clientele::dotenv().ok();
let Ok(args) = clientele::args_os() else {
return EX_USAGE;
};
let options = Options::parse_from(&args);
asimov_module::init_tracing_subscriber(&options.flags).expect("failed to initialize logging");
if options.flags.version {
println!("asimov-module {}", env!("CARGO_PKG_VERSION"));
return EX_OK;
}
if options.flags.license {
print!("{}", include_str!("../UNLICENSE"));
return EX_OK;
}
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(async {
asimov_installer::Installer::default()
.create_file_tree()
.await
.inspect_err(|e| {
tracing::debug!("failed to create module file tree: {e}");
})
.ok();
});
let result = match options.command.unwrap() {
Command::Browse { name } => commands::browse(name, &options.flags),
Command::Config { name, args } => commands::config(name, &args, &options.flags),
Command::Disable { names } => commands::disable(names, &options.flags),
Command::Enable { names } => commands::enable(names, &options.flags),
#[cfg(feature = "unstable")]
Command::Find { name } => commands::find(name, &options.flags),
#[cfg(feature = "unstable")]
Command::Inspect { name } => commands::inspect(name, &options.flags),
Command::Install { names } => commands::install(names, &options.flags),
Command::Link { name } => commands::link(name, &options.flags),
Command::List { output } => {
commands::list(output.as_deref().unwrap_or(&"cli"), &options.flags)
},
Command::Resolve { url } => commands::resolve(url, &options.flags),
Command::Uninstall { names } => commands::uninstall(names, &options.flags),
Command::Upgrade { names } => commands::upgrade(names, &options.flags),
};
match result {
Ok(()) => EX_OK,
Err(err) => err,
}
}
const CONFIG_USAGE: &str = r#"
config <module> # Interactive configuration
config <module> <key> # Show value for key
config <module> [<key> <value>]... # Set key(s) to value(s)
"#;