pub mod init;
pub mod pipelines;
pub mod plugin;
pub mod render;
pub mod run;
pub mod version;
pub use plugin::PluginCommand;
pub use run::RunArgs;
use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::context::RunContext;
#[derive(Debug, Parser)]
#[command(
name = "hm",
version,
about = "hm — CLI for the Harmont CI platform",
long_about = "hm is the command-line interface for Harmont.\n\n\
Run `hm run` to push local code through a pipeline without committing.",
propagate_version = true,
arg_required_else_help = true,
disable_help_subcommand = true
)]
pub struct Cli {
#[arg(long, short, global = true)]
pub verbose: bool,
#[arg(long, global = true)]
pub no_color: bool,
#[arg(long, global = true, hide = true, value_name = "PATH")]
pub debug_trace: Option<std::path::PathBuf>,
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Clone, Subcommand)]
pub enum Command {
Init(init::InitArgs),
Run(RunArgs),
Pipelines(pipelines::PipelinesArgs),
Render(render::RenderArgs),
Version,
#[command(subcommand)]
Plugin(PluginCommand),
#[command(subcommand)]
Cache(CacheCommand),
#[command(subcommand)]
Cloud(hm_plugin_cloud::cli::CloudCommand),
}
#[derive(Debug, Clone, Subcommand)]
pub enum CacheCommand {
Save(CacheSaveArgs),
Restore(CacheRestoreArgs),
Clean,
}
#[derive(Debug, Clone, clap::Args)]
pub struct CacheSaveArgs {
pub dir: PathBuf,
}
#[derive(Debug, Clone, clap::Args)]
pub struct CacheRestoreArgs {
pub dir: PathBuf,
}
pub async fn dispatch(command: Command, ctx: RunContext) -> Result<i32> {
match command {
Command::Init(args) => crate::commands::init::handle(args).await.map(|()| 0),
Command::Run(args) => crate::commands::run::handle(args, ctx).await,
Command::Pipelines(args) => crate::cli::pipelines::run(args).await.map(|()| 0),
Command::Render(args) => crate::cli::render::run(args).await.map(|()| 0),
Command::Cache(cmd) => match cmd {
CacheCommand::Save(args) => crate::commands::cache::handle_save(&args.dir).await,
CacheCommand::Restore(args) => crate::commands::cache::handle_restore(&args.dir).await,
CacheCommand::Clean => crate::commands::cache::handle_clean().await,
},
Command::Version => version::run().await.map(|()| 0),
Command::Plugin(cmd) => plugin::run(cmd).await.map(|()| 0),
Command::Cloud(cmd) => {
let env = std::env::vars().collect();
hm_plugin_cloud::cli::dispatch_command(cmd, env).await
}
}
}