#![warn(missing_docs)]
use clap::{crate_version, Parser, Subcommand};
pub use holochain_cli_bundle as hc_bundle;
use holochain_cli_sandbox as hc_sandbox;
use lazy_static::lazy_static;
use std::process::Command;
mod external_subcommands;
lazy_static! {
static ref HELP: &'static str = {
let extensions = external_subcommands::list_external_subcommands()
.into_iter()
.map(|s| format!(" {s}\t Run \"hc {s} help\" to see its help"))
.collect::<Vec<String>>()
.join("\n");
let extensions_str = match extensions.len() {
0 => String::from(""),
_ => format!(
r#"
Extensions:
{extensions}"#
),
};
let s = format!(
r#"Holochain CLI
Work with DNA, hApp and web-hApp bundle files, set up sandbox environments for testing and development purposes, make direct admin calls to running conductors, and more.
{extensions_str}"#
);
Box::leak(s.into_boxed_str())
};
}
fn builtin_commands() -> Vec<String> {
["hc-web-app", "hc-dna", "hc-app", "hc-sandbox"]
.iter()
.map(|s| s.to_string())
.collect()
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Parser)]
#[command(about = *HELP, infer_subcommands = true, allow_external_subcommands = true, version = crate_version!())]
pub struct Cli {
#[command(subcommand)]
pub subcommand: CliSubcommand,
}
#[derive(Debug, Subcommand)]
#[allow(clippy::large_enum_variant)]
pub enum CliSubcommand {
Dna(hc_bundle::HcDnaBundle),
App(hc_bundle::HcAppBundle),
WebApp(hc_bundle::HcWebAppBundle),
Sandbox(hc_sandbox::HcSandbox),
#[command(external_subcommand)]
External(Vec<String>),
}
impl CliSubcommand {
pub async fn run(self) -> anyhow::Result<()> {
match self {
CliSubcommand::App(cmd) => cmd.run().await?,
CliSubcommand::Dna(cmd) => cmd.run().await?,
CliSubcommand::WebApp(cmd) => cmd.run().await?,
CliSubcommand::Sandbox(cmd) => cmd.run().await?,
CliSubcommand::External(args) => {
let command_suffix = args.first().expect("Missing subcommand name");
Command::new(format!("hc-{command_suffix}"))
.args(&args[1..])
.status()
.expect("Failed to run external subcommand");
}
}
Ok(())
}
}